初心者向けのDirectXプログラミングの課題:バウンドするグラフィックの停止方法についてのアドバイスをお願いします

このQ&Aのポイント
  • DirectXプログラミングの初心者のためのアドバイスとして、クライアントエリア内でグラフィックをバウンドさせる課題について解説します。
  • グラフィックのバウンドが停止する条件やプログラムを書く方法について詳しく説明します。
  • 具体的なサンプルコードを示しながら、バウンドするグラフィックを止めるプログラムを作成する手順を解説します。
回答を見る
  • ベストアンサー

DirectX

現在学生でDirectXプログラミングを学んでいます。 そこである課題を出されたのですが、まだまだ初心者でなかなか問題が解けないのでアドバイスをお願いします。 問題はクライアントエリア内でとあるグラフィックをバウンドさせて、バウンドしているグラフィックがバウンドをする力がなくなったときにその場で停止させるというものです。ですが、停止させる時のプログラムをどう書いてよいのか分かりません。 ソースは static void MoveId(void) { const double g = 2.0; const double i = 0.9; dIdX += dIdSpeedH; if (dIdX<0||dIdX > nClientWidth - nIdWidth) dIdSpeedH = 0.0 - dIdSpeedH; dIdSpeedV += g; dIdY += dIdSpeedV; if (dIdY<0||dIdY > nClientHeight - nIdHeight) dIdSpeedV = 0.0 - dIdSpeedV; if(dIdY>nClientHeight - nIdHeight) dIdY=nClientHeight - nIdHeight; dIdSpeedV = i + dIdSpeedV; } どうかアドバイスをお願いします。

  • stt31
  • お礼率100% (1/1)

質問者が選んだベストアンサー

  • ベストアンサー
  • atushi256
  • ベストアンサー率62% (10/16)
回答No.1

> dIdSpeedH = 0.0 - dIdSpeedH; dIdSpeedHの絶対値が変わらないため、少なくとも絶対に横方向には グラフィックは動き続けてしまう気がします。 止めるためには、なんらかの摩擦の様なものや、反発係数を変化させる かける必要があるのではないでしょうか? 具体的には以下のような感じに、 double masatu = 0.95; double hanpatu = 0.5; dIdSpeedH *= masatu; dIdSpeedV *= masatu; if (dIdX<0||dIdX > nClientWidth - nIdWidth) dIdSpeedH = hanpatu*(0.0 - dIdSpeedH); if (dIdY<0||dIdY > nClientHeight - nIdHeight) dIdSpeedV = hanpatu*(0.0 - dIdSpeedV); #物理的に正しいかどうかとかは無視してます。

stt31
質問者

お礼

お礼が遅くなって申し訳ありません。 つい先日、ここで頂いたアドバイスなどを元に何とかこのプログラムが完成しました。 どうもありがとうございました。

