Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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-based Collision Detection Where Sprite is bigger than Tiles  (Read 2760 times)
0 Members and 1 Guest are viewing this topic.
Offline civicdude95

Senior Newbie


Medals: 1



« Posted 2011-08-03 15:06:28 »

Hey guys, like the title says, I'm having a tough time getting my collision detection to work properly. My game is a sidescrolling platformer and I'm wanting to use a tiled-based collision scheme, where each tile is either passable or impassable. I already know how to assign each tile a rectangle, but I'm not sure how to iterate over the tiles surrounding the player because my player's bounding box is twice as wide and 4 times as high as one tile (16 x 16 vs. 32 x 64). My actual sprite's dimensions are 64 x 64 (not that it matters).

Does anyone have any experience with this kind of a collision detection setup?

Read about simple game development.
Download your free copy of Prospectus
Offline Cero
« Reply #1 - Posted 2011-08-03 15:11:15 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public boolean collide(Rectangle in)
    {
        for (int y = (int)((in.y) / TEXTURE_SIZE)-4; y <= (int)((in.y+in.height) / TEXTURE_SIZE)+4; y++)
        {
            for (int x = (int)((in.x) / TEXTURE_SIZE)-4; x <= (int)((in.x+in.width) / TEXTURE_SIZE)+4; x++)
            {
                if (x >= 0 && y >= 0 && x < width && y < height)
                {
                    if (tiles[x][y] != null)
                    {
                        checkRect.setBounds(x*TEXTURE_SIZE,y*TEXTURE_SIZE, TEXTURE_SIZE, TEXTURE_SIZE);
                        if (in.intersects(checkRect))
                        {
                            return true;
                        }
                    }
                }
            }
        }

      return false;
    }

Doesn't matter how big the object is. This is my method.

note: checkRect is just a Rectangle in the class to do these calculations, just so you dont usw "new" as often, reusing this object

Offline civicdude95

Senior Newbie


Medals: 1



« Reply #2 - Posted 2011-08-03 15:19:53 »

Aha, I see what you're doing. You've basically made a bounding box that's bigger than the object (player in my case) by 4 pixels all around and you're looping through all of the box's edge pixels ( or is it 4 tiles all around the object instead? ) to check for intersection with a collide-able tile.

Do you mind if I ask you how you resolve your collisions, specifically what you do with you player to prevent them from passing through the collide-able object?

Read about simple game development.
Download your free copy of Prospectus
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2011-08-03 15:36:32 »

Aha, I see what you're doing. You've basically made a bounding box that's bigger than the object (player in my case) by 4 pixels all around and you're looping through all of the box's edge pixels ( or is it 4 tiles all around the object instead? ) to check for intersection with a collide-able tile.
its 4 tiles around. You can also use this method to render, then you will see what tiles are checked for collision, as only those are rendered on screen. or rendering all, but overlaying those with a color or something


Do you mind if I ask you how you resolve your collisions, specifically what you do with you player to prevent them from passing through the collide-able object?

This is a really broad topic, and I can't say that after long time of development I got it right. It obviously has a lot to do with what constraints you have.
gravity, shaped and nature of objects, what can the player do
specifically diagonal tiles, going up and down can be a nightmare if your player can do much to break it.

But basically my sprites have a x and y speed
I will create another Rectangle(reuse this object) copy the values of the player or whatever sprite we are talking about.
then I do x+=speedX and y+=speedY obviously
and then I take this changed Rectangle and ask if there is a collision with it.
If yes I wont apply that speed for the sprite, if no, I do.

Basically.
there are always little problems and stuff thats 1 pixel off and so on and so forth, hard to get perfect depending on how complex the game is.
I mean even in super mario you can jump into the wall if you know how

clipping and collision issues are found in most commercial games even today

hard to get right

Offline civicdude95

Senior Newbie


Medals: 1



« Reply #4 - Posted 2011-08-03 15:43:28 »

Ok I think I get it now, I can basically make a copy of the player's rect, apply the intended movement to it, then check collision for that rect, and if there was no collision then set the player's actual rect to the values of the temp rect.

I do have one question though. Why do you check for collision 4 tiles around the object/player instead of just the immediate neighboring tiles that surround the object/player?

Read about simple game development.
Download your free copy of Prospectus
Offline Cero
« Reply #5 - Posted 2011-08-03 18:54:47 »

I do have one question though. Why do you check for collision 4 tiles around the object/player instead of just the immediate neighboring tiles that surround the object/player?

not written in concrete or anything. you check how many you think is good, I just wanna be sure
partially it has to do with the fact that I had different sizes tiles once

Offline civicdude95

Senior Newbie


Medals: 1



« Reply #6 - Posted 2011-08-03 19:09:28 »

Oh ok I see. I'll probably end up doing just 1 or two tiles away for mine. I'm going to try and code this up tonight after work and hopefully I can get it working. Thanks for your help thus far!

Read about simple game development.
Download your free copy of Prospectus
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.

BurntPizza (14 views)
2014-09-19 03:14:18

Dwinin (32 views)
2014-09-12 09:08:26

Norakomi (58 views)
2014-09-10 13:57:51

TehJavaDev (79 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (62 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (31 views)
2014-09-07 01:12:14

Longarmx (36 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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!