SFML community forums

Bindings - other languages => Java => Topic started by: Codejunkie on August 24, 2015, 02:28:46 am

Title: Collision Detection
Post by: Codejunkie 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?
Title: Re: Collision Detection
Post by: Hapax 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.
Title: Re: Collision Detection
Post by: dabbertorres on August 24, 2015, 07:43:26 pm
It looks right to me from here. Could you create a minimal example?
Title: Re: Collision Detection
Post by: Nexus on August 24, 2015, 08:37:48 pm
Have you tried to debug that code and compare your expectations with the actual results?
Title: Collision Detection
Post by: Codejunkie 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.
Title: Re: Collision Detection
Post by: Nexus 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.
Title: Re: Collision Detection
Post by: Codejunkie 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?
Title: Re: Collision Detection
Post by: Codejunkie 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...