Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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 Detection] Thinks it's colliding even though it isn't  (Read 508 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2014-03-04 20:53:41 »

Hey,

I've been trying to solve this little bug in my code for a while now, but I can't seem to figure it out yet. When running my program the player entity correctly falls to the ground due to the gravitational pull, but if he walks off of the ground then he doesn't fall.

As far as I can tell, the two collision checking if-else statements or something regarding them is probably messing up. The game seems to think that the player is colliding with something and therefore won't allow it to fall anymore.

If someone cares to check out the code and see if you can figure out a possible solution, thanks.

Link to the two classes I believe are causing/are related to this issue:
https://github.com/MackProgramming/SideScroller/tree/master/src/entity

Here is the compiled game:
https://mega.co.nz/#!wg11xJgb!PAbhBae2qjtM_N8Ka036_Me4f0p686n4AmV6nAv9vsM
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


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


« Reply #1 - Posted 2014-03-04 22:16:26 »

It looks like you are only checking collisions if you are moving?

I think that you need to separately check collisions. Check the y axis and x axis collisions separately from the movement code. The only reference I see to y is when you jump. The only time I see it for x is when you move.

Also, there is no code that alters the y value. I'll have to go digging around, but it seems like it'll always be 0.

Offline pploco1996

Junior Devvie


Medals: 2
Exp: 1 year



« Reply #2 - Posted 2014-03-04 23:03:57 »

You can just use JBox2D and chill...  Cool
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 160
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2014-03-04 23:07:15 »

Some people like learning how these things work, recommending a library isn't solving anything. I'm sure OP knows about all the libraries he could use, he just doesn't want to which is fine.

Offline pploco1996

Junior Devvie


Medals: 2
Exp: 1 year



« Reply #4 - Posted 2014-03-04 23:19:03 »

Yeah you are right. Its cool to know that kind of stuff and its a good programming habit as well.
Offline UprightPath
« Reply #5 - Posted 2014-03-05 00:12:08 »

Found it! It's one of the issues with using copy paste!

If you look at your willcollide calls you're defining your rectangle incorrectly in both cases. The first two parameters are the corners of the rectangle. In this case? You're setting them as your delta not your position. So your always checking against your bottom left tile. And you'll never collide on the left edges. :3

So this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      // Check X-Axis collision:
      if(!super.willCollide(tiles, new Rectangle((int)(tempDX + super.getX()), (int)(tempDY), super.getCurrentSprite().getWidth(), super.getCurrentSprite().getHeight()))) {
         super.setDX(tempDX);
      }

      // Check Y-Axis collision:
      if(!super.willCollide(tiles, new Rectangle((int)(tempDX), (int)(tempDY + super.getY()), super.getCurrentSprite().getWidth(), super.getCurrentSprite().getHeight()))) {
         super.setDY(tempDY);
      } else {
         super.setDY(0.0);
      }

Should be (I changed the second one slightly so that you're testing at the end X instead of at the previous):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      // Check X-Axis collision:
      if(!super.willCollide(tiles, new Rectangle((int)(tempDX + super.getX()), (int)(super.getY()), super.getCurrentSprite().getWidth(), super.getCurrentSprite().getHeight()))) {
         super.setDX(tempDX);
      }

      // Check Y-Axis collision:
      if(!super.willCollide(tiles, new Rectangle((int)(super.getDX() + super.getX()), (int)(tempDY + super.getY()), super.getCurrentSprite().getWidth(), super.getCurrentSprite().getHeight()))) {
         super.setDY(tempDY);
      } else {
         super.setDY(0.0);
      }


Offline tyeeeee1
« Reply #6 - Posted 2014-03-05 02:26:41 »

=O I've been going over it for hours and that was my only problem... Wow. Thanks for the information, now I'll know for next time and it works perfectly now!

You can just use JBox2D and chill...  Cool

I never use a library unless I either can't code what I need or it's necessary. Collision is simple, when I don't mess up a piece of code as UprightPath has shown me, so I don't use a library for it. =P
Offline UprightPath
« Reply #7 - Posted 2014-03-05 02:47:28 »

Glad to be of help!

Also, just as a bit of extra advice: Your collision detection will likely act oddly at times the way it's written. If the player is less than the delta away from the object that's going to be collided with it's going to stop without hitting the object. By that, I mean that its DX will be set to 0 at such a point that its edge will not touch the other object. This will probably be more noticeable when the player is falling from a higher point (e.g. has a higher DX before impact) or if you increase the update time. It will have a sort of falling stutter: It'll have DX = 0 while above the ground, then have gravity applied until it's DX is greater than the distance between the player and floor, stop again, then continue. In the end, if the positional information were different then you might even see the player "float" above the ground due to this.

Offline tyeeeee1
« Reply #8 - Posted 2014-03-05 15:14:14 »

Hmm... Now that you mention it, I do think that it could do that. I'll rework things if I do come across that issue in the future, but at the moment I should be able to prevent it with carefully chosen values for the gravitational pull, movement speeds, etc...
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.

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

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

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

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

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

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

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

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

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
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!