Predicting Projectory Path in Box2D

When i was searching thru the internet i wasn’t able to find a perfect tutorial that gives a solution. After googling around and watching lot of tutorials. Im writing this which i found will be useful for many others. And it works perfectly fine as long as you know the object velocity and its start position.

Vec2 startPosition = getBallPosition(); //object start position

Vec2 ballVelocity = //ur object start velocity

ballVelocity.x *= PTM_RATIO;   //converting meter to pixel

float gravityInPixels = GRAVITY*PTM_RATIO;

int granularity = 20;

for (int index = 0; index < granularity; index++)


Sprite * path = Sprite::create("aimline.png");

path->setAnchorPoint(Vec2(0.0, 0.0));

if (_aimingLine == nullptr)


_aimingLine = Sprite::create();




path->setScale(5.0/path->getBoundingBox().size.width, 5.0/path-      >getBoundingBox().size.height);

ballVelocity.y += gravityInPixels / granularity;

startPosition.x += ballVelocity.x / granularity;

startPosition.y += ballVelocity.y / granularity;




Leave a Reply

Your email address will not be published. Required fields are marked *