# How can I move a sprite in the direction it is facing?

I'm using Java/Slick 2D. I'm trying to use the mouse to rotate the sprite and the arrow keys to move the sprite. I can get the sprite to rotate no problem, but I cannot get it to move in the direction it is supposed to. When I hit "forwards", the sprite doesn't necessarily move towards the mouse. Actually, it will only move towards the left of the screen really. I'm sure there has to be some standard code for this since many games use this style of motion. Can anyone help me out with what the trig is supposed to be? Thanks

EDIT: Here is the rotation code (which does something else weird: https://stackoverflow.com/questions/12610320/why-is-my-image-rotating-off-center)

int mX = Mouse.getX();
int mY = HEIGHT - Mouse.getY();
int pX = sprite.x;
int pY = sprite.y;
int tempY, tempX;
double mAng, pAng = sprite.angle;
double angRotate=0;

if(mX!=pX){
mAng = Math.toDegrees(Math.atan2(mY - pY, mX - pX));
if(mAng==0 && mX<=pX)
mAng=180;
}
else{
if(mY>pY)
mAng=90;
else
mAng=270;
}

sprite.angle = mAng;
sprite.image.setRotation((float) mAng);


And the movement code. I can only move towards the left of the screen...

double ang = sprite.angle;
Input input = gc.getInput();

if(input.isKeyDown(sprite.up)){
sprite.x += Math.cos(ang)*sprite.moveSpeed;
sprite.y += Math.sin(ang)*sprite.moveSpeed;
}if (input.isKeyDown(sprite.down)){
sprite.x += -1*Math.cos(ang*Math.PI/180)*sprite.moveSpeed;
sprite.y += -1*Math.sin(ang*Math.PI/180)*sprite.moveSpeed;
}if (input.isKeyDown(sprite.left)){
sprite.x -= Math.cos(ang*Math.PI/180)*sprite.moveSpeed;
sprite.y += Math.sin(ang*Math.PI/180)*sprite.moveSpeed;
}if (input.isKeyDown(sprite.right)){
sprite.x += Math.cos(ang*Math.PI/180)*sprite.moveSpeed;
sprite.y -= Math.sin(ang*Math.PI/180)*sprite.moveSpeed;
}

• What's the point of the code you've added? Is this a new problem? If it's a new problem, please open a new question. Please only update this question with information related to moving the sprite in the direction it's facing. Commented Sep 28, 2012 at 18:15
• No it's the code I used that caused the problem. See the comments below. Someone asked for it. Commented Sep 28, 2012 at 19:21
• I don't see where you calculate sprite.movespeed? Commented Sep 28, 2012 at 20:00
• movespeed is a constant, in this case set at .3 Commented Sep 29, 2012 at 22:33

You'll want to get a vector based on your current velocity and heading. Then use that vector to increment your position.

//first get the direction the entity is pointed
if (direction.length() > 0) {
direction = direction.normalise();
}
//Then scale it by the current speed to get the velocity
velocity.x = direction.x * speed;
velocity.y = direction.y * speed;


So now you know your velocity based on your rotation. You can the update your position with that information.

//Update the position based on our current speed
//This is basic s = vt physics
position.x += velocity.x * timeElapsed;
position.y += velocity.y * timeElapsed;

• For a (sloppy) class that contains all this, you can see a Ludum Dare 21 entry I put together here: bitbucket.org/byte56/ld21/src/af3dfc2c4c48/src/Byte56_LD21/… Commented Sep 24, 2012 at 21:18
• Thank you. After a few modifications, this did work. I have another question for you though if you happen to read this: getting the sprite to move backwards is just -= of what you have here. How do I make it move left or right though? Commented Oct 2, 2012 at 16:31
• @rphello101 Good question, you should ask a new question. More people will benefit and you may get a better answer than the one I have. Commented Oct 2, 2012 at 16:45
• What is direction an instance of here? I tried implementing the above code using just float, but it doesn't quite work. I think I need to add that if statement, but I don't know what it does. Commented Jul 23, 2020 at 23:17

You get the mouse position

mouseX = ...
mouseY = ...


You get the sprite position

spriteX = ...
spriteY = ...


You find the angle

angle = Math.atan2(mouseY - spriteY, mouseX - spriteX);


moveX = Math.cos(angle) * speed * time;