cocos2d水果忍者抛物线数学 [英] cocos2d fruit ninja parabola math

查看:151
本文介绍了cocos2d水果忍者抛物线数学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个类似水果忍者的游戏。鸟儿飞到水面上,然后向上(就像水果一面朝下)

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

但是一些鸟飞得太远,而其他鸟儿飞得太近。
有人可以检查我的代码吗? vy应该非常接近(vx不是问题)

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];
        }
    }

}


推荐答案

你的问题不是程序化的,而是物理的(机械的)。

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 

$ b b

,其中g - 重力加速度,V - 初始速度,Vx和Vy - 它分别在X轴和Y轴上的投影。

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

问题是什么是最高点,即我们需要找到MAX(y(t))。
衍生物: y'(t)= -g * t + Vy。

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

如果你想让你的鸟Y在一定范围内,你应该计算速度。

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

添加:


btw有一个样例cocos2d代码
parabola?

btw is there a sample cocos2d code for parabola?

这是我的工作代码。

    - (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)正如你所看到的,没有必要每次都计算Velocity:使用初始速度。
2)顶点是当前Sprite位置的CGPoint数组。
3)t(当前时间),顶点,重力,速度是公共类的实例变量。

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.

这篇关于cocos2d水果忍者抛物线数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