Reacting to collisions with different Objects

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Reacting to collisions with different Objects

Postby Mustache » Thu Apr 20, 2017 5:50 pm

Hey, no newbie to programming here, but new to game development.

In my game i have an ArrayList of Objects of class GameElement. All "actors" of my game are derived from this class. In each frame I detect collisions between these GameElements and give each GameElement a list of objects it collided with.

Now assuming i have two different "actors" in my ArrayList, Player and Enemy (both derived from GameElement), i wish to react differently depending on if a Player collides with a Player or an Enemy. The list containing the collisions however is a list of GameElements.

Is there anyway to check to find out the (actual) subclasses of the GameElements and then cast them? Is this a completely wrong approach to collision handling?

ive tried using instanceof but this returns false (outputs: "colliding with something else")
Code: Select all
GameElement collisionObject = new GameElement();

collisionObject = getCollisionObject(); //GameElement returned here may have subclass Player or Enemey

if(collisionObject instanceof Player){
      System.out.println("colliding with a Player");
}else{
      System.out.println("colliding with something else");
}
Mustache
 
Posts: 7
Joined: Mon Sep 08, 2014 8:07 am

Re: Reacting to collisions with different Objects

Postby evilentity » Thu Apr 20, 2017 6:23 pm

Code: Select all
collisionObject.getClass();

Sticking some sort of more generic type into GameElement and handling collisions based on that is probably a better idea.
Looking for a freelancer? PM me!
evilentity
 
Posts: 3702
Joined: Wed Aug 24, 2011 11:37 am

Re: Reacting to collisions with different Objects

Postby Mustache » Thu Apr 20, 2017 6:35 pm

Hey thanks for the reply! My issue is that i may wish to act based on information that not all classes share. For instance if Player collides with Car Class i may wish to call Car.getEngineType(), however if Player collides with Tree Class i would call Tree.getTrunkLength();

Having a Baseclass that has getEngineType() and getTrunkLength() wouldnt really make sense :)

I dont want to keep different lists for different types of objects because that defeats part of the point of having a GameElement class in the first place.

Is there a best practice for this kind of situation? How are collisions with different object types generally handled?
Mustache
 
Posts: 7
Joined: Mon Sep 08, 2014 8:07 am

Re: Reacting to collisions with different Objects

Postby evilentity » Thu Apr 20, 2017 7:29 pm

It wouldnt. Thats not what i was saying.
You need to know somehow how to deal with the collision, or not. Checking what sort of class the thing is, is certainly one of them.
Try replacing
Code: Select all
System.out.println("colliding with something else");

with
Code: Select all
System.out.println("colliding with " + collisionObject.getClass());

so its clearer what sort of fun bug you have in there.
Looking for a freelancer? PM me!
evilentity
 
Posts: 3702
Joined: Wed Aug 24, 2011 11:37 am

Re: Reacting to collisions with different Objects

Postby Mustache » Fri Apr 21, 2017 1:06 pm

Hi, I solved my problem by implementing the visitor pattern. As for why instanceOf wasnt working as intended i assume it because i initialized the collisionObject with new GameElement()!

Thanks for your time :)
Mustache
 
Posts: 7
Joined: Mon Sep 08, 2014 8:07 am


Return to General Development

Who is online

Users browsing this forum: No registered users and 4 guests