Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
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  
  Slick2d smooth movement between tiles  (Read 3684 times)
0 Members and 1 Guest are viewing this topic.
Offline EamoD

Junior Newbie





« Posted 2011-07-10 13:23:36 »

I've just started using Slick2d this week and have so far managed to put together the basics of a simple 2d top down game. I have a sprite moving around the screen and doing basic tile based collision detection. For the game mechanic that I eventually want restricting player/sprite movement to be tile by tile is sufficnet. However as you will see from the code below I dont have smooth movement between the tiles as during my collision detection if the target tile is not blocked I simply move the sprite 32 pixels (the size of a tile). I'm trying to figure out how can I keep my tile based collision detection by smoothly move the sprite from tile to tile, and then eventually animate it. I've some some experiments with multiplying by the delta in the update method but have not made any progress.

I'd be extremely grateful for any suggestions on how I should accomplish this. Source below.

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  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
import org.lwjgl.util.vector.Vector2f;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

public class TestGame extends BasicGame{
   private boolean[][] blocked;
   Vector2f currentPosition = new Vector2f();
   Image characterFront = null;
   Image characterLeft = null;
   Image land = null;
   float x = 100;
   float y = 90;
   float scale = 1;
   TiledMap map = null;
   Player player;
   public TestGame()
   {
      super("Game");

   }

   @Override
   public void init(GameContainer gc)throws SlickException {
      //load played entity which handles resource loading for player sprite  
     player = new Player();
      //load the tiled map
     map = new TiledMap("data/untitled.tmx","/data");          
      // build a collision map based on tile properties in the tile map
     blocked = new boolean[map.getWidth()][map.getHeight()];
      //set starting position
     currentPosition.set(0f, 0f);
      //populate collision map
     for (int xAxis=0;xAxis<map.getWidth(); xAxis++)
      {
         for (int yAxis=0;yAxis<map.getHeight(); yAxis++)
         {                        
            int tileID = map.getTileId(xAxis, yAxis, 1);
            String value = map.getTileProperty(tileID, "blocked", "false");                        
            if (value.equalsIgnoreCase("true"))
            {
               System.out.println("tile id " + tileID + " at x=" + xAxis +" y="
                     + yAxis +" is BLOCKED");
               blocked[xAxis][yAxis] = true;
            }else{

               blocked[xAxis][yAxis] = false;
            }
         }
      }

   }

   @Override
   public void update(GameContainer gc, int delta)
   throws SlickException
   {
      Input input = gc.getInput();
      //check what key has been pressed then check if target tile is registered as blocked in collision map
     
      if(input.isKeyPressed(Input.KEY_A))
      {
         player.setPlayerImage("a");
         if(blocked[(int) Math.abs(currentPosition.getX() - 1f)][(int) Math.abs(currentPosition.getY())] == true){
            System.out.println("blocking movement");          
         }else{          
            x = x - 32;
            currentPosition.setX(currentPosition.getX() - 1);
         }          
      }

      if(input.isKeyPressed(Input.KEY_D))
      {
         player.setPlayerImage("d");
         if(blocked[(int) Math.abs(currentPosition.getX() + 1f)][(int) Math.abs(currentPosition.getY())] == true){            
            System.out.println("blocking movement");
         }else{
            x = x + 32;
            currentPosition.setX(currentPosition.getX() + 1);
         }
      }

      if(input.isKeyPressed(Input.KEY_W))
      {
         player.setPlayerImage("w");
         if(blocked[(int) Math.abs(currentPosition.getX())][(int) Math.abs(currentPosition.getY() - 1f)] == true){            
            System.out.println("blocking movement");                          
         }else{
            y = y - 32;
            currentPosition.setY(currentPosition.getY() - 1);
         }          
      }

      if(input.isKeyPressed(Input.KEY_S))
      {
         player.setPlayerImage("s");
         if(blocked[(int) Math.abs(currentPosition.getX())][(int) Math.abs(currentPosition.getY() + 1f)] == true){            
            System.out.println("blocking movement");                        
         }else{        
            y = y + 32;
            currentPosition.setY(currentPosition.getY() + 1);
         }        
      }
   }

   public void render(GameContainer gc, Graphics g)
   throws SlickException
   {
      map.render(100,100);
      player.getPlayerImage().draw(x, y, scale);

   }

   public static void main(String[] args)
   throws Exception
   {
      AppGameContainer app =
         new AppGameContainer( new TestGame() );
      app.setDisplayMode(800, 600, false);
      app.setVSync(true);
      app.setTargetFrameRate(60);
      app.start();
   }
}
Offline Cero
« Reply #1 - Posted 2011-07-10 15:31:09 »

use setTargetFPS(int fps);

and maybe setVsync(boolean);

Offline EamoD

Junior Newbie





« Reply #2 - Posted 2011-07-10 15:33:44 »

use setTargetFPS(int fps);

and maybe setVsync(boolean);

I already am, its not the issue that I have the logic and its not smooth, I dont know how to move the sprite gradually in the update method rather than moving it 32 pixels at a time.

For example, I want the player to press a key and it takes half a second or so for the sprite to move from tile to tile, rather then just suddenly appearing there as its being moved the full distance in one frame.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline EamoD

Junior Newbie





« Reply #3 - Posted 2011-07-11 00:12:58 »

I've managed to get this working by using the following code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
if(moveInProgress == true){
         if(moveDirection.equalsIgnoreCase("a")){
               if(currentMoveStep <= 31 && currentMoveStep > 0){                  
               x = x-1;
               currentMoveStep--;
            }else if(currentMoveStep == 0){
               moveInProgress = false;
               currentMoveStep = 31;
            }  


Every update I'm incrementing the sprites position by one pixel, once a full tile has been moved a reset a boolean value to stop any further movement and accept another key press. It seems to work well, I just cant tell if there is a better solution to this.
Offline Mads

JGO Ninja


Medals: 24
Projects: 3


One for all!


« Reply #4 - Posted 2011-07-16 21:02:12 »

I usually set a position for the player where the movement should stop.
When the movement is started be sure to have the target position set. Then each loop you can check if the player is standing on the target location, and then stop movement.
I usually use either a bool or an enum to define the state of the player (moving, still) so that way I know what to do and when movement stops I can just set the flag to no-movement.

Offline Kakashi266

Junior Member





« Reply #5 - Posted 2011-07-24 21:02:06 »

I've managed to get this working by using the following code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
if(moveInProgress == true){
         if(moveDirection.equalsIgnoreCase("a")){
               if(currentMoveStep <= 31 && currentMoveStep > 0){                  
               x = x-1;
               currentMoveStep--;
            }else if(currentMoveStep == 0){
               moveInProgress = false;
               currentMoveStep = 31;
            }  


Every update I'm incrementing the sprites position by one pixel, once a full tile has been moved a reset a boolean value to stop any further movement and accept another key press. It seems to work well, I just cant tell if there is a better solution to this.
That's probably what my movement code started out looking like. Over the years it's grown to include checking/setting collision of tiles, changing the sprite based on how far through the step it is, and some other stuff.

Offline GustavXIII

Junior Member


Projects: 1



« Reply #6 - Posted 2011-07-31 21:21:48 »

How can I resize a Image?
The Image is 800x600. I want to resize it to 640x480.
Can Graphics.drawImage already this?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #7 - Posted 2011-07-31 21:55:35 »

How can I resize a Image?
The Image is 800x600. I want to resize it to 640x480.
Can Graphics.drawImage already this?
Check out this sweet method perfect for the job

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.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (217 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!