Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
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  
  Java 2D collision detection  (Read 9999 times)
0 Members and 1 Guest are viewing this topic.
Offline matevos

Junior Newbie





« Posted 2012-08-13 10:46:55 »

Hi, I learning java about 5 days, and today I wanted to make a game, I stuck on collision detection in my game. If someone can take a look on source code I will be appreciate. Already I made the collsion "system" but character just only slowing the move when it colliding and do not stop as I want. Colliding system is based on Rectangles and intersects.

Its a link for source code for my game http://www.mediafire.com/download.php?wlk8dh1a4v8axfm


Ps
I'm new on this forum and If you wrote something wrong in my post or put this post in wrong place just say and I will fix this. Thanks.

Offline ReBirth
« Reply #1 - Posted 2012-08-13 11:19:54 »

Posted your code on forum's pastebin. It's bothering to go to MF.

I havent seen your code, but had you reset the colliding object when it intersected? If not then the method keep returning true.

Afterall like I said, post your code on pastebin and post the link here for futher diagnose. Welcome to JGO Smiley

Offline matevos

Junior Newbie





« Reply #2 - Posted 2012-08-13 12:12:59 »

Main game class Game.class
http://pastebin.java-gaming.org/7508a212028

Movement.class
http://pastebin.java-gaming.org/508a1302829

Map render map.class
http://pastebin.java-gaming.org/08a1048292b

For help big thanks.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-08-13 12:18:36 »

Had tried to set the Movement.Speed to 0?

Offline matevos

Junior Newbie





« Reply #4 - Posted 2012-08-13 12:22:07 »

I do not know to to explain you this but if you can download the project and run him and you will see whats happening when character colliding with wall. I tried many ways to make this work, and finally I found this forum.
Yeah I tried setting up speed to 0 but this don't work, character just passes through the wall slowly.
Please check the project if you can then you will see what happening. Thanks for any advice.
Offline StumpyStrust
« Reply #5 - Posted 2012-08-13 17:16:22 »

It looks like you are doing game logic inside you rendering loop....Never ever do that.

Have a game update or tick method that updates everything. Then have a render method that ONLY renders. g.drawStuff at x and y

Confusing game logic and rendering is a very bad idea.

Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


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


« Reply #6 - Posted 2012-08-13 22:54:02 »

I can help you out with the error part. It is at class Map on line 52. You are checking to see if the guy collides with "every" block in the entire map. He is going to move for each of those blocks little by little.

To fix, you might want to iterate through the block loop once and see if the guy is colliding with "1" red block. If he is, break out of the loop and stop his movement. You might want to position your movement code within the Map class, and if possible, get a separate update and render phase. It'll make the process easier. Hope this helps.

Offline ReBirth
« Reply #7 - Posted 2012-08-14 02:19:13 »

@OP
Not everybody want to be bothered DLing project, open eclipse then import it. That's why JGO has pastebin for long code so we can diagnose with mind and imagination.

Your 0 setting wont work because of this
1  
2  
3  
4  
5  
6  
if(!hero2.hero.intersects(blocks.get(i))){
                  Movement.isCollUp = false;
                  Movement.isCollDown = false;
                  Movement.isCollLeft = false;
                  Movement.isCollRight = false;        
            }

That IF, acts like "else" for
1  
if(hero2.hero.intersects(blocks.get(i)))
. If the player doesn't collide with last block on the ArrayList (no matter he collides with other block), all booleans will be false resulting movement not to be 0 anymore.

Offline matevos

Junior Newbie





« Reply #8 - Posted 2012-08-14 07:56:38 »

So where I can put this code which resuming the movement. Because I know what you mean but I don't know where I can put this code to resume the movement. I don't have any idea. For help big thanks.
Offline ReBirth
« Reply #9 - Posted 2012-08-14 11:45:15 »

