Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (763)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
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  
  Java rectangles for collision? [solved]  (Read 3231 times)
0 Members and 1 Guest are viewing this topic.
Offline GNecro1
« Posted 2014-07-13 14:21:28 »

I have been using rectangles for collision since i have started making "games" in java, but they are not working i have trued everything to make it work and it doesn't! So is there any other way of collision in java? I don't mean collision detection i mean like player cannot pass throu walls and stuff like that! I have been getting the collision and making a rectangle out of it and i take it width and subtract it of the player x, repeat for y! I get player shaking out of the rectangle. Need help!

Java freak! Cheesy
Offline Longarmx
« Reply #1 - Posted 2014-07-13 15:35:13 »

You will have to use the rectangles as sensors. If they overlap, then you process that collision, like moving the player back to its last spot to keep it from going through walls.

If you don't want to use rectangles, then google AABB (Axis-Aligned Bounding Box) collision detection. This will give you the same result as the rectangles, but it is a different implementation for you to try out.

Edit: The rectangles will not magically keep from going through each other if that's what you want. You just have to use them to detect the collision, and then you will process it.

Offline GNecro1
« Reply #2 - Posted 2014-07-13 16:12:37 »

But that is what i have been doing and it's not working out. Maybe my code is wrong? Can you give me an a example?

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

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #3 - Posted 2014-07-13 17:08:49 »

But that is what i have been doing and it's not working out. Maybe my code is wrong? Can you give me an a example?
What do you mean by "not working out"? Have you tried to debug your game? Do you have at least a slight idea that what is wrong with your code?
Basically all you have to do is make a bounding box for your sprites and when you're moving them iterate over all the other bounding boxes. If it collides with one of them prevent the movement (or do something like bouncing off). This should be trivial to implement, but here you go:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public void collisionDetection(){
   for(Sprite sprite1 : sprites){
      // Move sprite1 to it's new position (and it's bounding box too)
      // but before you do that you might want to save it's current position
      // to restore the player there if a collision happens.
      // ...
      Rectangle bb1 = sprite1.getBoundingBox();
      boolean collided = false;
      for(Sprite sprite2 : sprites){
         Rectangle bb2 = sprite2.getBoundingBox();
         if(sprite1 != sprite2 && bb1.intersects(bb2)){
            // Collision happened
            // Handle collision (prevent movement, bounce off, etc.)
            // ...
            collided = true;
            break;
         }
      }
      if(!collided){
         // The sprite didn't collide with anything else
         // so handle movement (increase position, etc.)  
      }
   }
}

I wrote this off the top of my head so hopefully there are no errors in the code. This uses Java's AWT Rectangle class for checking rectangle intersections. Implementing your own intersection "algorithm" shouldn't be hard either. Smiley

Edit: I forgot to mention that these kind of collision detections will never be perfect because if your sprite's acceleration goes high it can easily clip through slim walls and such. To prevent this you'll have to look into raycasting which is considered as a bit of an advanced topic (but it isn't terribly complex IMO). Pointing

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Offline Longarmx
« Reply #4 - Posted 2014-07-13 17:08:57 »

But that is what i have been doing and it's not working out. Maybe my code is wrong? Can you give me an a example?

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  
33  
// Example code
Rectangle player;
Rectangle wall;

public void update()
{
     // These will store the player's last position before it moves.
     lastPlayerX = playerX;
     lastPlayerY = playerY;

     // However you move your player
     movePlayer();

     // Set the rectangles to enclose the player and the wall
     player.set(playerX, playerY, playerWidth, playerHeight);
     wall.set(wallX, wallY, wallWidth, wallHeight);

     checkCollision();
}

public void checkCollision()
{
     // If the player rectangle is intersecting the wall rectangle
     if(player.intersects(wall))
          handleCollision();
}

// handleCollision could be like setting the player's coordinates to the last coordinates to keep it from going through the wall
private void handleCollision()
{
     playerX = lastPlayerX;
     playerY = lastPlayerY;
}

Offline GNecro1
« Reply #5 - Posted 2014-07-13 17:15:02 »

Thanks! But i get stuck in place when i am doing this!

Java freak! Cheesy
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #6 - Posted 2014-07-13 22:32:18 »

Collision systems are all about how people react to them. In other words, the moment the collision happens, you need to react in the right way in order to create a realistic feel. I've done collision for both tile based and non-tile based games. For tile based, it is very easy, you just block the tile and the movement stops in its tracks. For non-tile based games, the best way to halt movement is to base collision on direction.

Mario Game Example - Cut & Paste IDE

In other words, if the player is trying to move right, you want to not only stop the player but move them left far enough so the don't collide with the object. Vice-versa for right. Having player states (like walking, jumping, falling, etc.) can help you, the designer, plan out the movements better.

Remember, it isn't the colliding boxes that is causing the problem, it is how you tell your program to react to them. The example I put up is a fully working collision example that covers all the basic cases. Collision is only hard if you don't take the time to solve every case and cover all your edge cases.

Offline SauronWatchesYou

JGO Ninja


Medals: 33
Projects: 4
Exp: 2 years


Hi there! :)


« Reply #7 - Posted 2014-07-14 12:09:00 »

The easiest way that someone helped me with this problem is to simply move the bounding rectangle before the player is moved. If the player bounds overlaps something; say a wall in this case, the player can't move and we use a boolean to stop the movement. Below is a snippet of my movement code for my game, it works great for what I need, though it might not be ideal for every case.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
if(Gdx.input.isKeyPressed(Keys.D) && canMoveEast){ // This is testing if the player can move east

         player.bounds.set(player.getX()+4, player.getY(), 32, 32); // Set the bounds of the collision rectangle 4 ahead, stops player getting stuck in the wall
         for(int i =0; i < CollisionArray.size(); i++){ // Loop the array to check for collisions

            if(player.bounds.overlaps(CollisonArray.get(i))){ // If the player does collide with something
               canMoveEast = false; //Set the movement of east to false and the player can't move
            }
          }
         
         if(canMoveEast){ // If the loop runs all the way through and finds no collisions, the player can then move.
            player.setX(player.getX() + 4); // Moves the player
         }
resetMovement(); // canMoveEast = true; etc. Don't forget to reset the movement at the end :)!
}


I hope this helps in someway

Hey, you! Back to work
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (541 views)
2018-06-10 19:43:48

EgonOlsen (646 views)
2018-06-10 19:43:44

EgonOlsen (442 views)
2018-06-10 19:43:20

DesertCoockie (788 views)
2018-05-13 18:23:11

nelsongames (1019 views)
2018-04-24 18:15:36

nelsongames (1006 views)
2018-04-24 18:14:32

ivj94 (1587 views)
2018-03-24 14:47:39

ivj94 (560 views)
2018-03-24 14:46:31

ivj94 (1393 views)
2018-03-24 14:43:53

Solater (566 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!