Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 bug fix [Solved]  (Read 486 times)
0 Members and 1 Guest are viewing this topic.
Offline kingroka123

JGO Ninja


Medals: 44
Projects: 7
Exp: 1 year


Gamer's Helmet


« Posted 2013-04-15 23:21:23 »

I have a simple collision system in my game where it uses rectangles to detect when another rectangle intersects with it. whenever the player collides from the top or left of the rectangle the collision acts fine but when it collides with the bottom of right sides, the player goes through the block until it hits the other side. anyone else have this problem. anyone have a fix?

thanks

ps. i didnt post any code because i am assuming that the process is well known and popular since it is so simple. if im wrong please tell me

Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #1 - Posted 2013-04-15 23:33:50 »

Could you post your code? It sounds like you implemented collision but not fully (thats why it doesn't work on all sides)

Offline kingroka123

JGO Ninja


Medals: 44
Projects: 7
Exp: 1 year


Gamer's Helmet


« Reply #2 - Posted 2013-04-16 00:26:28 »

here it is:

Player collision (collision box in the middle of screen):

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  
50  
51  
52  
53  
54  
public static void Collision(){
      for (int i=0;i<e.size();i++){
         if (bounds().intersects(e.get(i).bounds())){  
            //collision stuff here
            if(e.get(i).enNum==5){
               System.out.println("hit a wall");
               if(direction==1){
                  goRight=false;
                  goUp=false;
                  goLeft=false;
                  goDown=false;
                  speedx=0;
                  x-=1;
                  canChange=false;
               }
               else if(direction==2){
                  goRight=false;
                  goUp=false;
                  goLeft=false;
                  goDown=false;
               x+=5;
               speedx=0;
               canChange=false;
            }
               else if(direction==3){
                  goRight=false;
                  goUp=false;
                  goLeft=false;
                  goDown=false;
               speedy=0;
               y-=5;
               canChange=false;
            }
               else if(direction==4){
                  goRight=false;
                  goUp=false;
                  goLeft=false;
                  goDown=false;
               speedy=0;
               y+=1;
               canChange=false;
            }
         }
            else{
               goRight=true;
               goUp=true;
               goDown=true;
               goLeft=true;
               //speed=8;
            }
      }

}
}


here is the collision to the tiles:

1  
2  
3  
public Rectangle bounds(){
      return (new Rectangle(-Player.x+x,-Player.y+-y, size,size));
   }


and finally the player collision box:

1  
2  
3  
4  
5  
public static Rectangle bounds(){
      return (new Rectangle(Main.width/2*Main.Scale, Main.height/2*Main.Scale,32,32));
     
     
   }

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online SHC
« Reply #3 - Posted 2013-04-16 08:39:32 »

I would instead use the top left point as the anchor of the rectangle.

Here my
getBounds()
method.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public Rectangle getBounds()
{
    // reuse a single rectangle to save memory
    this.bounds.x = this.x;
    this.bounds.y = this.y;
    this.bounds.width = this.width;
    this.bounds.height = this.height;

    return this.bounds;
}

And use this code to know the side of overlapping.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
// Calculate distances
int xd = Math.abs( (rect1.x + rect1.width)/2 - (rect2.x + rect2.width)/2 );
int yd = Math.abs( (rect1.y + rect1.height)/2 - (rect2.y + rect2.height)/2 );

if (xd > yd)
{
    // Collision on either top or bottom
}
else if (yd > xd)
{
    // Collision on either left or right
}
else
{
    // Collision on corners
}

Offline quew8

JGO Coder


Medals: 33



« Reply #4 - Posted 2013-04-16 11:08:28 »

@SHC, I just noticed that your doing twice as many divisions as you need to in your "Calculate distances."

1  
2  
int xd = Math.abs( (rect1.x + rect1.width)/2 - (rect2.x + rect2.width)/2 );
int yd = Math.abs( (rect1.y + rect1.height)/2 - (rect2.y + rect2.height)/2 );


Could instead be:

1  
2  
int xd = Math.abs( ( rect1.x + rect1.width - rect2.x - rect2.width ) / 2 );
int yd = Math.abs( ( rect1.y + rect1.height - rect2.y - rect2.height ) / 2 );


Just trying to help.
Online SHC
« Reply #5 - Posted 2013-04-16 11:46:28 »

@quew8

Yeah, that would be more faster.

Offline kingroka123

JGO Ninja


Medals: 44
Projects: 7
Exp: 1 year


Gamer's Helmet


« Reply #6 - Posted 2013-04-16 20:23:30 »

thanks guys this really helped Grin

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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (88 views)
2014-12-09 22:41:13

BurntPizza (111 views)
2014-12-08 04:46:31

JscottyBieshaar (82 views)
2014-12-05 12:39:02

SHC (92 views)
2014-12-03 16:27:13

CopyableCougar4 (99 views)
2014-11-29 21:32:03

toopeicgaming1999 (159 views)
2014-11-26 15:22:04

toopeicgaming1999 (157 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

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