How to resolve collisions?

Hi,

I've problem about resolving collisions. Let's say I have a class like this


public class Thing {
  private DynamicPhysicsNode physNode;
  private int health;
  private int damage;

  //constructor
  public Thing() {
    physNode = getPhysicsSpace().createDynamicNode();
    health = 100;
    damage = 10;

    final SyntheticButton collisionEventHandler = physNode.getCollisionEventHandler();
    input.addAction( new MyCollisionAction(), collisionEventHandler, false );  //input is some InputHandeler - doesn't matter
  }

  private class MyCollisionAction extends InputAction {
    Object owner;

    public MyCollisionAction(Object owner) {
      this.owner = owner;
    }

    public void performAction( InputActionEvent evt ) {
      final ContactInfo contactInfo = ( (ContactInfo) evt.getTriggerData() );
      //how can I perform following?
      owner.health -= $$theOtherThing$$.damage;
      $$theOtherThing$$.health -= owner.damage;
    }
  }
}



The problem there is how can I get from ContactInfo some knowledge about the Thing object which contains just collided physicsNode? As the code shows I'm able to get information about one of those Thing objects but not about both of them in the same performAction() call.

A small additional question: Do I understand it right that if the code was like it is, then with one physical collision of two Things comes two calls of mycollision.performAction() so the health of both Things would be decreased twice?

Thanks for any ideas

hiya,

i think you don't need a collision handler per Object. You only need one per PhyscisSpace.



    public void performAction(final InputActionEvent evt) {

        final ContactInfo info = ((ContactInfo) evt.getTriggerData());
        DynamicPhysicsNode object1 = (DynamicPhysicsNode) info.getNode1();
        DynamicPhysicsNode object2 = (DynamicPhysicsNode) info.getNode2();

        // now you can check the name or reference of the two objects
        if (object1 == physNode) {
           // do stuff
        }
       // or
        if (object2.getName().equals("playerPhysicsNode") {
           // do stuff
        }



Hi,

you are right that in such case I would need only one collision handler for whole PhysicsSpace, but it does not solve the problem with the Thing class. I have to use and modify attributes of Thing class (health, damage), but how can I access them in performAction method? I have access to the collided physicNodes but not to the Thing objects above them. And because I can not modify or subclass PhysicNode and therefore made a back-reference to the Thing class I can not access and modify it:-(…


Simply introduce a map from physics node to your "things" (e.g. java.util.HashMap).

darn irrosor beat me to it, i type to slow …

Thanks both:-)

Yea, the Map actually is the solution. I was just hoping for something less… I dont know how to express it - it's another map to manage:-)) It seems like a patch or an alternative way. But it probably can't be solved in different way, so thank both

Core-Dump said:

darn irrosor beat me to it, i type to slow ...

hehe, looks like it's still too fast :P

pff, i know irrosor is your real name, you made a typo when you registred :wink: