Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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 map collision detection  (Read 1289 times)
0 Members and 1 Guest are viewing this topic.
Offline delmator

Junior Member


Medals: 4
Projects: 3



« Posted 2012-10-29 23:59:52 »

Hey guys!

I know there are A LOT of collision detection topics all around here and the internet, but I am really struggling to get my head around it even though there are so many resources (I'm not a very clever guy).

I think my main issue is that I'm not understanding the logic behind collision detection with tiles. From what I've read so far, it seems as though what I want to do is:
-get the players current position,
-check if the next position will be a solid block,
-move the player if it isn't.

Is this understanding correct?
Sorry about another collision detection topic!

Offline cubemaster21
« Reply #1 - Posted 2012-10-30 00:07:56 »

yeah, you've got the concept there. I see collision detection as just a check before you move the object.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline delmator

Junior Member


Medals: 4
Projects: 3



« Reply #2 - Posted 2012-10-30 00:29:09 »

Ok cool! Smiley thank you.

Just another question regarding the same thing. When it comes to the players 'bounding box' how should i go about setting that up so that it works with the collision detection?
Should I just be testing collisions with the players position +/- his height or width?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cubemaster21
« Reply #3 - Posted 2012-10-30 00:36:26 »

I usually use Rectangles. But that would work if you want.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline philfrei
« Reply #4 - Posted 2012-10-30 00:39:34 »

Depends on the shape of the character, and how you encode the position (e.g., whether you use the top-left corner, or the middle of the graphic, etc).

It is possible to also create a rectangle or shape and store it with the character, and use that for your collision tests. Many shapes have a contains() function.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline delmator

Junior Member


Medals: 4
Projects: 3



« Reply #5 - Posted 2012-10-30 01:04:41 »

Wow! so I can just apply a Rectangle() to my player, and use that for my collisions?
With my previous game all collisions were handled with Rectangle's .intersects() method, but for obvious reasons this wont work with a platformer which requires collisions for specific sides of the player.
So then how would I separate the players left, right, top and bottom with a Rectangle?
This is so magical.

thanks for the help! It is amazing getting some sort of direction after weeks of trying to get this working!
Offline Agro
« Reply #6 - Posted 2012-10-30 01:06:15 »

Well what I do is to avoid having to loop through every single other tile, just get the tile that the player is currently located at and work with that. I also make getters for tiles around it like getNorthTile, east, west, south, and of course the tile the player is currently on.

Offline philfrei
« Reply #7 - Posted 2012-10-30 05:57:23 »

Quote
So then how would I separate the players left, right, top and bottom with a Rectangle?

Sub-rectangles?

How about Polygon triangles that meet in the center but have a broad side on each relevant front? Just throwing out an idea, one that I haven't seen used but seems like it should work.

I'd have to know more about your specific intended use to better understand the tradeoffs of the different possibilities.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline SonicPixelation

Senior Newbie





« Reply #8 - Posted 2012-10-30 06:28:34 »

You sort of have the right idea. As for collision I do a comparison between the location of all four sides. Like if the right side of block a is greater than the left side of block and the left side of block a is less than the right side of block b : there will be a collision on the x axis. But that just gives you x and it would collide anywhere on the y axis so you do something very similar to the y a is just with the top and bottom.


Then in the update method have something like if (!solid){x+= xvelocity; y+= yvelocity };  and if x+xvelocity intersects another x of a tile  solid = true so the it dosent update

Hope this helps.
Offline StumpyStrust
« Reply #9 - Posted 2012-10-30 06:43:50 »

How to till what side there is a collision on using rectangles.

Get the intersection using the oh so convenient getIntersection method in Rectangle/Rectanle2D. This will give the rectangle that is the intersection of the 2 rectangles.

If the width is less then height, then it is a collision on either the right or left. See whether or not the x vel is positive or negative to find which side.

If the width is greater then height, then you have collision on either the top or bottom. Again, check y vel to see which one.

You can then add the intersection width or height to the player to move them up properly. This will not work for all cases (equal width and height or vel of 0) but is a good starting point.

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  
Rectangle2D rect = this.getIntersection(block.entities.get(i));
            if(trect.getWidth() < rect.getHeight())
            {
               if(vel.x < 0)
               {
                   //blah blah
              }
               else
               {
                 
                   //blah blah
              }
            }
            else
            {
               if(vel.y < 0)
               {
                 
                   //blah blah
              }
               else
               {
                 
                   //blah blah
              }
            }

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline delmator

Junior Member


Medals: 4
Projects: 3



« Reply #10 - Posted 2012-10-30 13:52:00 »

You guys are absolute legends!
Thanks for all the useful help. I decided to go for Rectangles for my player and the tile map. From my understanding I can use the methods getMinX, getMaxY ect to my advantage for checking collisions, ie. if (player.getMinY < block.getMaxY) I can move.

Now I'm just stuck on figuring out which Rectangles in my tile map I'm testing against :S is this what getIntersection() is used to figure out? or should I use a function which returns the players current position in the tile map?

Sorry about all the questions! For some reason this is a tough concept for me to get my head around. Honestly you guys must be actual geniuses for understanding all this!
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.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (59 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!