# [Box2D] Applying force to body, dependant on mouse distance (move to mouse)

Printable View

• 11-11-2009, 01:21 PM
nolen
[Box2D] Applying force to body, dependant on mouse distance (move to mouse)
This is crossposted from the Math and Physics forum as well. I figured it would get more exposure in the Games forum.

--

I'm trying to create a setup where you have a player that is controlled by clicking where you want them to move.

What I'd like to implement is a scenario where you click the mouse, and the player jumps toward the location that was just clicked. This in itself isn't terribly difficult, however I would also like to ensure that if I click above another body (a wall, for example), that the player will perform a jump that has him land on said body.

The image below helps explain a bit better, I hope:

http://board.flashkit.com/board/

The arrows B and C can both be accomplished by doing something like this:

Code:

`hero.ApplyImpulse(new b2Vec2((mx - hx)*hero.GetMass(), (my - hy)*hero.GetMass()), hero.GetPosition());`
Where mx, my are the mouse x/y coordinates and hx, hy are the hero's coordinates. This moves the hero toward the point clicked, but does not ensure that it is his stopping place. I want to be able to click at point B and have the hero jump/move toward that position but stop once he arrives. If I click at point C, I want the hero to jump no higher than the clicked point.

And, my biggest hurdle, I want to click point A and have the hero apply a high enough vertical impulse to be able to land on the wall.

What types of calculations/tracking should I create to make this happen? I don't need code written out for me, but really just the method to this madness.