RSS

Tag Archives: iterator

Path/Trajectory prediction – What path will the object take

Path/Trajectory prediction – What path will the object take

The question this post will address is how do i tell what path would my projectile take?

Most games these days come with a visual indicator to help user assist in targetting something using the projectile. Angry birds ‘Space’ and peggle being a famous example. Thus using the basic projectile information, we can accomplish this requirement. This approach is much better than creating a parallel world to run the simulations only as it requires much less processing time and is much conviniently setup. However, there is a catch. The time step used for the world has to be the same throughout the simulation for it to work.

The formula used is pretty simple:

postion (at nth time step) =
position (zeroth step) +
n * stepVelocity (velocity in this step) +
(n*n + n) * stepGravity (gravity in this step) * 0.5

So here is the function that does it for me:

//calculates the point where the object will be after nth iterations for a given start point and start velocity
-(b2Vec2) getTrajectoryPoint:(b2Vec2) startingPosition andStartVelocity:(b2Vec2) startingVelocity andSteps: (float)n
{

//velocity and gravity are given per second but we want time step values here
// seconds per time step (at 60fps)
float t = 1 / 60.0f;

// m/s
b2Vec2 stepVelocity = t * startingVelocity;

// m/s/s
b2Vec2 stepGravity = t * t * world->GetGravity();

return startingPosition + n * stepVelocity + 0.5f * (n*n+n) * stepGravity;
}

Here is a link to the video of how it works

This slideshow requires JavaScript.

Link to the well documented source code

 
7 Comments

Posted by on April 8, 2012 in Box2d, cocos2d, Objective C, Xcode

 

Tags: , , , , , , , , , ,

 
Design a site like this with WordPress.com
Get started