Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Accuracy vs flow, 2d top-down diagonal movement.  (Read 725 times)
0 Members and 1 Guest are viewing this topic.
Offline Rayvolution

JGO Kernel


Medals: 190
Projects: 2
Exp: 1 year


Resident Crazyman


« Posted 2014-03-22 20:38:21 »

How do you guys handle diagonal movement in 2d games, where the movement is not tile based?

Technically speaking, currently in my game when you walk diagonally in any direction you are traveling faster than if you just move straight up/down/left/right (Because you are moving X and Y += Speed*delta basically). The result is, is takes the same amount of time to travel from one corner of a tile to the opposite corner as it does to travel from one side to the opposite side.

Well, for game flow, this still appears like fluid, natural movement. The character doesnt look like hes moving faster when he travels diagonally, but he is technically covering more ground.

There are many solutions to this, most of them are basically various ways of dividing the movement speed by the sqrt2, this results in much more accurate travel distance from point A to B. But, from a player's perspective the character feels like he's slowing down substantially when you move diagonally, it just doesn't feel natural anymore.

From playing a few old top down games that allow free flowing diagonal movement, It looks to me like they don't care about the discrepancy either unless they have a magical way of hiding it, their diagonal movement appears like mine. It's just X *and* Y += Speed*delta.

I was hoping to get some more input on the situation, because I can't find a solid answer anywhere. I've found plenty of people asking for solutions on how to make diagonal movement accurate, but no one asking if it should be done in the first place.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline orange451

Senior Member


Medals: 7
Projects: 1
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #1 - Posted 2014-03-22 20:41:47 »

I use vectors for movement; This makes it easy because you can normalize a vector which eliminates the whole problem of faster diagonal movement.

If you have a max speed, say 6...
1  
2  
3  
if (velocity.getMagnitude() > maxSpeed) {
   velocity.normalize().multiply(maxSpeed);
}

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline Drenius
« Reply #2 - Posted 2014-03-22 20:42:40 »

1  
2  
x += Math.sin(Math.toRadians(angleToMove))*wayToMove;
y += Math.cos(Math.toRadians(angleToMove))*wayToMove;

Or quite similar...

Edit: orange451 is mainly doing the same.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza
« Reply #3 - Posted 2014-03-22 20:50:33 »

orange and Drenius are correct, let me provide some of the theory behind it.

You want to separate movement direction from movement speed.

Direction is a vector, speed is a scalar; specifically the movement of an entity is described by the scalar product of direction and speed.
The direction vector should (needs to) be a unit vector, so that the length of the movement vector (the distance traveled) is equal to speed.
To create a unit vector in the desired direction, you normalize any vector in that direction; the resultant vector will have a length of 1.
How you normalize is dependent on how you are representing your directions and locations, seen in orange and Drenius's responses.
Offline Drenius
« Reply #4 - Posted 2014-03-22 20:55:41 »

sin(angle) = adjacent/hypotenuse = vectorWidth/distance;
cos(angle) = opposed/hypotenuse = vectorHeight/distance;

sin(angle) * distance = vectorWidth = distance to move on x axis
cos(angle) * distance = vectorHeight = distance to move on y axis
Offline Rayvolution

JGO Kernel


Medals: 190
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #5 - Posted 2014-03-22 21:00:55 »

Wouldnt using vectors to move still result in the odd appearance that the character is moving slower on the diagonals?

I might just not be understanding how vectors work.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline Drenius
« Reply #6 - Posted 2014-03-22 21:02:14 »

Vectors mean that it is moving exactly the same distance, just balanced to both (or more) axes.
So no.
Offline BurntPizza
« Reply #7 - Posted 2014-03-22 21:13:25 »

Wouldnt using vectors to move still result in the odd appearance that the character is moving slower on the diagonals?

Nope, consider the fact that without normalization, the character appears to be moving faster on the diagonals. It's because he is.
With normalization, the character moves at the same speed in any direction, and it appears as such. Give it a try!

Little diagram of Euclidean distance, showing the separation of distance into the two dimensional components as Drenius showed, but in reverse (coords to dist, as opposed to dist and angle to coords)
Offline Rayvolution

JGO Kernel


Medals: 190
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #8 - Posted 2014-03-22 21:18:18 »

Ah, interesting.

I guess I'll start work on converting my crappy character movement to vectors sooners than I expected. I had full intention on doing so, but for now I just setup a bunch of basic +x +y movements, I was going to use overhaul it with something more practical when I started adding in entities and working with AI/Pathfinding.

But, I guess it's about time I just go ahead and do it now. Tongue

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (39 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (36 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!