Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Zelda/Pokemon esque Grid Walking  (Read 4962 times)
0 Members and 2 Guests are viewing this topic.
Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Posted 2012-06-02 17:51:18 »

Like many before me, I'm making a game in which the game world is stored in a 2D array
The player is able to walk around with the arrow keys, and when the player moves in a direction the game checks to make sure the space the player would like to move is unoccupied, and then moves the player there.

However, I would like movement that is more smooth. Instead of the player simply jumping to the adjacent block, how could I make it such that the place walks to the next block over a period of time?

How have people done this in the past? Thank you.
Offline davedes
« Reply #1 - Posted 2012-06-02 18:30:43 »

Which library are you using? What have you got so far?

If you don't have a game loop, you'll want to do some reading.
http://entropyinteractive.com/2011/02/game-engine-design-the-game-loop/
http://www.java-gaming.org/topics/game-loops/24220/view
http://gafferongames.com/game-physics/fix-your-timestep/

You'll also need to load/render your tiles somehow.
http://www.java-gaming.org/topics/basic-tile-engine/25287/view
http://www.java-gaming.org/topics/tilemap-question/26210/msg/228878/view.html#msg228878

"Smooth movement" is just a matter of moving your character sprite on a per-pixel basis rather than per-tile. Collision detection could be as simple as adding a "isTileBlocked(x, y)" method.

Slick2D has a very basic "scroller" test which does what you're looking for: tiled map loading/rendering, smooth player movement, wall collision, centering the camera on the player, etc.
https://bitbucket.org/kevglass/slick/src/tip/trunk/Slick/examples/org/newdawn/slick/examples/scroller/Scroller.java

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2012-06-03 01:13:57 »

Here's what I have.

I too am working on a tile based game, and wanted smooth walking. To get tile based movement with animated walking between tiles, I made it so that when you press a direction, say left, it sets your xDir value to -1, which represents left. It also sets moveDistance to 16, since my tiles are 16x16. Now, on every pass, it will subtract one from moveDistance, and, depending on the xDir and yDir variables, it will change your x or y coordinate.

Checking if done moving to a new tile
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
     if (moveDistance == 0) {
         newDir = 4;
         xDir = 0;
         yDir = 0;
         
         int tx = 0, ty = 0;
         
         if (listener.turn.down) {
            if (listener.up.down) dir = UP;
            else if (listener.down.down) dir = DOWN;
            else if (listener.left.down) dir = LEFT;
            else if (listener.right.down) dir = RIGHT;
         } else {
            if (listener.up.down) ty = -1;
            else if (listener.down.down) ty = 1;
            else if (listener.left.down) tx = -1;
            else if (listener.right.down) tx = 1;
         }
         
         if (Tile.checkEmpty(x / 16 + tx, y / 16 + ty, true)) {
            Tile.tilesArray[x / 16][y / 16] = Tile.EMPTY;
            Tile.tilesArray[x / 16 + tx][y / 16 + ty] = Tile.PLAYER;
           
            cx = x / 16 + tx;
            cy = y / 16 + ty;
           
            if (ty == -1) newDir = UP;
            if (ty == 1) newDir = DOWN;
            if (tx == -1) newDir = LEFT;
            if (tx == 1) newDir = RIGHT;
         } else {
            if (ty == -1) dir = UP;
            else if (ty == 1) dir = DOWN;
            else if (tx == -1) dir = LEFT;
            else if (tx == 1) dir = RIGHT;
         }
         
         if (newDir != 4) {
            dir = newDir;
            moveDistance = 16;
         }
      }


Moving in the Y direction
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
     if (yDir != 0) {
         y += yDir;
         
         if (y > Game.HEIGHT / 2 - 9 && y < Level.levelHeight - (Game.HEIGHT / 2) - 7) {
            super.updateOffs(0, yDir);
            if ((y - yoffs) < centerY) {
               yoffs--;
            } else if ((y - yoffs) > centerY) {
               yoffs++;
            }
         }
      } else {
         super.updateOffs(0, yDir);
      }


Moving in the X direction
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
     if (xDir != 0) {
         x += xDir;
           
         if (x > Game.WIDTH / 2 - 9 && x < Level.levelWidth - (Game.WIDTH / 2) - 7) {
            super.updateOffs(xDir, 0);
            if ((x - xoffs) < centerX) {
               xoffs--;
            } else if ((x - xoffs) > centerX) {
               xoffs++;
            }
         }
      } else {
         super.updateOffs(xDir, 0);
      }


Hope this helps.
-Nate

Check out my website!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-06-03 02:30:52 »

For smooth move, do interpolation one until the player reach a certain coordinate. There'll be some pixels off depends on your FPS, just force it down.
1  
2  
3  
4  
5  
x += dx * delta;
y += dy * delta;
...
if (x > targetX) x = targetX;
if (y > targetY) y = targetY;


Your name is Nate, too?

Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2012-06-03 02:32:31 »

Do you want to move (aka animate) to the tiles, or to jump to them?
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #5 - Posted 2012-06-03 02:34:00 »

For smooth move, do interpolation one until the player reach a certain coordinate. There'll be some pixels off depends on your FPS, just force it down.
1  
2  
3  
4  
5  
x += dx * delta;
y += dy * delta;
...
if (x > targetX) x = targetX;
if (y > targetY) y = targetY;


Your name is Nate, too?

I couldn't remember that it's called interpolation, haha. And yep! It's short for Nathaniel, but I don't really care for that.

Check out my website!
Offline ReBirth
« Reply #6 - Posted 2012-06-03 02:45:12 »

Yes as far as I recall that's the name. I just know it this year too.

Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Reply #7 - Posted 2012-06-03 10:51:23 »

Thank you, Nate!
I my prexisting code was pretty different from yours, but I solved in a similar way
What I ended up doing is that when the character starts moving, the "tile coordinate" is changed, although the offset from that coordinate is made large enough such that it appears to not have moved yet. Then on subsequent "steps" I decrease the offset, at which point I keep checking if the object should start moving again. It works beautifully.

Thanks for your help!
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #8 - Posted 2012-06-03 13:18:26 »

You are welcome! Just a few months ago I was on here asking questions every day, and it feels nice to be able to answer someone else's Smiley

-Nate

Check out my website!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!