Collision Detection Trouble (basics)

Hi :slight_smile:



Firstly: yes this is homework, and I'm sorry for asking for such basic help but I'm really struggling here with collision detection and I'm very pressed for time (tomorrow @midnight GMT is my deadline  :|).



I have a node representing a room, in which there are several pillars. A pillar is a cylinder with setIsCollidable(true) and setModelBounds(new BoundingBox()).



I also have a player Node (which contains a camera node) which moves around the room. I am trying to get the player node not to be able to walk through the pillars. The player object has setIsCollidable(true) and model bounds set. It is controlled by the keyboard.



I am trying to implement this first with the step forward method and then I will roll this out along the other movement methods.



This is the code which moves the player forward:



        private class KeyNodeStepForwardAction extends KeyNodeForwardAction {
      
      public KeyNodeStepForwardAction() {
         super(player,movespeed);
      }
      
      public void performAction(InputActionEvent evt) {
         super.performAction(evt);
         player.setModelBound(new BoundingBox(player.getLocalTranslation(),1,1,1));
         for(Spatial s:lab.getChildren()) {
            if(player.hasCollision(s, true))
               System.err.println("t- collided with "+s.getName()+"!");
         }
      }
      
   }



The pillars are attached as children to the lab object mentioned in the code (the room is our compsci lab).

My test routine is to start the game and make the player walk through a pillar while watching System.err waiting for an error to pop up and tell me there has been a collision. So far I have not managed to remove the player's godlike ability to walk through solid pillars :(

What am I doing wrong, can anyone help?

Many thanks for any help offered :)

So you never see your print statement?



On thing I noticed is:



public void performAction(InputActionEvent evt) {

super.performAction(evt);

[glow=red,2,300]player.setModelBound(new BoundingBox(player.getLocalTranslation(),1,1,1));[/glow]

for(Spatial s:lab.getChildren()) {





This will set a new bounding box on the player every time an event is processed for the input action. That is no good, and could be part of your problem. Move that out and put with the creation code for your player.



Compare what you have written to the TestCollision example. What are you doing different?

Well for a start I am missing the calculateCollisions(…) call, and yeah that new bounding thing can't be good - here's my updated code (which still isn't working) - i can't see any glaring difference between this and the test version:



   private class KeyNodeStepForwardAction extends KeyNodeForwardAction {
      
      public KeyNodeStepForwardAction() {
         super(player,movespeed);
      }
      
      public void performAction(InputActionEvent evt) {
         
         // move forward
         super.performAction(evt);

         // clear the previous results
         results.clear();
         
            // calculate the collisions
         player.calculateCollisions(lab, results);
         // if there's a collision the step back
         if(player.hasCollision(lab,false))
            System.err.println("t- collided!");
      }
      
   }



lab is a high level node object of which all the objects with which the player could collide are children. Of note, here is the initialisation of the CollectionResults object results, just to be sure...


public PlayerHandler(Player player,Lab lab) {
      
      super();
      
      this.player=player;
      this.lab=lab;
      
      setKeyBindings();
      setActions();
      
      results = new BoundingCollisionResults()
      {
           public void processCollisions() {
           }
       };
      
   }



... and the definition of the key binding, so we're sure that the method is actually being called...


   private void setActions() {
      
      /*
       * step forwards and backwards
       */
        KeyNodeStepForwardAction forward = new KeyNodeStepForwardAction();
        addAction(forward, "forward", true);

...