cocos2d fruit ninja parabola math

主宰稳场 提交于 2019-12-24 15:09:57

问题


i am making a game similar to fruit ninja. birds flying down the water and then up (just like fruits up side down)

but some of the birds fly too far and the others too near. can someone check my code? vy should quite close to each other.(vx is not a problem)

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:


thoght your question not programmatical but physical (mechanical).

position of object can be calculated from the system of equation:

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

, where g - Gravitational acceleration, V - initial speed, Vx and Vy - its projections on axes X and Y, respectively.

Question is what's the highest point, i.e. we need to found MAX(y(t)). derivative: 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

End you should calculate velocity accroding to this, if you want your bird Y to be in certain range.

Addition:

btw is there a sample cocos2d code for parabola?

Here is my working code.

    - (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) As you can see, there's no need to calculate Velocity every time: use initial speed. 2) Vertices is CGPoint array of current Sprite positions. 3) t (current time), vertices, gravity, velocity are instance variables of common class.



来源:https://stackoverflow.com/questions/5295028/cocos2d-fruit-ninja-parabola-math

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!