Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Topdown rotation and movement  (Read 2225 times) 0 Members and 1 Guest are viewing this topic.
Zushii

Senior Newbie

 « Posted 2011-03-24 20:51:53 »

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.

Junior Devvie

 « Reply #1 - Posted 2011-03-24 21:11:29 »

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!

http://www.java-gaming.org/topics/car-rotation-problem-in-top-down/23817/view.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.
Zushii

Senior Newbie

 « Reply #2 - Posted 2011-03-24 21:21:05 »

Thanks

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.
ra4king

JGO Kernel

Medals: 374
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #3 - Posted 2011-03-25 03:04:25 »

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 How's that car game going?

Zushii

Senior Newbie

 « Reply #4 - Posted 2011-03-25 07:42:17 »

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.

dishmoth
 « Reply #5 - Posted 2011-03-25 09:30:02 »

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

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
 1  2  3 `double movementAngle = angle + ...;x += Math.sin(movementAngle) * Speed;y += Math.cos(movementAngle) * Speed;`

with different versions of the movement angle:
 1  2  3 `double movementAngle = angle + 0.5*Math.PI; // 90 degreesdouble movementAngle = angle + Math.PI; // 180 degreesdouble 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.
Simon

Zushii

Senior Newbie

 « Reply #6 - Posted 2011-03-25 14:10:11 »

Thanks alot

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.
dishmoth
 « Reply #7 - Posted 2011-03-25 16:43:35 »

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.

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.

Simon

Zushii

Senior Newbie

 « Reply #8 - Posted 2011-03-25 20:18:25 »

Thanks alot

works great now.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 BurntPizza (30 views) 2015-04-23 03:42:11 theagentd (35 views) 2015-04-22 16:23:07 Riven (49 views) 2015-04-16 10:48:47 Duke0200 (59 views) 2015-04-16 01:59:01 Fairy Tailz (42 views) 2015-04-14 20:13:12 Riven (44 views) 2015-04-12 21:36:37 bus hotdog (60 views) 2015-04-10 02:39:32 CopyableCougar4 (66 views) 2015-04-10 00:51:04 BurntPizza (70 views) 2015-04-06 22:06:58 ags1 (69 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 17x wessles 15x kingroka123 11x alwex 11x 65K 11x kevglass 8x Rayvolution 7x Roquen 7x Riven 7x chrislo27 7x Hanksha 7x Olo 7x Ecumene 7x ra4king 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org