2014-09-10 77 views
-1
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使用。

+2

你能縮小你的問題和代碼嗎?看起來太長了。 – 2014-09-10 12:33:15

+0

[gamedev SE](http://gamedev.stackexchange.com/)可能會更好。 – Quentin 2014-09-10 12:51:51

回答

1

我想我得到了你所問的,這聽起來像你有興趣在與槳碰撞後得到球的方向。

我經常看到這樣做的方式是保持x方向和y方向的速度,並假定所有的碰撞都是完全彈性的(從而允許保存動量和動能)。這樣做的好處是,衝突解決由四個簡單的案件處理:

  • 碰撞與遊戲區的頂部,在x方向的速度不受碰撞和速度在y方向相反;即v_ {y}^{'} = -v_ {y}。

  • 碰撞與遊戲區域的左手側,在y方向上的速度不受碰撞,而在x方向上的速度被反轉;即x_ {x}^{'} = -v_ {x}。

  • 與遊戲區域(或槳板)底部碰撞,x方向的速度不受碰撞的影響,而y方向的速度相反。

    與遊戲區域的右手側
  • 相撞,在y方向上的速度不受在x方向上的碰撞和速度反轉。

這樣做的好處是,我們避免了昂貴的操作,如罪和COS,以及避免浮點操作。

+0

當我開始使用這種技術時,是不是衝擊的角度與結果的角度相同?主要思想是槳葉給球發出一個音高,這樣角度就會改變。 – 2014-09-10 20:48:15

+0

是的,入射角等於反射角。我正在考慮遊戲中的突破,比如槳在屏幕底部移動(並且與遊戲場底部平行)。我沒有意識到你的槳是傾斜的(如果我理解你的評論)。當我實現了一個突破克隆時,如果我想通過對y-velocity進行細微更改來修改音高。例如,並且非常簡單地說,我有一個平行於底部的槳,並使用從槳中點的距離作爲y速度的修飾符。 – thurizas 2014-09-10 20:53:32

+0

我明白爲什麼此主題被拒絕。在衝浪時,我看到了很多(大部分)相同的問題。我需要做一些關於遊戲物理和碰撞的更好的作業。和平。 – 2014-09-16 14:21:44