开发者

How to create a rotation based Impulse Vector (Cocos2d, Chipmunk, Spacemanager)

So im trying to create character with two jetpacks - either of which can be fired independently of one another to create an impulse offset from the center of gravity (Using Cocos2d, Chipmunk, and SpaceManager).

My problem is that the by default, the impulse function doesn't take into account the current rotation of the object (i.e. which way its pointing), therefor the impulse offset and direction that I use ends up being the same no matter what direction the character is pointing in.

Im trying to create a more realistic model - where the impulse is based on the existing rotation of the object. Im sure I could programmatically just maintain a vector variable that holds the current direction the character is pointing and use that, but there has to be a simpler answer.

Iv heard people write about world space vs body relative coordinates and how impulse is world space by default, and body relative w开发者_JAVA技巧ould fix my problem. Is this true? If so how do you convert between these two coordinate systems?

Any help you could give me would be Greatly appreciated.


If you have the current heading of your character (the angle it has rotated through, going counter-clockwise) stored in theta, and your impulse vector is in ix and iy, then the world-space vector will be

ix_world = ix * cos(theta) - iy * sin(theta);
iy_world = ix * sin(theta) + iy * cos(theta);

The angle theta must be in radians for cos and sin to work correctly. If you need to convert from degrees to radians, multiply the angle by PI / 180.0.

If you want to see where this formula came from, read here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