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
Link to the well documented source code
Kotaiba
April 10, 2012 at 6:42 pm
Nice work keep it up
jaydip patel
July 9, 2012 at 9:00 pm
Thanks i get lots of idea and concepts of game developing because i am unknown person about game development….
Thanks a lots and keep it
Muhammed Alee
July 15, 2012 at 1:03 pm
Thanks for appreciating 🙂
zuzoovnNam
November 12, 2012 at 11:50 am
Hi, could you please port it to Cocos2d-x
Muhammed Alee
November 19, 2012 at 9:07 am
Sure, ill make a cocos2dx port from it. Due to the busy schedule at my end, i wont be able to do it immediately. But ill keep you posted.
shunmugam
March 8, 2013 at 2:37 pm
Hi
Thanks for your lots of idea and concepts.
I need one help from u
How to calculate the starting velocity.
i have start location, end location, and gravity.
Kupiakos
December 4, 2013 at 2:34 pm
How would you go about doing this for multiple radial planets, like in Angry Birds Space? I’m struggling to find a way to do it without simulating for each dot.