ページ 11

バットの当たり反応について

Posted: 2011年2月07日(月) 23:44
by ジョーイ
ご観覧ありがとうございます。

早速本題に移りますがゲーム作成をしていてバットとボールの当たり判定で詰まってしまったのでここに書き込みさせて頂きました。

バットの根元と先端の座標にボールがきたらはボールの座標を移動するようにプログラミングしたのですが。ボールが移動してくれません。

下にソースファイルを書いてますので、どこがおかしいかを指摘ください。よろしくお願いします

#include "DxLib.h"
#include "math.h"
#define PI 3.1415926
typedef struct{
int x,y,img;
}ch_t;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){

char Key[256];
ch_t ch[2];
int swing=0;
double bat_top1_x , bat_top2_x , bat_top1_y ,bat_top2_y;
if( ChangeWindowMode(TRUE) != DX_CHANGESCREEN_OK || DxLib_Init() == -1 ) return -1; //ウィンドウ化と初期化処理
int ax=2;
int ay=-2;
ch[0].x =320;
ch[0].y =320;
ch[1].x =240;
ch[1].y =240;
SetDrawScreen( DX_SCREEN_BACK ) ; //描画先を裏画面に設定
ch[0].img=LoadGraph("pad.png");
ch[1].img=LoadGraph("ball.png");

while(!ProcessMessage() && !ClearDrawScreen() && !GetHitKeyStateAll( Key ) && !Key[KEY_INPUT_ESCAPE]){
//↑メッセージ処理 ↑画面をクリア ↑キーボード入力状態取得 ↑ESCが押されると終了

/*ボール操作*/
if( Key[ KEY_INPUT_RIGHT ] == 1 ) //右ボタンが押されたら
ch[1].x=ch[1].x+1 ; //xの値を1増やす
if( Key[ KEY_INPUT_LEFT ] == 1 ) //左ボタンが押されたら
ch[1].x=ch[1].x-10 ; //xの値を10減らす
if( Key[ KEY_INPUT_DOWN ] == 1 ) //下ボタンが押されたら
ch[1].y=ch[1].y+1 ; //yの値を1増やす
if( Key[ KEY_INPUT_UP ] == 1 ) //上ボタンが押されたら
ch[1].y=ch[1].y-10 ; //yの値を10減らす

/*バット操作*/
if( CheckHitKey( KEY_INPUT_X ) == 1 ) //Xボタンが押されたら
{
swing=swing-3; //バットを振る
if(swing<=-8) //バットを一定以上振らない
{
swing=-8;
}
}
else //Xボタンが押されなかったら
{
swing++ ; //バットが戻る
if(swing>=8) //バットを一定以上戻さない
swing=8;
}

/*壁の当たり判定*/
if (ch[1].x < 0)
{
ch[1].x = 0;
ax =-ax;
}
else if (ch[1].x > 628)
{
ch[1].x = 628;
ax = -ax;
}
if (ch[1].y < 0)
{
ch[1].y = 0;
ay = -ay;
}

bat_top1_x=cos(PI*((double)swing*1.0/20.0))*128; //バットの幅の長さが128のため
bat_top1_y=sin(PI*((double)swing*1.0/20.0))*16; //バットの縦の長さが16のため

/*バットとボールの当たり判定*/
if(ch[0].x < ch[1].x && ch[0].y < ch[1].y && ch[0].x + bat_top1_x > ch[1].x && ch[0].y + bat_top1_y > ch[1].y )
ch[1].x = 10;


ch[1].x=ch[1].x+ax;
ch[1].y=ch[1].y+ay;
DrawRotaGraph2( ch[0].x , ch[0].y , 0 , 0 ,1 , PI*((double)swing*1.0/20.0) , ch[0].img , TRUE );
DrawGraph( ch[1].x , ch[1].y , ch[1].img , TRUE ) ;
ScreenFlip();//裏画面を表画面に反映
}

DxLib_End();
return 0;
}

Re: バットの当たり反応について

Posted: 2011年2月08日(火) 03:42
by ジョーイ
自己解決しましたー迷惑かけてもうしわけないです。