Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

box2d Apply force in a particular direction

I want to apply a force to my object in the direction that it is currently facing, here is my code so far but it is throwing errors when I try do force * t, what am I doing wrong?

        b2Transform t;
        t.Set(b2Vec2(0, 0), spaceCraft->GetAngle());
        b2Vec2 force = b2Vec2(0, 2.5f);
        spaceCraft->ApplyForce(force * t, spaceCraft->GetPosition());
like image 728
Chris Avatar asked Mar 06 '12 22:03

Chris


1 Answers

I can't try right now but something like that should do it:

float magnitude=2.5f;
b2Vec2 force = b2Vec2(cos(spaceCraft->GetAngle()) * magnitude , sin(spaceCraft->GetAngle()) * magnitude);
spaceCraft->ApplyForce(force, spaceCraft->GetPosition());
like image 169
erkanyildiz Avatar answered Sep 24 '22 09:09

erkanyildiz