# How to move a b2Body explosively to a new Location

I am building an Game based on Box2d with the following basic structure:

The Box2d World itself does not have any Gravity and is filled with some bodies (sheres and simple polygons) of more or less the same size. The user can select on of them and if he clicks on the free space, the selected body should be moved onto the "point of click".

The movement should be explosive, what the use of b2Body.ApplyImpulse suggests, but also stop quickly when the point is reached.

This leads me to two problems:

Which method should i use: ApplyImpulse or ApplyForce? How could I calculate the right vector of the impulse, if you would use this method?

How could I correct the body's way if it collides with another?

Greetings Philipp

-
Why do you use a physics-engine? It sounds like you would be better off without it :) – bummzack Aug 9 '12 at 19:07
I use box2d, as it is tagged with – philipp Aug 9 '12 at 19:15
If you can manage to implement a fully blown physics engine in your game, I'd go with it (unless you have specific design goals that speak against it). Those things have amazing emergent gameplay capabilities. – TravisG Aug 9 '12 at 19:50

I'm not sure I understand the term "explosively" correctly, but if you want the move to be immediate and not quite over a frame.. have a look at SetTransform. You can find this in this section under "Position and Velocity".

-
ok, i tried this, but that just "beams" the body directly to the new location, but i want the body to move to the new location. I would like to have the body move to the new location, explosive means fast in this case, so that i "kicks" away everything what is in the way... – philipp Aug 9 '12 at 19:14
You can use atan2 and compute the angle from the body to the mouse click; then you can apply a force in this direction at a very high rate. If it's moving really quickly, make sure you set it as a Bullet (SetBullet) at this enables CCD. – Vaughan Hilts Aug 9 '12 at 19:18
And sorry for the comment spam, but in order for your object you're moving to not be phased (as in their path to be changed from other objects hitting them) you should set the moving object to a kinematic body. The rest should be dynamic, and you can set the moving body back to dynamic when it comes to a rest. Also, you can set Bullet to false as well if you choose. – Vaughan Hilts Aug 9 '12 at 19:19
Thanks, the trick with the kinematic type seems to work! But is it possible to give its movement a little of easing? Setting its linear Velocity produces a quiet unnatural movement? Is there another trick to fix this? – philipp Aug 9 '12 at 19:27
Set the linearDamping on the body. – Vaughan Hilts Aug 9 '12 at 19:46

An explosion consists of an impulse applied directly by the detonation (moving at supersonic speed) and a force that is applied over time by the pressure wave (moving at subsonic speed). If have a high order explosion(C4 etc), also called detonation, then you're mainly going to notice the impulse part. If you have a low order explosion (gunpowder, fuel etc), also called deflagration, then you are going to have pretty much only the later. If you are interested (and just who is NOT interested in explosions??) check out the wikipedia articles on detonation and deflagration That's enough real world physics stuff.

For your game I think you'll do well enough only by applying impulse. I've never used box2D but you should probably do nothing special to the bodies affected by the explosion once you have applied the impulse, just let the physics engine handle that.

EDIT: Or maybe I misunderstood your question entirely, just got carried away when I saw explosive movement :)

-
Vaughan's hint (using a kinematic body) takes the right direction, now the body is moving straight through the field, but with linear velocity. What i would achieve is that the body accelerates and slows down very fast. – philipp Aug 9 '12 at 19:42
As Vaughan suggests you can add linear damping to slow it down. That will not stop it however. I guess you could instead calculate the needed force to stop it in a certain time or a certain travelled distance but that might be a little overkill. I have to agree with bummzack, for what you are trying to accomplish I think using a physics engine might be making things more complicated than they need to be... – Mikael Högström Aug 9 '12 at 19:58
Your are right, it does not stop slowly, it stops immediately, but i need a physics engine to handle the collisions between all other bodies and the one who is moving after a click. I think I have to calculate some easing, but that is now much simpler because i do not have to care about the correction of the way. – philipp Aug 9 '12 at 20:01
perfect answer. Interesting wiki pages! – Amplify91 Sep 12 '12 at 3:19
You are not understanding the question, Amplify. The object was needed to move really fast... not simulate an explosion. – Vaughan Hilts Sep 12 '12 at 18:37