Topdown rotation and movement

Hello, I’m very new to Game Programming.

Okay I’m trying to make a top down game. But I’m failing at the basics, and I checked all over the web and in JG. But I couldn’t solve my problem.

How do you make the sprite move in the direction its facing?

I can rotate the sprite, but how do I calculate the x,y values so that it moves in the direction its facing.

To give you an idea what I mean: GTA 2
The player can rotate and when you hit W oder “UP” he walks in the direction he is facing.

Hi, in the interest of saving you a couple of extra questions which might arise when the solution is given, here’s a link to the same problem I’ve had and was resolved with the help of the community! :slight_smile:

http://www.java-gaming.org/index.php/topic,23817.0.html

Good luck, and if you have any other questions, I’ll do my best (as I’m sure the community will) to try to explain whatever it is you might need answering too.

Thanks :slight_smile:

I had already bookmarked that thread for later on^^

But you obviously already got your car to drive around, how so?
For now I just want a player at a constant speed to run around, so bare me with friction, acceleration and such. :slight_smile:

You should have 1 double variable representing speed and 1 double variable representing the angle the character is facing.
The rest is sin-ing and cos-ing your way to the next x and y.

Unrelated: Hey deadly, its been a while ;D How’s that car game going?

so?

e.g.

x += Math.sin(angle) * Speed;
y += Math.cos(angle) * Speed;

Is that right?

Maybe I should have payed more attention in math.
Could you by chance give a simple snipplet? That way I can analyse the code and try to understand it.
All I need is the calculation.

Yep, that’s good. Just remember that ‘angle’ is in radians. And if it doesn’t quite work (the player moves and turns alright - but always at an angle to where they’re facing) then try

double movementAngle = angle + ...;
x += Math.sin(movementAngle) * Speed;
y += Math.cos(movementAngle) * Speed;

with different versions of the movement angle:

double movementAngle = angle + 0.5*Math.PI; // 90 degrees
double movementAngle = angle + Math.PI; // 180 degrees
double movementAngle = angle + 1.5*Math.PI; // 270 degrees

Maybe it’s just me but the ‘drawing’ angle and the ‘movement’ angle never seem to line up first time. :wink:
Simon

Thanks alot :smiley:

That worked great.

One more question…why is there a delay in keyPressed() when rotating? The method fires once imidiatly, then there is a delay. and then it fires again, after that its smooth.

This only counts for the rotation. Moving forward/blackwards is smooth.

That’s the key auto-repeat. Open up Notepad (or whatever) and hold down a key. You’ll see the same effect.

To get around it, implement both keyPressed() and keyReleased() to set and clear flags for the keys you are interested in. Don’t assume that keyPressed() means that the key is being held down - it doesn’t work like that.

If I recall, this also came up in the thread that deadly72 linked to.

Simon

Thanks alot :smiley:

works great now.