typedef struct direction
{
char q; // quadrant (up_left, up_right, low_left, low_right)
double c; // coefficient in degrees (0 .. 90)
} bdir;
struct ball
{
int xpos;
int ypos;
bdir dir;
} mainball;
int ploc; // refers to paddle location
int jump; // refers to velocity
//ball_cycle is called somewhere from a real timer signal handler.
void ball_cycle(void)
{
int paddle_hit_resolution = 0;
if (mainball.dir.q == up_left)
{ //sin and cos from math.h
mainball.xpos-=jump * cos(mainball.dir.c * PI/180);
mainball.ypos-=jump * sin(mainball.dir.c * PI/180);
if(mainball.xpos < 0)
{
mainball.dir.q = up_right;
}
if(mainball.ypos < 0)
{
mainball.dir.q = low_left;
}
return;
}
if (mainball.dir.q == up_right)
{
mainball.xpos+=jump * cos(mainball.dir.c * PI/180);
mainball.ypos-=jump * sin(mainball.dir.c * PI/180);
if(mainball.xpos > window_x_size - BALL_WIDTH)
{
mainball.dir.q = up_left;
}
if(mainball.ypos < 0)
{
mainball.dir.q = low_right;
}
return;
}
if (mainball.dir.q == low_left)
{
mainball.xpos-=jump * cos(mainball.dir.c * PI/180);
mainball.ypos+=jump * sin(mainball.dir.c * PI/180);
if(mainball.xpos < 1)
{
mainball.dir.q = low_right;
}
if(mainball.ypos > window_y_size - (BALL_HEIGHT + PADDLE_HEIGHT)) // paddle hit?
{
mainball.dir.q = up_left;
if ((mainball.xpos >= ploc) && (mainball.xpos <= ploc + PADDLE_WIDTH))
{
score++;
/*
this is where the question is about.
*/
} else
{
lost();
}
}
return;
}
if (mainball.dir.q == low_right)
{
mainball.xpos+=jump * cos(mainball.dir.c * PI/180);
mainball.ypos+=jump * sin(mainball.dir.c * PI/180);
if(mainball.xpos > window_x_size - BALL_WIDTH)
{
mainball.dir.q = low_left;
}
if(mainball.ypos > window_y_size - (BALL_HEIGHT + PADDLE_HEIGHT) ) //paddle hit?
{
mainball.dir.q = up_right;
if ((mainball.xpos >= ploc ) && (mainball.xpos <= ploc + PADDLE_WIDTH))
{
score++;
/*
here too.
*/
} else
{
lost();
}
}
return;
}
return;
}
上面的代碼是來自槳/球遊戲的片段,爲我的教育寫作。我如何計算球/槳碰撞?
有兩個地方(見評論)這個問題是關於。
當球朝向屏幕的下部(當mainball.dir.q被low_left或low_right)
球移動或者撞擊擋板和移動到另一個象限(up_left或up_right),或者,它錯過了槳,失敗()被稱爲並且遊戲結束。 (45度,所以當球擊中屏幕牆時,它也會向另一個象限反彈45度)。
在我的情況下,PADDLE_WIDTH等於120(像素)。所以我可以堅決在(ex)180部分的槳。例如。
resolution = ((mainball.xpos - ploc) * 180)/PADDLE_WIDTH
(所以無論地方球擊中槳,將由0和180之間的值,而不管槳寬度的指示,對嗎?)
在兩個地方我需要計算一個碰撞。 (槳有速度,定義爲全局變量別的地方)
(球已經與槳相撞後,我需要有一個新的角度,讓遊戲開始尋找流暢。)
會是什麼在球與槳碰撞後成爲新的mainball.dir.c值?
我猜的分辨率值,甚至可能速度應通過計算新mainball.dir.c使用。
你能縮小你的問題和代碼嗎?看起來太長了。 – 2014-09-10 12:33:15
[gamedev SE](http://gamedev.stackexchange.com/)可能會更好。 – Quentin 2014-09-10 12:51:51