Sign in to follow this  

Moving in sprites direction help

Recommended Posts

Simple put, I want to move my sprite based on it rotated angle, like in Asteroids where if you rotate to the left and apply impulse you will move in the direction you are facing. In my program I use ApplyImpulse which takes two vectors the first is the impulse and the second is the point, basically impulse would be velocity of the object and point would be the position of the object. Here is an example of adding impulse to my object
//Moves the object 0 on the X coordinate and -1 on the Y coordinate
object.ApplyImpulse(Vector2(0, -1), Vector2(position.X, position.Y));

I can get the angle of my object by calling object.angle, which is obtained by using the GetAngle() in my physics library. Anyone have any ideas on how to apply this directional moving by rotation? Or any suggestions here?

Share this post

Link to post
Share on other sites
First of all, it will get confusing if you have an implulse vector and an angle, because you clearly want to use the angle as the direction, but having an implulse vector implies that the impulse vector is the direction, so we'll use the angle. The angle is the direction that you want to move (which we will convert to a direction vector), so just to make things simpler, have the other arguments as the current position and the distance you want to move.

So, take the angle, convert to radians (if not in radians already) and use sin(angle) to calculate the 'y' component of the direction vector and use cos(angle) to calculate the 'x' component of the direction vector. This will result in a direction vector of length 1 (like all direction vectors should be).

Now, just take the direction vector and scale it by the distance you want to move. This will result in a vector that we'll use as an offset. Add it to the position, and you should end up with the final position that you need.

Share this post

Link to post
Share on other sites
Thanks Endar, but I got it figured out before your post. After tickering around and some successful Googling I got it figured.

Here is how I did it successfully

//Set the ships velocity, thrust, rotation thrust
myShip.velocity.x = sin(myShip.body->GetAngle());
myShip.velocity.y = -cos(myShip.body->GetAngle());
myShip.rotThrust = .5f;
myShip.thrust = 1 / myWorld.scale;

//Now to apply the thrust to my ship
myShip.body->ApplyImpulse((xVelocity * thrust), (yVelocity * thrust), myObject.body->GetWorldCenter().x, myObject.body->GetWorldCenter().y);

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this