Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
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 617 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 Wizard


Medals: 64
Projects: 8


Muahahahahahaha...


« 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  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (94 views)
2013-05-17 21:29:12

alaslipknot (102 views)
2013-05-16 21:24:48

gouessej (132 views)
2013-05-16 00:53:38

gouessej (127 views)
2013-05-16 00:17:58

theagentd (138 views)
2013-05-15 15:01:13

theagentd (126 views)
2013-05-15 15:00:54

StreetDoggy (167 views)
2013-05-14 15:56:26

kutucuk (189 views)
2013-05-12 17:10:36

kutucuk (192 views)
2013-05-12 15:36:09

UnluckyDevil (199 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.136 seconds with 20 queries.