Applying an impulse to a body in the direction of a touch

General discussion about Box2D tutorials
Post Reply
therealcos
Posts: 16
Joined: Tue Aug 20, 2013 7:30 am

Applying an impulse to a body in the direction of a touch

Post by therealcos »

Hi there,

I have a zero gravity environment with a single body to which I'd like to apply an impulse. When I touch the screen at a given location, I'd like to apply a linear impulse to the body so that it travels in the direction of that touch location. I know I would need to get the body location and the touch location, and then find the difference between the two in order to get an appropriate vector, but I'm not exactly sure how to do the math and what it looks like in code. How would I do this? I'm doing this within a Cocos2d project, so any Obj-C/C++ code I could use would be extremely helpful...

Thanks in advance!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Applying an impulse to a body in the direction of a touc

Post by iforce2d »

The direction is just the vector from where you are, to where you want to go:

b2Vec2 direction = touchLocation - body->GetPosition();

Then you can use that direction with ApplyForce or ApplyImpulse. You might like to normalize the direction vector before using it: viewtopic.php?f=4&t=81
therealcos
Posts: 16
Joined: Tue Aug 20, 2013 7:30 am

Re: Applying an impulse to a body in the direction of a touc

Post by therealcos »

Yeah, I assumed as much - pretty straightforward and not too complicated to figure out I guess. It wasn't behaving as I'd liked, though, and I just realized (once I actually started printing out the CGPoint values) that the touch and the body were on different scales...

Here goes another noob question for you: the body starts at the lower left corner with a point of (0,0) and operates on a scale whereby the upper right corner is (32,24) - obviously in landscape orientation with a PTM_RATIO conversion. The touches, on the other hand, operate on a flipped scale - the top left is (0,0) and the bottom right is (32,24). What's the best way to get them both on the right scale? Also, I'm sort of hazy in general on when to apply (either multiply or divide) the PTM-RATIO...
therealcos
Posts: 16
Joined: Tue Aug 20, 2013 7:30 am

Re: Applying an impulse to a body in the direction of a touc

Post by therealcos »

...aaaaand I just found the convertToGL function in CCDirector. Thanks for all the help!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Applying an impulse to a body in the direction of a touc

Post by iforce2d »

You could 'flip' the Y coordinate by subtracting it from the overall height of the dimensions you're working with, in this case 24.

float flippedY = 24 - y;

For floating point coordinates that should be fine.

For integer coordinates (eg. cells in a discrete grid) you would probably want to subtract 1 from the result of that, for example if you flip the coordinate 0, you will get 24 - 0 which is 24, which is off the screen (because the last coordinate at the top of the screen is actually 23 if coordinates are counted from zero). So it might be better to use:

int flippedY = 24 - y - 1;
Post Reply