线性脉冲 - Cocos2d/Objective-C/Box2d
我有一门大炮、一个球和一个扳机。当您按下扳机时,就会向球施加线性脉冲。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否是你想要的,我也没有测试过。只是做了一些心理测试,看看它是否适用于您提出的案例。
但我希望您可以以此为起点来缩小您的代码。
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.