This demo here shows the path of a particular body when it is set in a projectile motion at a certain angle with some initial velocity. The path shows the motion of the body in the next 3 seconds (it can be increased).

We will use a simple formula for projectile motions which goes like this: S = P + u*t + 0.5*a*t*t. Here S = Distance Travelled, P = Original Position, u = Initial Velocity, t = Time after which position has to be found out, a = Acceleration.


a = world.GetGravity();

I have used the world time step of 1/60 seconds which means everytime update function is called the world steps by 1/60 seconds. Since I have used a frame rate of 60 (fps), each second 60 times update() is called and hence step() is also called 60 times. This means that in 1 second world steps by 60*(1/60) = 1 second. So in update function, I run a for loop 180 times and on each iteration the time 't' in the formula is increased by (1/60). This time 't' is fed to the motion formula and we get the position of the body after each 1/60 seconds. On each iteration we draw a point on the canvas to track its position. After completion of all iterations we can see all the dots, representing the path of the center of the body.

Another thing which has been done here is to detect if the path of the body (within 3 seconds) overlaps with a body. To check that we will use RAYCASTING. The first point will be the previous point (trajectory point calculated in previous iteration) and current point (trajectory point calculated in current iteration). If there is a body present between these two points, we'll calculate the intersection point and stop the iteration using break command.

Here is a demo of TRAJECTORY in Box2dweb.

PS: Please read the's article on Projected Trajectory.

NOTE: If anyone needs a tutorial on this, please leave a comment or Reply.

By administrator at 03:59:08 PM 5 Comment(s)


Thank you for all of these extremely detailed tuts!! I'm building a game for Windows 8, and these are coming in very handy for my learning. Box2DWeb works great for Windows 8 apps!
By evertith on 5 Oct, 2012 at 11:59:37 PM
You're welcome! :)
By administrator on 6 Oct, 2012 at 03:14:48 PM
You don't happen to have a tut on how to manually rotate a body using SetAngle do you?
By evertith on 7 Oct, 2012 at 03:42:49 AM
Nevermind! Your Interactivity article pointed me in the right direction. :)
By evertith on 7 Oct, 2012 at 03:57:37 AM
By administrator on 7 Oct, 2012 at 09:46:51 PM

Add a Comment

Please enter the email address.Invalid format. (Won't be Displayed)
Notify me of followup comments
Enter the displayed Code: captcha