ページ 11

GetRand関数について

Posted: 2011年12月01日(木) 07:40
by nekogori
今、このサイトを参考にさせていただいてシューティングゲームを作っているのですが、DxLibにあるGetRand関数が原因だと思われる不具合に悩まされています。
以下ソースコードです。

~Shot.cpp~

コード:

void Shot_Syosin_Initialize( Enemy_Syosin_t Syosin[] )
{
	Syosin[0].SyosinShotG = LoadGraph("Graph/敵 弾01.png") ;
	Syosin[1].SyosinShotG = LoadGraph("Graph/敵 弾04.png") ; 

	for( i = 0 ; i < SYOSIN ; i++ )
	{
		Syosin[i].SyosinShotC = 0 ;
		Syosin[i].SyosinShotFtmp = 0 ;
		GetGraphSize( Syosin[0].SyosinShotG , &Syosin[i].SSW0 , &Syosin[i].SSH0 ) ;
		GetGraphSize( Syosin[1].SyosinShotG , &Syosin[i].SSW1 , &Syosin[i].SSH1 ) ;

		for( u = 0 ; u < SYOSINSHOT0 ; u++ )
		{
			Syosin[i].SyosinShotF0[u] = 0 ;
		}

		for( u = 0 ; u < SYOSINSHOT1 ; u++ )
		{
			Syosin[i].SyosinShotAngle1[u] = 0 ;
			Syosin[i].SyosinShotF1[u] = 0 ;
		}
	}
}

void Shot_Syosin_BeforeDischarge( Enemy_Syosin_t Syosin[] , Player_t Player )
{
	for( i = 0 ; i < SYOSIN ; i++ )
	{
		Syosin[i].SyosinShotC++ ;
	}

	for( i = 0 ; i < SYOSIN ; i++ )
	{
		//弾の計算
		if( Syosin[i].SyosinShotC > 360 && ( Syosin[i].SyosinX >= 0 && Syosin[i].SyosinX <= 350 && Syosin[i].SyosinY >= 50 && Syosin[i].SyosinY <= 400 ) )
		{	
			switch( Syosin[i].SyosinShotFtmp )
			{
			case 0 :
				{
					for( u = 0 ; u < SYOSINSHOT0 ; u++ )
					{
                                                //※
						Syosin[i].Randangle[u] = (PI/4) + GetRand(10000) / 10000 * (PI/2) ;
						Syosin[i].RandSpeed[u] = 1 + GetRand(10000) / 10000 * 3 ;
						
						Syosin[i].SyosinGuidSX0[u] = cos( Syosin[i].Randangle[u] ) * Syosin[i].RandSpeed[u] ;
						Syosin[i].SyosinGuidSY0[u] = sin( Syosin[i].Randangle[u] ) * Syosin[i].RandSpeed[u] ;

						Syosin[i].SyosinShotX0[u] = Syosin[i].SyosinX + Syosin[i].SW / 2 ;
						Syosin[i].SyosinShotY0[u] = Syosin[i].SyosinY + Syosin[i].SH / 2 ;

						Syosin[i].SyosinShotF0[u] = 1 ;
					}
							
						Syosin[i].SyosinShotC = 0 ;
						Syosin[i].SyosinShotFtmp = 1 ;

					break ;
				}
			case 1 :
				{
					Syosin[i].SyosinShotC = 0 ;
					Syosin[i].SyosinShotFtmp = 0 ;
					break ;
				}
			}
		}
	}
}

void Shot_Syosin_Discharge( Enemy_Syosin_t Syosin[] )
{
	DrawFormatString( 0 , 50 ,GetColor(255,255,255),"angle=%d", y ) ;

	for( i = 0 ; i < SYOSIN ; i++ )
	{
		for( u = 0 ; u < SYOSINSHOT0 ; u++ )
		{
			if( Syosin[i].SyosinShotF0[u] == 1 )
			{
				Syosin[i].SyosinShotX0[u] += Syosin[i].SyosinGuidSX0[u] ;
				Syosin[i].SyosinShotY0[u] += Syosin[i].SyosinGuidSY0[u] ;

				//もし弾が画面外に入ったら消す
				if( Syosin[i].SyosinShotY0[u] < -50 || Syosin[i].SyosinShotY0[u] > 400 || Syosin[i].SyosinShotX0[u] < -50 || Syosin[i].SyosinShotX0[u] > 350 )
				{
					Syosin[i].SyosinShotF0[u] = 0 ;
					Syosin[i].SyosinShotY0[u] = 500 ;
				}

				DrawGraph( Syosin[i].SyosinShotX0[u] , Syosin[i].SyosinShotY0[u] , Syosin[0].SyosinShotG , TRUE ) ;
			}
		}
	}
}
以下、ショシンの構造体があるhファイルです。

コード:

//敵 ショシンの構造体
#define SYOSIN 2
#define SYOSINSHOT0 2
#define SYOSINSHOT1 2

typedef struct{
	int SyosinX , SyosinY ;
	int SyosinCount ;
	int SyosinHP ;
	int SyosinFlag ;
	int SyosinActionFlag ;
	int SyosinGraph ;
	int SW , SH ;
	double SyosinShotX0[SYOSINSHOT0] , SyosinShotY0[SYOSINSHOT0] ;
	int SyosinShotF0[SYOSINSHOT0] ;
	double SyosinShotX1[SYOSINSHOT1] , SyosinShotY1[SYOSINSHOT1] ;
	int SyosinShotF1[SYOSINSHOT1] ;
	int SyosinShotFtmp ;
	int SyosinShotC ;
	int SyosinStotCB ;
	int SyosinShotG ;
	int SSW0 , SSH0 ;
	int SSW1 , SSH1 ;
	double SyosinGuidSX0[SYOSINSHOT0] ;
	double SyosinGuidSY0[SYOSINSHOT0] ;
	double RandSpeed[SYOSINSHOT0] , Randangle[SYOSINSHOT0] ;
	double SyosinGuidSX1[SYOSINSHOT1] ;
	double SyosinGuidSY1[SYOSINSHOT1] ;
	double SyosinShotAngle1[SYOSINSHOT1] ;
} Enemy_Syosin_t ;
※のある場所が不具合の原因だと思われるGetRandです。
実行すると弾がランダムに変化せず、アングルはPI/4に代入され、スピードは1のままになっています。
GetRandをかえて10000などの数値を適当に入れてみたところ、ちゃんと方向が変わったりスピードが速くなりました。
この不具合がわかる方どうかよろしくお願いします。

開発環境
Visual C++ 2008 Express Edition
DxLib
Windows XP

Re: GetRand関数について

Posted: 2011年12月01日(木) 07:56
by beatle
GetRand関数はint型で乱数を返しますから、それをint型の数値で割ったら小数点以下は切り捨てられます。

コード:

int a = 10;
double b = a / 20;
とやってもbは0.5にはなりませんね。そういうことです。

それから、なぜ「1」だけ全角の「1」なのでしょうか。そこでエラーは出ないのでしょうか。

コード:

Syosin[i].Randangle[u] = (PI/4) + GetRand(10000) / 10000 * (PI/2) ;

Re: GetRand関数について

Posted: 2011年12月01日(木) 12:50
by nekogori
そういうことでしたか!単純のことで悩んでいたんですね(馬鹿
ありがとうございました。
beatle さんが書きました: それから、なぜ「1」だけ全角の「1」なのでしょうか。そこでエラーは出ないのでしょうか。
記入ミスです・・・。すみません。急いでいたもので・・・

本当にありがとうございました。