Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Collision Detection  (Read 13035 times)

0 Members and 1 Guest are viewing this topic.

Codejunkie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Collision Detection
« on: August 24, 2015, 02:28:46 am »
Hi, I'm somewhat new to java game development and thought I'd give SFML a try. I'm trying to do some basic collision detection. For some reason it's not detecting anything. Here's my function for my collision:
Code: [Select]
public static boolean isColliding(ArrayList<Sprite> sprites, Sprite spriteA){
boolean result = false;
for(Sprite sprite : sprites){
if(spriteA.getGlobalBounds().intersection(sprite.getGlobalBounds())!= null){
result = true;
break;
}

}
return result;
}
Am I on the right track at least?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision Detection
« Reply #1 on: August 24, 2015, 03:32:26 pm »
I don't know Java but it looks like it's the right kind of thing.
It may be a specific coding error that only someone that knows Java can spot.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Collision Detection
« Reply #2 on: August 24, 2015, 07:43:26 pm »
It looks right to me from here. Could you create a minimal example?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Collision Detection
« Reply #3 on: August 24, 2015, 08:37:48 pm »
Have you tried to debug that code and compare your expectations with the actual results?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Codejunkie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Collision Detection
« Reply #4 on: August 24, 2015, 09:23:44 pm »
It runs fine without collision detection, but as soon as I enable it; the sprite won't move at all.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Collision Detection
« Reply #5 on: August 24, 2015, 09:26:58 pm »
That doesn't answer my question ;)

Looks like it collides too often, and the collision response prevents the sprite from moving. You should really use a debugger, with it you can solve this in minutes.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Codejunkie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Collision Detection
« Reply #6 on: August 25, 2015, 02:58:28 am »
Okay, I added this line to my code:
Code: [Select]
System.out.println("spriteA: " + spriteA.getGlobalBounds() + "\nsprite" + i + ": " + sprite.getGlobalBounds());
And got this as a result:
Quote
spriteA: FloatRect{left=561.0, top=296.0, width=128.0, height=128.0}
sprite1: FloatRect{left=56.0, top=176.0, width=128.0, height=128.0}
spriteA: FloatRect{left=561.0, top=296.0, width=128.0, height=128.0}
sprite2: FloatRect{left=176.0, top=56.0, width=128.0, height=128.0}
spriteA: FloatRect{left=561.0, top=296.0, width=128.0, height=128.0}
sprite3: FloatRect{left=176.0, top=296.0, width=128.0, height=128.0}
spriteA: FloatRect{left=561.0, top=296.0, width=128.0, height=128.0}
sprite4: FloatRect{left=296.0, top=176.0, width=128.0, height=128.0}
spriteA: FloatRect{left=561.0, top=296.0, width=128.0, height=128.0}
sprite5: FloatRect{left=561.0, top=296.0, width=128.0, height=128.0}
Should I be using getPosition() instead of getGlobalBounds() possibly?

Codejunkie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Collision Detection
« Reply #7 on: August 25, 2015, 03:25:31 am »
I think I figured it out. I have all of my sprites (including the one I designated as my "player" sprite) in the ArrayList of sprites to check for collision. Thus, it was checking for a collision against itself... *doh* how embarrassing...