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.