Cocos2d +花栗鼠物理 - cpBodyApplyImpulse()
所以我正在开发一款游戏,本质上是一个球的自上而下的视图。当我点击球时,我基本上想将其发射到给定的方向。这将是瞬时的力量(想象一下用球杆击打台球)。我正在尝试使用 applyImpulse 来执行此操作。
到目前为止,我已经得到:
cpBodyApplyImpulse(sprite.body,
cpBodyLocal2World(sprite.body, cpv(0.0, 1.0)),
cpBodyLocal2World(sprite.body, cpv(0.0, 0.0))
);
据我所知,该函数接受的两个向量是“世界坐标”,所以我所做的是将身体相对坐标中的所有内容可视化,然后将它们转换为世界坐标。
从上面的代码中,我认为球会直接向上发射,不会旋转,因为它是施加在重心的正 y 方向上的矢量。然而,球最终偏向右侧,无法控制地旋转。有什么想法为什么会发生这种情况,以及我如何实现我想要做的事情吗?
So I am working on a game, essentially, its a top down view of a ball. When I click the ball, I basically want to launch it in a given direction. It would be instantaneous force (think hitting a pool ball with the cue). I am trying to use applyImpulse to do this.
So far I've got:
cpBodyApplyImpulse(sprite.body,
cpBodyLocal2World(sprite.body, cpv(0.0, 1.0)),
cpBodyLocal2World(sprite.body, cpv(0.0, 0.0))
);
As I understand it, the 2 vectors that this function takes in is "world-coords" so what I am doing is visualizing everything in body-relative coordinates and then converting them to world coords.
From my above code, I would think that the ball would launch straight up with no rotation because it is a vector in the positive-y direction applied at the center of gravity. However, the ball ends up going to the right, spinning uncontrollably. Any ideas why this is happening, and how I achieve what I am trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cpBodyApplyImpulse 采用身体坐标而不是世界坐标进行输入。
试试这个:
cpBodyApplyImpulse
takes imputs in body coordinates, not world.Try just this :