Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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  
  Tile Collision Detection  (Read 286 times)
0 Members and 1 Guest are viewing this topic.
Offline _Scyth_

Junior Member





« Posted 2013-09-08 14:41:14 »

I have a tile map:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public int[][] level1 = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                     {1,1,1,1,1,1,1,1,1,1,1,1,1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                     {2,2,2,2,2,2,2,2,2,2,2,2,2,6,1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,1,1,1,1,1,1},
                     {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7,2,2,2,2,2,2,2},
                     {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                     {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                     {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                     {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                     {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3}};


Now the 1's are supposed to be walkable tiles, tiles that the player will not fall through.
I have a method that iterates through the tiles, but how can I detect collision for the tiles that are 1. The player can freely move, so i dont want its movement to be tile based.

Any ideas, i tried creating a class called CollisionBlocks and set up an intersects method:

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  
public boolean intersects(Player player)
   {
      boolean intersect = false;
     
      int areaX = (width + position.x);
      int areaY = (height + position.y);
     
      if( player.position.x > position.x && player.position.x < areaX)
      {
         if( player.position.y > position.y && player.position.y < areaY)
         {
            intersect = true;
         }
         else
         {
            intersect = false;
         }
      }
      else
      {
         intersect = false;
      }
     
      return intersect;
   }


and i am trying to test this by using:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
if( falling )
      {
         if( (position.y + height) > 240)
         {
            position.y = 240 - height;
            falling = false;
         }
         else
         {
            for( int i = 0; i < 37; i++)
            {
               if( cBlocks[i].intersects(this) )
               {
                  falling = false;
               }
            }
            position.y += GRAVITY;                  
         }
      }


basically it iterates through an array of collision blocks that are set up by this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public CollisionBlocks[] iterateTiles()
   {
      CollisionBlocks[] cb = new CollisionBlocks[(noCollideable + 1)];
      int nextBlock = 0;
      for(int y = 0; y < level1.level1.length; y++)
      {
         for(int x = 0; x < level1.level1[y].length; x++)
         {
            int tID = level1.level1[y][x];
           
            if( tID == 1)
            {
               cb[nextBlock] = new CollisionBlocks(x * 16, y * 16, 16, 16);
               nextBlock++;
               
            }
         }
      }
      return cb;
   }


but it doesnt seem to work for me.

Help would be appreciated. I am using Blackberry JDE library so i dont have access to libgdx or lwjgl
Offline relminator
« Reply #1 - Posted 2013-09-08 15:04:02 »

This might be useful...

www.java-gaming.org/topics/basic-2d-tile-collision/29657/view.html
Offline _Scyth_

Junior Member





« Reply #2 - Posted 2013-09-08 15:19:37 »

I've tried reading it, but some comments might help me understand it better, im not too sure what some of the variables stand for.
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.

Pippogeek (40 views)
2014-09-24 16:13:29

Pippogeek (31 views)
2014-09-24 16:12:22

Pippogeek (21 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (29 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (43 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
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!