Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I wonder how I should increase and decrease the value of xPos and yPos to be able to move a sprite object diagonal and in different angles?

canvas.drawBitmap(image, xPos - (image.getWidth() / 2), yPos - (image.getHeight() / 2), null);
-1, Really trivial to work out, lack of effort. –  Casper Beyer Mar 22 '13 at 14:45

2 Answers 2

up vote 3 down vote accepted

Casper gave a good answer, but you can also use sin and cos to move your sprite at a certain angle:

sprite.x += sin(angle) * amount;
sprite.y += cos(angle) * amount;

Of course, depending on what Sine and Cosine functions you're using, you may have to enter the angle in radians/degrees. Seeing as you're using Java, java.lang.Math comes with all the functions you'll need.

For more about sin and cos, look at this.

I personally would discourage using sin and cos and just explain a bit linear algebra using vectors. But whatevs works for the person. –  Sidar Mar 22 '13 at 16:20
@Sidar Vectors are useful in some cases, yes, but if you want to work with angles you're gonna have to use some trigonometry at some point. –  Aaron Mar 23 '13 at 1:00

How about just working it out?

Assuming a coordinate system where 0, 0 is top-left. Defining our object's coordinates as being 0, 0 (x, y).

object.x += 50;
object.y += 50;

would move the object 50 pixels diagonally towards the bottom right corner.

What's even the point of answering his question? I know it's easy to do (and a trivial way of getting +15 rep), but IMHO he should have to put in some work himself. Still, good answer to a bad question. –  Polar Mar 22 '13 at 14:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.