Reputation: 27394
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());
Upvotes: 5
Views: 7821
Reputation: 8906
You can try like this as IFORCE2D suggested
float mangnitude = anything;
b2Vec2 forceDirection = body->GetWorldVector( b2Vec2(0,1) );
forceDirection = magnitude * forceDirection;
body->ApplyLinearImpulse(forceDirection, body->GetPosition(), true);
Upvotes: 0
Reputation: 8272
The easiest way is to look at the direction the object is 'facing' when you define the body, and use GetWorldVector to see how it has changed. For example if it's facing directly upwards when you create the body, this would be the direction (0,1). Then you can use GetWorldVector at any time to get the current direction of that vector in world coordinates to apply the force:
b2Vec2 forceDirection = body->GetWorldVector( b2Vec2(0,1) );
Upvotes: 4
Reputation: 13234
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());
Upvotes: 8