3

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.

Salman
  • 494
  • 4
  • 18
  • Sounds like you just need to transform your impulse from object space to world space. – Stephen Waits Mar 16 '11 at 22:48
  • 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 Mar 22 '11 at 03:55

1 Answers1

1

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.

Alex
  • 2,152
  • 2
  • 17
  • 20
  • What about inbuilt functions within chipmunk? Couldnt we also use the functions `cpVect cpBodyLocal2World(cpBody *body, cpVect v)` and `cpVect cpBodyWorld2Local(cpBody *body, cpVect v)` ? – Salman Mar 27 '11 at 18:06
  • I don't know anything about Chipmunk. – Alex Mar 28 '11 at 00:18