Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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  
  Collision Help 2D Tiles  (Read 760 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74
« Posted 2013-05-28 05:40:42 »

Hello JGO, I've had this weird issue every time I try to program collision into a game lol.

I am trying to implement Collision for Rectangle Vs. Rectangle.
As we all know a rectangle has 4 sides, thus there's 4 sides to check collision for Grin

My collision works perfect, I just cannot get it to work on ALL 4 SIDES >...<


At the moment I can basically 'pick' which 2 sides (top/bottom -or- left/right) of a tile will have active collision areas.

Here is the process of my game:
1  
2  
3  
4  
5  
6  
7  
8  
   public void process() {
      if (canMove) {
         if (up) { /* do stuff... */ }
         if (down) { /* do stuff... */ }
         if (left) { /* do stuff... */ }
         if (right) { /* do stuff... */ }
      }
   }

The above code will result in me being able to slide up / down while moving right / left.
(Collision is being applied properly to the tiles left / right side)


It is vice-verse, if I re-order the code like so:
(Please note the changes between the 2 code snippets)
1  
2  
3  
4  
5  
6  
7  
8  
   public void process() {
      if (canMove) {
         if (left) { /* do stuff... */ }
         if (right) { /* do stuff... */ }
         if (up) { /* do stuff... */ }
         if (down) { /* do stuff... */ }
      }
   }

The above code will result in me being able to slide left / right while moving up / down.
(Collision is being applied properly to the tiles top / bottom side)


Notes: It always favors the last 2 if statements 0_0

This is rather weird 0_0, I've tried implementing a Thread which starts prior to the if statements in hopes of running the other collision sides on a whole new 'scene' but that still didn't work >.<

If anyone can help that would be great, if I left out anything needed or you require something more than what I provided ^_^ please feel free to ask me for it.

I need help :c
I'm motivated lets figure this out =D

Offline HeroesGraveDev

JGO Kernel


Medals: 269
Projects: 11
Exp: 2 years


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


« Reply #1 - Posted 2013-05-28 05:46:48 »

Okay, I'll try and outline a good method for collision detection.

1. Create a new position by adding the total velocity to the current position.
2. Check (currentX, newY) for collisions.
3. If there is a collision, don't do anything. If there isn't, set currentY to newY.
4. Check (newX, currentY) for collisions.
5. If there is a collision, don't do anything. If there isn't, set currentX to newX.

Hope that helps.

Offline GabrielBailey74
« Reply #2 - Posted 2013-05-28 05:52:38 »

Okay, I'll try and outline a good method for collision detection.

1. Create a new position by adding the total velocity to the current position.
2. Check (currentX, newY) for collisions.
3. If there is a collision, don't do anything. If there isn't, set currentY to newY.
4. Check (newX, currentY) for collisions.
5. If there is a collision, don't do anything. If there isn't, set currentX to newX.

Hope that helps.

Thanks for the feedback, implementing it now ^_^

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #3 - Posted 2013-05-28 06:46:19 »

Never mind, it didn't work :c

Thanks for the feedback though

Offline HeroesGraveDev

JGO Kernel


Medals: 269
Projects: 11
Exp: 2 years


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


« Reply #4 - Posted 2013-05-28 07:05:15 »

Code?

Offline GabrielBailey74
« Reply #5 - Posted 2013-05-28 07:13:17 »

Tis not a issue with the collision code nor how it's checked lol.

Like I said Grin it will pick a set of if statements for some reason, maybe I'm not approaching collision correctly <.<
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  
   private void update(int translate_x, int translate_y) {
      translated_x = -translate_x + absX; // player moving right = increase
      translated_y = -translate_y + absY; // player moving up = increase
      if (canMove) {
         if (up) { // up key's pressed
            last_y_move[1] = false;
            last_x_move[0] = false;
            last_x_move[1] = false;
            boolean move = true;
            if (collision_tiles != null) {
               for (int i = 0; i < collision_tiles.length; i++) {
                  if (collision_tiles[i] != null) {
                     for (Tile t : collision_tiles[i]) {
                        if (t != null) {
                           if (t.getColor() == 1) { // solid block
                              if (new Rectangle(translated_x, translated_y + mapVelY,
                                    width, height).intersects(t.getRect())) {
                                 move = false;
                              }
                           }
                        }
                     }
                  }
               }
            }
            if (move) {
               last_y_move[0] = true;
               getLoader().getWorldRender().mapY += mapVelY;
            }
         }
      }
   }

Offline kutucuk
« Reply #6 - Posted 2013-05-28 07:21:37 »

I am using something like HeroesGraveDev told to keep my character inside of the screen. It is a little buggy, but it might help you to get a better one for yourself. Here is how I do it for now;
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
float x = body.getLinearVelocity().x; //I get velocities so that when I set the Vector2, I am able to set only x or only y to 0
      float y = body.getLinearVelocity().y;
                //New position is just old position + velocity
      Vector2 newPos = new Vector2(body.getPosition().x + body.getLinearVelocity().x, body.getPosition().y + body.getLinearVelocity().y);
      if (newPos.x < 0 || newPos.x > world.camera.viewportWidth) {
         // Don't move or return
         body.setLinearVelocity(0, y);
      }
      if (newPos.y < 0 || newPos.y > world.camera.viewportHeight) {
         // Don't move or return
         body.setLinearVelocity(x, 0);


Instead of if (newPos.x < 0 || newPos.x > world.camera.viewportWidth), you could do if (newCharBounds.overlaps(wallBounds))

Like I said, it is buggy for now. It doesn't go all the way to the border of the screen when the velocity is high Cheesy
Offline relminator
« Reply #7 - Posted 2013-05-28 07:36:10 »

Why are you iterating through all the tiles when you check for collisions?
Offline GabrielBailey74
« Reply #8 - Posted 2013-05-28 07:38:47 »

Why are you iterating through all the tiles when you check for collisions?
What do you mean why xD

It's a LinkedList containing collision based & non collision based tiles.

I'm looping through the list, finding the collision based tiles and handling them appropriately lol. persecutioncomplex
1  
2  
3  
   if (t.getColor() == 1) { // solid block
      /* Handle teh collision */
   }

Should I add the collision based tiles to a new list to keep my iteration count as low as possible when dealing with collision? ~_^

Offline relminator
« Reply #9 - Posted 2013-05-28 07:51:35 »

You know, there's a way to handle tile-based collisions without iterating.


Simple test in a 1d or 2d map.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #10 - Posted 2013-05-28 08:10:25 »

You know, there's a way to handle tile-based collisions without iterating.


Simple test in a 1d or 2d map.

Do you mean problematically resolving the collision?
or..?

Offline HeroesGraveDev

JGO Kernel


Medals: 269
Projects: 11
Exp: 2 years


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


« Reply #11 - Posted 2013-05-28 08:34:13 »

Floor the position of each corner and test if there is a solid tile there.

Offline GabrielBailey74
« Reply #12 - Posted 2013-05-28 08:38:51 »

Floor the position of each corner and test if there is a solid tile there.

What do you mean ~_^?

Offline Phased
« Reply #13 - Posted 2013-05-28 08:44:35 »

You only want to check like 4 tiles, left, right, up and down tile, as its impossible for you too collide with a tile 5 tiles away
Offline GabrielBailey74
« Reply #14 - Posted 2013-05-28 08:48:14 »

You only want to check like 4 tiles, left, right, up and down tile, as its impossible for you too collide with a tile 5 tiles away

Thanks for the tip mate :>

I'm in the middle of re-writing the whole project at the moment (Making a Mini_Project for testing) ^_^

This time the player will be restricted to moving + 32pixels in each direction instead of 'free roaming' in any direction (Will be easier to do collision)

So basically he'll be able to move 1 Tile at a time, collision's 1 area in creating games that's really held me back so I'll do it in a way I'm sure will work ^_^

Offline SHC
« Reply #15 - Posted 2013-05-28 09:02:57 »

You only need to check for the four tiles if you are moving one step in a direction at once. So in psuedocode,

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  
Tile[] collisionTiles = new Tile[4];

if ( tileExistsUp )
{
    collisionTiles[0] = upTile;
}
if ( tileExistsDown )
{
    collisionTiles[1] = bottomTile;
}
if ( tileExistsLeft )
{
    collisionTiles[2] = leftTile;
}
if ( tileExistsRight )
{
    collisionTiles[3] = rightTile;
}

for ( Tile tile : collisionTiles )
{
    if ( tile != null )
    {
        // process collision here.
    }
}

Offline relminator
« Reply #16 - Posted 2013-05-28 14:36:46 »

You know, there's a way to handle tile-based collisions without iterating.


Simple test in a 1d or 2d map.

Do you mean problematically resolving the collision?
or..?

What mean is, you can calculate which tile to check for collisions by a simple division.

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

playerpos/tilesize would give you the particular tile to check.


Offline HeroesGraveDev

JGO Kernel


Medals: 269
Projects: 11
Exp: 2 years


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


« Reply #17 - Posted 2013-05-29 04:59:17 »

Floor the position of each corner and test if there is a solid tile there.

What do you mean ~_^?

Okay:

Loop through each corner of the entity's collision box.
Divide and floor by the tile size.
Get the tile at that position.
If the tile is solid, then there is a collision.

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.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (56 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (44 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!