1

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?

Andy Hin
  • 30,345
  • 42
  • 99
  • 142

1 Answers1

1

cpBodyApplyImpulse takes imputs in body coordinates, not world.

Try just this :

cpBodyApplyImpulse(sprite.body, 
                    cpv(0.0, 1.0), 
                    cpv(0.0, 0.0));
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • Thanks. That worked, can't believe I missed that. One more quick question though - maybe I've been thinking about this too long but how can a vector defined as a single point? Where is the origin of the vector? – Andy Hin Feb 20 '12 at 15:37
  • In this case, the origin of both vectors is the centre of gravity of your body. A vector (0,0) just means don't offset by anything. (The spin you had before was because in world-coordinates this vector was not 0,0 so you were applying your force to one side of your object) – deanWombourne Feb 20 '12 at 16:17
  • @deanWombourne body-coordinates is not actually correct for the last value. The value wants to be in world coordinates, so the correct answer would be `cpBodyApplyImpulse(sprite.body, cpv(0.0, 1.0), cpBodyLocal2World(sprite.body, cpv(0.0, 0.0)) ); ` In this case it doesn't actually matter as cpv(0,0) will just map to the same value – Rob Jan 17 '14 at 19:30
  • And actually the 2nd parameter also is in world coordinates but you don't ever need to map it from local; just need to find the appropriate value. – Rob Jan 17 '14 at 19:36