线性脉冲 - Cocos2d/Objective-C/Box2d

发布于 2024-12-25 20:18:59 字数 3355 浏览 0 评论 0原文

我有一门大炮、一个球和一个扳机。当您按下扳机时,就会向球施加线性脉冲。

short int direction = [level.cannon cannon].rotation;

short int power = 24.8;
b2Vec2 force = b2Vec2(direction, power);
[level.ball body]->ApplyLinearImpulse(force, [level.ball body]->GetWorldCenter());

我的问题是,当按下扳机时,线性脉冲会施加到球上,但球实际上并没有从大炮精灵的顶部出来。

它这样做的原因(我认为)是因为我已将大炮的锚点设置为(0.5,0)。

cannon.anchorPoint = ccp(0.5, 0);

我发现大炮每旋转 5 度,就需要在旋转中加上/减去 2 的倍数。

EG

对于 0 到 55 度

0 到 5 从旋转中减去 2 5 到 10 循环减 4 10 到 15 循环减 6 等等

对于 0 到 -55 度

0 到 -5 旋转加 2 -5 到 -10 旋转加 4 -10 到 -15 在旋转中添加 6

目前,我正在使用此代码来完成此操作。

if (_cannon.rotation == 0)
    {
        direction = _cannon.rotation;
    } else if (_cannon.rotation >= 1 && _cannon.rotation < 6)
    {
        direction = _cannon.rotation - 2;
    } else if (_cannon.rotation >= 6 && _cannon.rotation < 11)
    {
        direction = _cannon.rotation - 4;
    } else if (_cannon.rotation >= 11 && _cannon.rotation < 16)
    {
        direction = _cannon.rotation - 6;
    } else if (_cannon.rotation >= 16 && _cannon.rotation < 21)
    {
        direction = _cannon.rotation - 8;
    } else if (_cannon.rotation >= 21 && _cannon.rotation < 26)
    {
        direction = _cannon.rotation - 10;
    } else if (_cannon.rotation >= 26 && _cannon.rotation < 31)
    {
        direction = _cannon.rotation - 12;
    } else if (_cannon.rotation >= 31 && _cannon.rotation < 36)
    {
        direction = _cannon.rotation - 14;
    } else if (_cannon.rotation >= 36 && _cannon.rotation < 41)
    {
        direction = _cannon.rotation - 16;
    } else if (_cannon.rotation >= 41 && _cannon.rotation < 46)
    {
        direction = _cannon.rotation - 18;
    } else if (_cannon.rotation >= 46 && _cannon.rotation < 55)
    {
        direction = _cannon.rotation - 20;
    } else if (_cannon.rotation <= -1 && _cannon.rotation > -6)
    {
        direction = _cannon.rotation + 2;
    } else if (_cannon.rotation <= -6 && _cannon.rotation > -11)
    {
        direction = _cannon.rotation + 4;
    } else if (_cannon.rotation <= -11 && _cannon.rotation > -16)
    {
        direction = _cannon.rotation + 6;
    } else if (_cannon.rotation <= -16 && _cannon.rotation > -21)
    {
        direction = _cannon.rotation + 8;
    } else if (_cannon.rotation <= -21 && _cannon.rotation > -26)
    {
        direction = _cannon.rotation + 10;
    } else if (_cannon.rotation <= -26 && _cannon.rotation > -31)
    {
        direction = _cannon.rotation + 12;
    } else if (_cannon.rotation <= -31 && _cannon.rotation > -36)
    {
        direction = _cannon.rotation + 14;
    } else if (_cannon.rotation <= -36 && _cannon.rotation > -41)
    {
        direction = _cannon.rotation + 16;
    } else if (_cannon.rotation <= -41 && _cannon.rotation > -46)
    {
        direction = _cannon.rotation + 18;
    } else if (_cannon.rotation <= -46 && _cannon.rotation > -55)
    {
        direction = _cannon.rotation + 20;
    }

我知道必须有一种更简单的方法来做到这一点,但我仍在学习。有人可以帮我吗?

I have a cannon, a ball, and a trigger. When you press the trigger a linear impulse is applied to the ball.

short int direction = [level.cannon cannon].rotation;

short int power = 24.8;
b2Vec2 force = b2Vec2(direction, power);
[level.ball body]->ApplyLinearImpulse(force, [level.ball body]->GetWorldCenter());

My problem is, when the trigger is pressed, the linear impulse is applied to the ball, but the ball doesn't actually come out of the top of the cannon sprite.

