Bit of help please to finish of collision detection

Hey guys,



I've been working on getting collision detection working. I basically need it to make sure you can go through an object - nothing special or realistic. So far I've managed to get it to work providing you go into the object a certain direction. I know why this happens, I move the object 'back' a little. However, if you go in to the object from the other direction, this actually makes the player go through object.

Here is what I've done so far.



        //Get collisions
        BoundingCollisionResults results = new BoundingCollisionResults();
        results.clear();
        player.findCollisions(this.getRootNode(), results);
       
       
        if(results.getNumber()==1)
        {
           //if player is not colliding - backup last position
           lastposition = new Vector3f(player.getLocalTranslation());
           //System.out.println("last position = "+lastposition);         
        }
       
        //player colliding into an object (e.g. A wall, tree etc)
        if(results.getNumber()>1)
        {       
           float collisionPointZ = results.getCollisionData(1).getTargetMesh().getLocalTranslation().getZ();
           //System.out.println("World Coords "+buf);

           if(player.getLocalTranslation().getZ() < collisionPointZ)
           {
              //set last pos
              System.out.println("colliding position1 = "+lastposition);
                        float posZ = lastposition.getZ();
              
              lastposition.setZ(posZ-0.02f);
              player.getLocalTranslation().set(lastposition);
           }

           if(player.getLocalTranslation().getZ() > collisionPointZ)
           {
              //set last pos              
                   float posZ = lastposition.getZ();
              
              lastposition.setZ(posZ+0.02f);
              player.getLocalTranslation().set(lastposition);
           }
        } 



So i'm trying to see what direction the player is going into the object from and just setting the player to be a bit further back. However, this doesn't get work as collisionPointZ is always a minus number (not sure why) and so only one of the if-statements ever gets executed, so if you go in to the object another way, you just go through it........damn.

i tried just setting the player back to the lastposition but they just ended up getting stuck in the object - not ideal!

I was thinking maybe I should be using the getWorldCoords() method to get a more absolute number and then this method would work? The problem with that is i'm not sure what each individual number of the FloatBuffer actually is.

Any help is great appreciated.

This is a bit old, but I will answer it anyway…



The easiest way to do this is to keep the last position of the player so that you know at least a last valid position you can backtrack if you detect a collision.

Hi,



I am working on the same thing. Its a bit difficult with physics and all I need like you is for the object to not go through the walls. This is what I have come up with so far (from TestSwitchNodeCollision.java example) and it acknowledges the collision but for some reason the lastPosition remains null. However, if I set it to any other vector(i.e say 110f, 12f, 110f) it works perfectly. The code is as described below:



        private TriangleCollisionResults results;

        results = new TriangleCollisionResults();
        results.clear();
        player.findCollisions(node, results);
       
              
        if (results.getNumber() == 0)
        {
            lastPosition = new Vector3f(player.getLocalTranslation());
        }
        else
        {
                    
            for (int i = 0; i < results.getNumber(); i++)
            {
                if (results.getCollisionData(i).getTargetTris().size() > 0)
                {
                    player.setLocalTranslation(new Vector3f(lastPosition));
                }
            }
        }



Hope it helps and if someone knows why the lastPosition is not being stored or how to get just slight back then it would be greatly apprecited!

Thanks,

Dev


EDIT

I was working on it and I found a way to partially implement it - it works in most cases but still has a tendency every now and again to go through the wall. I have minimized this by increasing the number being subtracted from X and Z co-ordinates(as my movement is along those axis). The smaller the number the more inefficient this code becomes. The code is as below:



        private TriangleCollisionResults results;
        results = new TriangleCollisionResults();
        results.clear();
        player.findCollisions(scene, results);
       
              
        if (results.getNumber() == 0)
        {
            //Do nothing
        }
        else
        {
            lastPosition = new Vector3f(player.getLocalTranslation());
            float X = lastPosition.getX();
            float Y = lastPosition.getY();
            float Z = lastPosition.getZ();
           
            for (int i = 0; i < results.getNumber(); i++)
            {                               
                if (results.getCollisionData(i).getTargetTris().size() > 0)
                {                   
                    player.setLocalTranslation(new Vector3f(X-2.5f, Y, Z-2.5f));
                }
            }
        }



Its not the best but is suitable for time being! Any comments and/or suggestions from other members welcome! I would be curious to know why does the object still go through the wall few times (the percentage of which is higher than I would like!)...