1
votes

I need to know how to use box2d for projectile motion.

initially, projectileTime=0; then i call the following function for projectile motion. It works pretty good. But i want to achieve the same thing using box2d. As far as i know Box2d works only with force, it does not encourage placing object directly, So how to use Box2d for projectile movement??

-(void)projectilelaunched:(ccTime)dt 
{
    projectileTime+=(5*dt);

    double vh=v*cos(theta);

    x=vh*projectileTime;

    double y = x*tan(theta)- 10*((x/vh)*(x/vh))/2;

    projectile.position=ccp(projectilePositionBeforeLaunched.x + x,projectilePositionBeforeLaunched.y+y);

} 
3

3 Answers

2
votes

With box2D, you'd only need to set its initial position and initial velocity (via applyForce). Box2d will take care of the rest, applying gravity, stopping when hitting other objects etc.

1
votes

Actually, you can directly set up position of the body by calling:

body->SetTransform(vector,angle)
1
votes
b2Vec2 vector = self.speed * b2Vec2(cos(angle), sin(angle));

self.projectileBody->SetLinearVelocity(vector);