Reputation: 5795
I'm making a Lular Lander game. I have a Lander object, for simplicity let's imagine it is simple square.
When I tap the screen I apply the force to said square, by default it is CGVectorMake(0,40)
This makes the square slowly move up.
Now I want to rotate the sprite based on accelerometer, I do it like so:
// update method
CGFloat updatedAccelX = accelerX;
CGFloat updatedAccelY = -accelerY;
CGFloat angle = vectorAngle(CGPointMake(updatedAccelX, updatedAccelY)) - 1.61;
self.lander.zRotation = angle;
// end of update
static inline CGFloat vectorAngle(CGPoint v){
return atan2f(v.y, v.x);
}
Now that my rotation has changed, I want to apply the force relative to the new orientation. So for example, my if my Lander rotation is 90 degrees to the right I want to apply (40,0) impulse.
How do I calculate the new vector of force to apply to the physics body?
Adding Box2d as tag since underneath it is Box2d physics.
Upvotes: 1
Views: 539
Reputation: 20274
Considering that you have the rotation
float rotation = lander.zRotation;
You can calculate the vector for the lander as follows:
rotation += M_PI_2;
float intensity = 40.0;
CGVector newVec = CGVectorMake(intensity * cosf(rotation), intensity * sinf(rotation));
[lander.physicsBody applyForce:newVec];
Upvotes: 2