开发者

Projectile movement using Box2d.

开发者 https://www.devze.com 2023-04-03 12:58 出处:网络
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.

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. 开发者_如何学CBut 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);

} 


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.


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

body->SetTransform(vector,angle)


b2Vec2 vector = self.speed * b2Vec2(cos(angle), sin(angle));

self.projectileBody->SetLinearVelocity(vector);
0

精彩评论

暂无评论...
验证码 换一张
取 消