Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  [SOLVED] Collision Issue on TiledMap (Slick2D)  (Read 1053 times)
0 Members and 1 Guest are viewing this topic.
Offline Rocketalypse

Senior Newbie




Bass Slapping Game Dev


« Posted 2012-12-21 07:53:00 »

Hello.
I've tried my best to make this collision work, but I simply can't. I tried every method, even copied the code from internet tutorial, but it simply won't work. It seems as if when the player is in the middle of two tiles, it won't work. Uh, I don't know how to explain it right, so I'll post some screenshots.

It works if the player is in a "full" tile.
Click here to see the screenshot.

But, if a player is kind of in a "half" tile, it doesn't.
Click here to see the screenshot.

The code I use to check for the collision:
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  
   public void update(int delta, int direction, Map map){
      speed = 0.2f; // Just for testing purposes. Will remove later.
     switch(direction){
         case 1: // Up
           sprite = up;
            if(!map.isBlocked(x, y - delta * speed)){
               y -= delta * speed;
            }
            break;
         case 2: // Down
           sprite = down;
            if(!map.isBlocked(x, y + Constants.TILE_SIZE + delta * speed)){
               y += delta * speed;
            }
            break;
         case 3: // Left
           sprite = left;
            if(!map.isBlocked(x - delta * speed, y)){
               x -= delta * speed;
            }
            break;
         case 4: // Right
           sprite = right;
            if(!map.isBlocked(x + Constants.TILE_SIZE + delta * speed, y)){
               x += delta * speed;
            }
            break;
         default:
            System.err.println("Invalid direction.");
            break;
      }
   }


My isBlocked(float, float) method (in Map class) is pretty simple:
1  
2  
3  
4  
5  
   public boolean isBlocked(float x, float y){
      int xTile = (int) (x / 32);
      int yTile = (int) (y / 32);
      return blocked[xTile][yTile];
   }


NOTE: In my map class I set up stuff with TiledMap, just so you know.
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2012-12-21 09:25:41 »

You are doing the collision checking wrong.

You are checking a + instead of a square.

What you need to do is to store the movements in a different variable (say, velX and velY), then check te bounds by axis. To check by axis you first check the coords (x, y+velY) then the coords (x+velX, y) or vice versa.

To check the bounds, you check each of the following coordinates:

1  
2  
3  
4  
x
y
x+sizeX
y+sizeY


And if any of them is blocked, don't do the movement for that axis.

Offline Rocketalypse

Senior Newbie




Bass Slapping Game Dev


« Reply #2 - Posted 2012-12-22 15:07:21 »

Actually, my collision checking method was correct, but I used it in a wrong context, for the lack of a term. My new method of doing it is drawing rectangles on the blocked tile spots. Maps gets drawn on top of those rectangles, so they're not visible. It works like a charm!

Here's the code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public boolean collides(float x, float y, Graphics g){
      Rectangle player = new Rectangle(x, y, 32, 32);
      g.draw(player);
      for(int i = 0; i < rects.size(); i++){
         if(player.intersects(rects.get(i))){
            System.out.printf("Collision detected at (%s, %s).\n", x, y);
            return true;
         }
      }
      System.out.printf("No collision has been detected.\n");
      return false;
   }


Thank you for your help, anyway!
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.

Riven (19 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!