3
votes

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 would 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.

1
Sounds like you just need to transform your impulse from object space to world space.Stephen Waits
Iv heard people say this before - but I cant seem to find a specific way to do this. Is there a predefined method in cocos2d that im missing here?Salman

1 Answers

1
votes

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.