Not really clear by "resuming the movement". If by collision the move will be change then you only need to deal with one block collision at one time loop check (at least for debugging sake if it isn't what you want).

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

Junior Newbie





« Reply #10 - Posted 2012-08-15 13:37:58 »

Ok I tried to fix this. It is a code:
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  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
public void checkcoll(){
      if (MemoryCollision == null){
         System.out.println("LASD");
         if(blocks.size() != 0){
            for (int i=0; i<blocks.size();i++){
               if(hero2.hero.intersects(blocks.get(i))){
                 
                  MemoryCollision = blocks.get(i);
                 
                  if(Movement.up == true){
                     Movement.Speed = 0;
                     Movement.isCollUp = true;
                     Movement.up = false;                  
                     break;
                  }
                  if(Movement.down == true){
                     Movement.Speed = 0;
                     Movement.down = false;
                     Movement.isCollDown = true;
                     break;
                  }
                  if(Movement.left == true){
                     Movement.Speed = 0;
                     Movement.left = false;
                     Movement.isCollLeft = true;
                     
                  }
                  if(Movement.right == true){
                     Movement.Speed = 0;
                     Movement.right = false;
                     Movement.isCollRight = true;
                     break;
                  }
               }
            }
         }
      }
     
      if (MemoryCollision != null){
System.out.println("Parameters: " + MemoryCollision"); //This method showing only this the if statement don't working.
         if(!hero2.hero.intersects(MemoryCollision)){ //Here is problem, this method don't checking if character don not colliding with this 1 object.
            Movement.isCollUp = false;
            Movement.isCollDown = false;
            Movement.isCollLeft = false;
            Movement.isCollRight = false;
            MemoryCollision = null;
         }
      }
   }


So I made that when character collide with 1 of the objects, this 1 object parameters going to the memory and I stoping checking for collision. And then I only checking if character stop colliding with this object if yes then I resuming the movement that character can move if no I resuming checking that character not colliding with this object. Thanks for any help.
Online BurntPizza
« Reply #11 - Posted 2012-08-19 20:32:00 »

OK, I think I got this:

It looks to me like you are checking for collision with any block in the map (brute force method), but then you get out of the loop and check again if he is still colliding. Well if you don't do anything of course he is still going to be colliding! Wink

Simple (not the best, but easy to understand, which is key!) method of resolving the collsion:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
Rectangle collision = hero.intersection(collisionBlock); //  Get the intersection itself
           
            if(hero.collUp) {              // If he was colliding upwards,
               hero.y -= collision.height;// move him back down
               
            } else if(hero.collDown) {     // If he was colliding downwards,
               hero.y += collision.height;// move him back up
               
            } else if(hero.collRight) {    // If he was colliding to the right,
               hero.x -= collision.width; // move him to the left
               
            } else if(hero.collLeft) {     // If he was colliding to the left,
               hero.x += collision.width; // move him to the right
           }


Put that in between the end of the collision check loop and the is-he-still-colliding check, and tell me how it goes!  Smiley

(Also I have more code if you need to see it to get the picture)
Offline SHC
« Reply #12 - Posted 2012-11-10 14:37:37 »

I know that this is not the actual question but you are doing your game loop wrong.

This is the part which you mentioned.

1  
2  
3  
4  
5  
6  
7  
8  
9  
public void run(){
    while(Running){
        movechar();
        repaint();
        try{
            Thread.sleep(10);
        }catch(Exception ex){}
    }      
}


You are moving the character and repainting the panel. It's fine and the error lies in your timing mechanism. Let's see this in a timeline.

The time taken by the the functions will depend on the machine. So assume that at first you are running the game on a fast computer and the updating takes 10 milliseconds (Suppose). Then you sleep for another 10 millis and one loop takes 20 millis. So for one second, you would move 50 pixels if your position increments by 1 every loop.

If you are running on a slow computer, the time may take more and result in slowing down the game.

I recommend you reading the following articles on game-loops.

http://www.koonsolo.com/news/dewitters-gameloop/
http://www.java-gaming.org/index.php?topic=24220.0

Offline RylandAlmanza

Junior Member


Medals: 3



« Reply #13 - Posted 2012-11-10 16:17:18 »

I use the algorithm described in the top answer of this question. It's very easy to implement, and works pretty well for most purposes. I don't have a Java example, but I could post a javascript implementation from my last game if you're interested.this question
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.

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

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

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

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

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

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

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

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

Longarmx (29 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
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!