関連するQ&A

  • プログラミング 関数の問題について

    どうもお世話になってます。プログラミングの問題ですが問題を見てもよく分らないので答えを教えてください。 double型の数値を引数にして呼び出されたら、それまでに渡された数値の最大値を返す関数 double maxd(double)を完成しなさい。 (1)maxd((2) x) {(3) int i=0; (4)double max; if((5)){ (6); (7); }else if((8))(9); return((10)); } 知り合いに聞いてみたところ (1)double(2)double(3)static(4)static(5)i=0(7)max = x(8)max < x(9)max = x(10)max と言われて説明してもらったのですが、説明してもらってもよく分りませんでした・・・。(そもそもプログラミング分かってないのですが・・・。) 答えだけ聞くのは本末転倒なのですが、上の答えで合っているのでしょうか?もし違っていたら申し訳ないですが正しい答えを教えてください。よろしくお願いします。

  • DirectX キーが入力されるまで一時停止

    下記の処理で0ボタンを押すとループ処理を抜ける処理をしたいのです。 その際、キーが押されて無い場合キー入力取得の所で停止させ、押された瞬間動き始めるプログラムを組みたいのですが、出来るのでしょうか? const BYTE *keys; for( int i = 0; i == 0; i ) { // キー入力の取得 keys = iGetKey(); // 0キーはキャンセル if( keys['0'] & 0x80 ) { break; } } 開発環境 VisualStudio2008 C++ SP1 Platform SDK(多分最新) DirectX SDK(多分最新)

  • 内定をもらった会社から課題がだされてしまいました。

    正直まったくもってソースコードをいじった事がありません。なのでわかる方いたらぜひ回答お願いします。 ロボットアームが自動的に動くようにしたいんですけどそのソースコードがわからなくてわかるかたいたら教えてください。 #include <stdlib.h> #include <math.h> #include <GL/glut.h> /* * 直方体を描く */ static void myBox(double x, double y, double z) { GLdouble vertex[][3] = { { -x, -y, -z }, { x, -y, -z }, { x, y, -z }, { -x, y, -z }, { -x, -y, z }, { x, -y, z }, { x, y, z }, { -x, y, z } }; const static int face[][4] = { { 0, 1, 2, 3 }, { 1, 5, 6, 2 }, { 5, 4, 7, 6 }, { 4, 0, 3, 7 }, { 4, 5, 1, 0 }, { 3, 2, 6, 7 } }; const static GLdouble normal[][3] = { { 0.0, 0.0,-1.0 }, { 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0 }, {-1.0, 0.0, 0.0 }, { 0.0,-1.0, 0.0 }, { 0.0, 1.0, 0.0 } }; const static GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 }; int i, j; /* 材質を設定する */ glMaterialfv(GL_FRONT, GL_DIFFUSE, red); glBegin(GL_QUADS); for (j = 0; j < 6; ++j) { glNormal3dv(normal[j]); for (i = 4; --i >= 0;) { glVertex3dv(vertex[face[j][i]]); } } glEnd(); } /* * 円柱を描く */ static void myCylinder(double radius, double height, int sides) { const static GLfloat yellow[] = { 0.8, 0.8, 0.2, 1.0 }; double step = 6.28318530717958647692 / (double)sides; int i = 0; /* 材質を設定する */ glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow); /* 上面 */ glNormal3d(0.0, 1.0, 0.0); glBegin(GL_TRIANGLE_FAN); while (i < sides) { double t = step * (double)i++; glVertex3d(radius * sin(t), height, radius * cos(t)); } glEnd(); /* 底面 */ glNormal3d(0.0, -1.0, 0.0); glBegin(GL_TRIANGLE_FAN); while (--i >= 0) { double t = step * (double)i; glVertex3d(radius * sin(t), -height, radius * cos(t)); } glEnd(); /* 側面 */ glBegin(GL_QUAD_STRIP); while (i <= sides) { double t = step * (double)i++; double x = sin(t); double z = cos(t); glNormal3d(x, 0.0, z); glVertex3f(radius * x, height, radius * z); glVertex3f(radius * x, -height, radius * z); } glEnd(); } /* * 地面を描く */ static void myGround(double height) { const static GLfloat ground[][4] = { { 0.6, 0.6, 0.6, 1.0 }, { 0.3, 0.3, 0.3, 1.0 } }; int i, j; glBegin(GL_QUADS); glNormal3d(0.0, 1.0, 0.0); for (j = -5; j < 5; ++j) { for (i = -5; i < 5; ++i) { glMaterialfv(GL_FRONT, GL_DIFFUSE, ground[(i + j) & 1]); glVertex3d((GLdouble)i, height, (GLdouble)j); glVertex3d((GLdouble)i, height, (GLdouble)(j + 1)); glVertex3d((GLdouble)(i + 1), height, (GLdouble)(j + 1)); glVertex3d((GLdouble)(i + 1), height, (GLdouble)j); } } glEnd(); } /* * 画面表示 */ static void display(void) { const static GLfloat blue[] = { 0.2, 0.2, 0.8, 1.0 }; /* 球の色 */ const static GLfloat lightpos[] = { 3.0, 4.0, 5.0, 1.0 }; /* 光源の位置 */ /* 画面クリア */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* モデルビュー変換行列の初期化 */ glLoadIdentity(); /* 光源の位置を設定 */ glLightfv(GL_LIGHT0, GL_POSITION, lightpos); /* 視点の移動(シーンの方を奥に移す)*/ glTranslated(0.0, 0.0, -10.0); /* シーンの描画 */ myGround(-2.0); /* 地面    */ glTranslated(0.0, -1.8, 0.0); myCylinder(1.0, 0.2, 16); /* 土台    */ glTranslated(0.0, 1.0, 0.0); myBox(0.3, 1.0, 0.3); /* 1番目の腕 */ glTranslated(0.0, 1.0, 0.0); glRotated(90.0, 1.0, 0.0, 0.0); myCylinder(0.4, 0.4, 16); /* 関節    */ glTranslated(0.0, 0.0, -1.0); myBox(0.3, 0.3, 1.0); /* 2番目の腕 */ glTranslated(0.0, 0.0, -1.0); glMaterialfv(GL_FRONT, GL_DIFFUSE, blue); glutSolidCube(0.9); /* ハンド   */ glFlush(); } static void resize(int w, int h) { /* ウィンドウ全体をビューポートにする */ glViewport(0, 0, w, h); /* 透視変換行列の指定 */ glMatrixMode(GL_PROJECTION); /* 透視変換行列の初期化 */ glLoadIdentity(); gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0); /* モデルビュー変換行列の指定 */ glMatrixMode(GL_MODELVIEW); } static void keyboard(unsigned char key, int x, int y) { /* ESC か q をタイプしたら終了 */ if (key == '\033' || key == 'q') {

  • 無名ネームスペース中のstatic宣言について

    お世話になります。 以下のように無名ネームスペース内でstatic定数を宣言することに 何か問題はあるでしょうか? QACという静的コードチェックツールで 「無名ネームスペース中のstatic宣言」と 警告が出てしまいます。 test.h ----------------------- class Test{ void testFunc(); }; test.cpp ------------------------ namespace { static const int i = 3; } void Test::testFunc(){ std::cout << i << std::endl; }

  • newとdeleteな気がするエラー

    以下のようなプログラムを書いているのですが、delete [] ang;の行を実行すると、 Debug Assertion Failed! Program: ...\Debug\pro.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp Line: 52 Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) というエラーが発生します。 fscanfを書いてからうまくいかなくなってきたので、modtorがオーバーフローしてるのかな?とも思ったのですが、特にそれらしいものは見当たりません。 いったい何が原因なのでしょうか? よろしくお願いします。 #include<stdio.h> #include<string> using namespace std; void main(void){   int i;   int ret;   static const int anum=3;   static const int LoopMax=100;   double *modtor=new double[LoopMax*anum];   double *ang=new double[LoopMax*2*anum];   const string strlearned="A.csv";   const string strlearning="_learning.csv";   FILE *fpl;   FILE *fpvnmd;   errno_t err;   double outputv[anum]={0};   double tempd;   double *tarr=new double[LoopMax];   //delete [] tarr;      //ここで消すとエラーが消える   fopen_s(&fpl,strlearned.c_str(),"r");   printf("データ読み込み\n");   for(i=0;i<LoopMax;i++){     if(fscanf(fpl,"%[^,],%lf,%lf,%lf,%lf",&tempd,&modtor[anum*i],&modtor[anum*i+1],&modtor[anum*i+2])==EOF)break;   }   fclose(fpl);     delete [] ang;   delete [] tarr;   delete [] modtor; }

  • c++ クラスに関してです。

    うまくクラス同士を連動させることができなくてエラーが出てしまいます。 どこが間違えているのかアドバイスくださると助かります。 #ifndef H22_H_ #define H22_H_ #include <string> #include "account.h" class Bank { public: Bank(); void deposit(double amount, const std::string& accountType); void withdraw(double amount, const std::string& accountType); void transfer(double amount, const std::string& accountType); double getBalance() const; double getSavingsBalance() const; double getCheckingBalance() const; private: Account checking, savings; }; #endif #ifndef ACCOUNT_H #define ACCOUNT_H #include <string> #include "account.h" class Account{ public: Account(); Account(double &amout); double deposit(double amount); double getBalance() const; double withdraw(double amount); double balance; private: }; #endif #include <iostream> #include "acount.h" using namespace std; Account::Account { balance = 0; } Account::Account(double amount) { balance = amount; } Account::double deposit() const { return balance; } Account::double getBalance(double balance) { balance += amount; return balance; } Account::double withdraw(double amount) { if(amount > balance) { balance -= 5; return balance; } elese { balance -= amount; return balance; }} h22.h の中身 #include <string> #include "h22.h" #include "account.h" #include <stdexcept> using namespace std; Bank::Bank() { Account checking; Account saving; } void Bank::deposit(double amount, const string& accountType) { if (accountType != "S" && accountType != "C") throw invalid_argument("Does not compute"); else if(accountType == "S") savings.balance += amount; else if(accountType == "C") checking.balance += amount; } void Bank::withdraw(double amount, const string& accountType) { if (accountType != "S" && accountType != "C") throw invalid_argument("Does not compute"); else if(accountType == "S") savings.balance -= amount; else if(accountType == "C") checking.balance -= amount; } void Bank::transfer(double amount, const string& accountType) { if (accountType != "S" && accountType != "C") { throw invalid_argument("Does not compute");} else if(accountType == "S") { savings.balance += checking.balance; savings.balance = 0;} else if(accountType == "C") { checking.balance += amount; checking.balance = 0;} } double Bank::getBalance() const { return getBalance(); } double Bank::getSavingsBalance() const { return savings.getBalance(); } double Bank::getCheckingBalance() const { return checking.getBalance(); } メイン.cpp http://pastebin.com/rQTj8xciです。 なにかヒントやアドバイスお願いします

  • Javaについて質問します。

    全員のjudgeBMIの適用結果を表示させたいのですが 下から4行目のcalcBMIにエラーが出てしまいます。 どこを修正すればいいかわからなく困っています。 わかる方いましたら教えていただきたく質問しました。 public class Enshu14_3 { private static double calcBMI(double weights, double heights){ return(weights/(heights*heights)); } private static String judgeBMI(double bmi){ String result; if(bmi<18.5){ result="やせ"; }else if(30.0<=bmi){ result="高度肥満"; }else if(25.0<=bmi){ result="肥満"; }else{ result="標準"; } return result; } public static void main(String[] args){ double[] weights = {64.0,51.5,70.2,58.3,85.4}; double[] heights = {1.65,1.67,1.66,1.75,1.68}; for(int i=0; i<weights.length; i++){ System.out.println(i+"番目の人は"+judgeBMI(calcBMI(weights,heights))+"です。"); } } }

  • Geforce4MX460でDirectX8.1をインストールしてから困っています。

    初めて質問します DirectX7.0の時は特に問題はなかったのですが、8.1にアップしてからDirectDraw対応のゲームが上手く表示できなくなってしまいました。 普通の3Dはちゃんと表示できています。DirectX診断ツールでも特に問題は ありません。Geforce4MXはDirectX7.0ベースのようですが製品のマニュアルにはいちお8.1サポートと書いてました。ドライバも最新の物をインストールしましたがだめでした。 解決方法など何でもいいのでどなたかお願いします。 GB MSI製Geforce4MX460 MB EPox製4G4A(i845G)

  • DirectXの表示について

    はじめまして、sakamoto-kanadeと申します。 DirectXについての質問です。 言語はC++です。 スクエアエニックス様の代表作でもある、ドラゴンクエストの戦闘シーンのメッセージを再現したいと思います。 しかし、私のソースではメッセージが瞬間表示なってしまいます。 期待値では、1~2を繰り返すことです。 1) メッセージ表示 2) 2秒待ち 以下、現在のソースの状態です。 ※重要な場所以外の処理は簡略しています。 DirectXで、メッセージを表示したまま処理をとめる場合はどうするのでしょうか? それができない場合のロジックをご教授いただけないでしょうか? よろしくお願いいたします。 TCHAR szMsg[MAX_PATH]; unsigned int iFlag= 0; int iColorWhite = GetColor(255,255,255); int WinMain() {   if( ChangeWindowMode(TRUE) != DX_CHANGESCREEN_OK, DxLib_Init() == -1 )return -1; // メイン関数   while( !ProcessMessage() && !ClearDrawScreen() )   { Zeromemory(szMsg, sizeof(szMsg)); srand((unsigned int)time(NULL)); // 0~2の乱数を発生 iFlag = 0 + (int)(rand() * (2-0+1.0)/(1.0+RAND_MAX)); Battle_Msg(iFlag, szMsg); DrawFormatString( 20, 350, iColorWhite, _T("%s") ,szMsg); ScreenFlip(); } void Battle_Msg(int i, *szMsg) { if( i == 0){ _tcscpy( szMsg, _T("主人公の攻撃")); } if( i == 1){ _tcscpy( szMsg, _T("主人公の攻撃")); } if( i == 2){ _tcscpy( szMsg, _T("主人公の攻撃")); } // 一秒待つ WaitTimer(2000); return; } 以上

  • C言語の参照はずしについて

    ソートのプログラムなんですが #include <stdio.h> #include <stdlib.h> int comp(const void *, const void *); int main() { int i; int test[6] = {10, 8, 2, 6, 4, 0}; qsort(test, (size_t)6, sizeof(int), comp); printf("\n"); for (i = 0; i < 6; i++) printf("%d\n", test[i]); return 0; } int comp(const void *a, const void *b) { static int i = 1; printf("%02d--%d,%d\n", i, *(int *)a, *(int *)b); i++; return (*(int *)a - *(int *)b); } 最後のreturnの()の中身がよくわかりません。「参照はずし」という事をしてるらしいんですが「参照はずし」とは何ですか意味も教えてください。