2011-03-14 47 views
1

我正在做一個類似水果忍者的遊戲。然後向上飛起來的鳥(就像水果朝上一樣)cocos2d水果忍者拋物線數學

但是有些鳥飛得太遠而其他太近。 有人可以檢查我的代碼嗎? (vx不是問題)

static float tuna = 10.0f; 
-(void) reset 
{ 

    float vy = 0.0f; 
    float vx = 0.0f; 
    int sign = 1; 
    if (CCRANDOM_0_1() >= 0.5) { 
     sign = -1; 
    } 

    float hurry = 0.0f; 
    if (CCRANDOM_0_1() <= 0.1) { 
     hurry = 1.0f; 
    } 

    switch (birdType) { 
     case BirdType1: 
      vx = 1.0f * sign + (CCRANDOM_0_1() - 0.5f) * 0.08f; 
      vy = -6.5f; 
      break; 
     case BirdType2: 
      vx = 1.5f * sign + (CCRANDOM_0_1() - 0.5f) * 0.08f; 
      vy = -6.2f + (CCRANDOM_0_1() - 0.5f) * 0.1f; 
      break; 
     case BirdType3: 
      vx = 1.0f * sign + (CCRANDOM_0_1() - 0.5f) * 0.1f; 
      vy = -5.8f - hurry; 
      break; 
     default: 
      [NSException exceptionWithName:@"BirdMoveComponent exception" reason:@"unhandled bird type" userInfo:nil]; 
      break; 
    } 
    velocity = CGPointMake(vx * 5, vy * 5); 


    if ((int)([[GameManager sharedManager] score]/100) >= prevLevel) { 
     if (tuna <= 12.0f) { 
      tuna += 0.01f; 
     } 
     prevLevel = (int)[[GameManager sharedManager] score]/100; 
    } 



} 


-(void) update:(ccTime) delta 
{ 

    if (self.parent.visible) { 

     NSAssert([self.parent isKindOfClass:[BirdEntity class]], @"node is not an entity"); 
     BirdEntity* bird = (BirdEntity*) self.parent; 

     bird.position = ccpAdd(bird.position, ccpMult(velocity, delta * tuna)); 

     velocity = ccpAdd(velocity, ccpMult(acceleration, delta * tuna)); 
     acceleration = ccp(0, 0.3f); 

     float birdHeight = CGRectGetHeight([bird boundingBox]); 

     //20 is the bottom trap 
     if (bird.position.y <= (birdHeight/2) + 20) { 
      [bird dieAccidently]; 
     } 

     if (CGRectIntersectsRect([GameScene screenRect], [bird boundingBox]) == NO) 
     { 
      bird.visible = NO; 

      [bird stopAllActions]; 
      [bird unscheduleAllSelectors]; 
      [bird unscheduleUpdate]; 

      [self reset]; 
     } 
    } 

} 
+1

水果忍者與鳥...聽起來像一場血洗! – 2011-03-15 10:42:42

+0

是的,這就是我想要的效果。仍在努力。在1個月內搜索「Fishes vs Birds」(包括蘋果批准時間)^ _^ – John 2011-03-17 17:22:05

+0

我會關注它的 - 當您獲得批准時,請給我一個鏈接! N – 2011-03-21 11:22:12

回答

2

你的問題不是編程的,而是物理的(機械的)。對象的

位置能夠從方程式的系統來計算:

x = Vx * t + x0 
y = (-g*t*t)/2 + Vy * t + y0 

,其中g - 重力加速度,V - 初始速度,Vx和Vy - 其上分別軸X和Y,預測。

問題是最高點,即我們需要找到MAX(y(t))。 導數:y'(t) = -g*t + Vy.

y'(t) should equals zero, -g*t + Vy = 0; t = Vy/g; MAX(y) = y(Vy/g) = Vy*Vy/2g. 

MAX(y) = Vy*Vy/2g + y0 // ballistic trajectory 
MIN(y) = y0 - Vy*Vy/2g // your case 

末,你應該計算速度accroding這一點,如果你希望你的鳥y以在一定範圍內。

增加:

順便說一句是有 拋物線樣品的cocos2d代碼?

這是我的工作代碼。

- (void) update: (ccTime)dt 
    { 
     t += dt*20; 
     ... 
     [self getVertices]; 
    } 

    - (void) getVertices 
    { 
     //for every index: { 
     ... 
     //getting initial position (x0, y0) 
     ... 
vertices[index] = ccpAdd(vertices[index], ccpMult(velocity[index/3], t * screenFactor)); //+velocity*t 
vertices[index] = ccpAdd(vertices[index], ccpMult(gravity, (t*t/2) * screenFactor)); //+acceleration*t^2 /2 
     //} 
    } 

1)如你所見,沒有必要每次都計算速度:用初始速度。 2)頂點是當前Sprite位置的CGPoint數組。 3)t(當前時間),頂點,重力,速度是普通類的實例變量。

+0

我使用CGPoint封裝速度,與加速度相同。加速度是積極的,因爲鳥類在水下(像水果忍者顛倒),順便說一下,cocos2d的起源是左下角 – John 2011-03-14 18:56:53

+0

我明白了,但它只是一個細節;好,修正的公式是MIN(Y)= y0 - Vy * Vy/2g – 2011-03-15 04:29:09

+0

我修改了代碼。將速度乘以3.只是三角洲混淆。 – John 2011-03-15 10:08:04