The reason it is doing this (I think) is because I have set the anchor point, for the cannon, to (0.5, 0).

cannon.anchorPoint = ccp(0.5, 0);

I have figured out that for every 5 degrees the cannon rotates, a multiple of 2 needs to be added/subtracted to the rotation.

E.G.

For 0 to 55 Degrees

0 to 5 subtract 2 from the rotation
5 to 10 subtract 4 from the rotation
10 to 15 subtract 6 from the rotation
etc.

For 0 to -55 Degrees

0 to -5 add 2 to the rotation
-5 to -10 add 4 to the rotation
-10 to -15 add 6 to the rotation

Currently, I am using this code to accomplish this.

if (_cannon.rotation == 0)
    {
        direction = _cannon.rotation;
    } else if (_cannon.rotation >= 1 && _cannon.rotation < 6)
    {
        direction = _cannon.rotation - 2;
    } else if (_cannon.rotation >= 6 && _cannon.rotation < 11)
    {
        direction = _cannon.rotation - 4;
    } else if (_cannon.rotation >= 11 && _cannon.rotation < 16)
    {
        direction = _cannon.rotation - 6;
    } else if (_cannon.rotation >= 16 && _cannon.rotation < 21)
    {
        direction = _cannon.rotation - 8;
    } else if (_cannon.rotation >= 21 && _cannon.rotation < 26)
    {
        direction = _cannon.rotation - 10;
    } else if (_cannon.rotation >= 26 && _cannon.rotation < 31)
    {
        direction = _cannon.rotation - 12;
    } else if (_cannon.rotation >= 31 && _cannon.rotation < 36)
    {
        direction = _cannon.rotation - 14;
    } else if (_cannon.rotation >= 36 && _cannon.rotation < 41)
    {
        direction = _cannon.rotation - 16;
    } else if (_cannon.rotation >= 41 && _cannon.rotation < 46)
    {
        direction = _cannon.rotation - 18;
    } else if (_cannon.rotation >= 46 && _cannon.rotation < 55)
    {
        direction = _cannon.rotation - 20;
    } else if (_cannon.rotation <= -1 && _cannon.rotation > -6)
    {
        direction = _cannon.rotation + 2;
    } else if (_cannon.rotation <= -6 && _cannon.rotation > -11)
    {
        direction = _cannon.rotation + 4;
    } else if (_cannon.rotation <= -11 && _cannon.rotation > -16)
    {
        direction = _cannon.rotation + 6;
    } else if (_cannon.rotation <= -16 && _cannon.rotation > -21)
    {
        direction = _cannon.rotation + 8;
    } else if (_cannon.rotation <= -21 && _cannon.rotation > -26)
    {
        direction = _cannon.rotation + 10;
    } else if (_cannon.rotation <= -26 && _cannon.rotation > -31)
    {
        direction = _cannon.rotation + 12;
    } else if (_cannon.rotation <= -31 && _cannon.rotation > -36)
    {
        direction = _cannon.rotation + 14;
    } else if (_cannon.rotation <= -36 && _cannon.rotation > -41)
    {
        direction = _cannon.rotation + 16;
    } else if (_cannon.rotation <= -41 && _cannon.rotation > -46)
    {
        direction = _cannon.rotation + 18;
    } else if (_cannon.rotation <= -46 && _cannon.rotation > -55)
    {
        direction = _cannon.rotation + 20;
    }

I know there has to be an easier way to do this, but I'm still learning. Can someone please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-01-01 20:18:59
 short int val, val2;
 val = _cannon.rotation/5;
 val2 = _cannon.rotation%5; //remainder of the division
                            //it will be zero when rotation is a multiple of 5. 
 if(val2 > 0)
    val += 1;
 direction = _cannon.rotation - 2*val;

我不确定这是否是你想要的,我也没有测试过。只是做了一些心理测试,看看它是否适用于您提出的案例。

但我希望您可以以此为起点来缩小您的代码。

 short int val, val2;
 val = _cannon.rotation/5;
 val2 = _cannon.rotation%5; //remainder of the division
                            //it will be zero when rotation is a multiple of 5. 
 if(val2 > 0)
    val += 1;
 direction = _cannon.rotation - 2*val;

I'm not sure if this is what you want, and I didn't test it. Just made some mental tests to see if it works for the cases you put.

But I hope you can use this as a starting point for making your code smaller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文