Reputation: 10554
Despite hours spent in front of box2d I still don't understand how does applyforce and applyimpulse works. I've tried to use some visual to better understand what is going on (by making lines between the body position and the point of application) but the more I try the less it makes sense.
I would like to use applyimpulse to let my box2d body jump in the direction of the point of application (the mouse coordinates, in this attempt). It goes anywhere other than the desired point...
Here are some of my attempts so far:
b.ApplyImpulse(
new b2Vec2(g.mouseX/SCALE,g.mouseY/SCALE),
new b2Vec2(b.GetPosition().x,b.GetPosition().y));
and the opposite
b.ApplyImpulse(
new b2Vec2(b.GetPosition().x,b.GetPosition().y),
new b2Vec2(g.mouseX/SCALE,g.mouseY/SCALE));
both producing unpredictable results which I don't understand at all.
Could please someone explain ApplyImpulse to me as if I were 5 years? How can I decide the intensity of the impulse? And the direction? No one seems to be explaining this on the internet.
Please forgive my poor physics background, I've tried in many different ways and this problem almost getting me stuck anytime I work with Box2d.
Many thanks in advance!
Upvotes: 3
Views: 1990
Reputation: 5472
I think the problem could be the position of the canvas, replace your mouse coordinates with removing canvas.left
and it could work
Upvotes: 0