Is bounding based collision detection possible to implement for more than 1 node - e.g. can I do that for players and enemies?? I tried but for some reason if I kept both player and the enemy the player would go through more often than not?? Is it some known problem or am I implementing it incorrectly?? Just to answer any common questions - I am not setting it to "lastPosition" but making calculations instead as because the velocity does not go zero the player keeps bumping into the wall.
player collision detection(added same for enemies but then player went through most walls)
private void findCollisions() //try and use this method for all have node as param
{
//Expand the collision detection to be even more precise
//e.g. say movement in only one direction
BoundingCollisionResults results = new BoundingCollisionResults();
results.clear();
walls.findCollisions(player1, results);
if(results.getNumber()==0)
{
lastPosition = new Vector3f(player1.getLocalTranslation());
System.out.println(player.getLocalTranslation());
}
Thanks neakor. The only problem with that is because the player is going at a certain velocity if I set it to last position which is closest to the wall the player will keep going into the wall. Because the velocity is handled by flagrush handler it is difficult to set it to 0. I tried to set the velocity to 0 on collision by making the flagrush handler and inner class and tried something along these lines:
instead of moving the ur player back to a previous location, do a collision check before u move it. if the returned distance between ur player and other objects is less than a tolerance value, just forbid the player from moving.
That sounds a better approach neakor. How do you do that kind of collision check? From what you say and from your experience it sounds like you have already worked on a game before. How did you approach it in terms of coding what you say above - could you give a small example?
Alright… I have come up with the code that throws the ray in default direction(that is enemy's +Z) and based on the results, if they contain "Walls" then it will detect their distance. If this distance becomes less than 4f then now we need to find the next suitable point. I can detect the direction of players movement but how do I detect the next best point for movement and set enemy's translation to that point? The code about the functionality is as below:
private void findEnemyCollisions()
{
Vector3f eP = enemyNode.getLocalTranslation();
Ray ray = new Ray(enemyNode.getLocalTranslation(), enemyNode.getLocalRotation().getRotationColumn(2));
PickResults results = new BoundingPickResults();
results.setCheckDistance(true);
enemyNode.findPick(ray,results);
System.out.println(ray.direction);
System.out.println(results.getPickData(2).getTargetMesh().getParentGeom().getParent().getName());
if(results.getNumber() > 0)
{
Vector3f position = enemyNode.getLocalTranslation();
float X = position.getX();
float Y = position.getY();
float Z = position.getZ();
for(int i = 0; i < results.getNumber(); i++)
{
if(results.getPickData(i).getTargetMesh().getParentGeom().getName() == "Walls")
{
if(results.getPickData(i).getDistance() < 4f)
{
if(enemyNode.getLocalTranslation().getX() > lastPosition.getX() && enemyNode.getLocalTranslation().getZ() > lastPosition.getZ())
{
System.out.println("Movement Detected in +X and +Z: " + enemyNode.getLocalTranslation());
}
else if(enemyNode.getLocalTranslation().getX() > lastPosition.getX() && enemyNode.getLocalTranslation().getZ() < lastPosition.getZ())
{
System.out.println("Movement Detected in at +X and -Z: " + guardNode.getLocalTranslation());
}
else if(enemyNode.getLocalTranslation().getX() < lastPosition.getX() && enemyNode.getLocalTranslation().getZ() > lastPosition.getZ())
{
System.out.println("Movement Detected in -X and +Z: " + enemyNode.getLocalTranslation());
}
System.out.println("Name of the object: " + results.getPickData(i).getTargetMesh().getParentGeom().getName() + " Distance: " + results.getPickData(i).getDistance());
}
results.clear();
}
}
I appreciate all you help neakor as I dont have long before finishing the project and this function would prove invaluable. Also, this would help in bringing all small bits and pieces such as following the player, getting back to get med and ammo supplies, implementing a fourth bot that hunts down the player - all with avoiding collision which will be the best.
This system is for enemy use primarily - I want enemy to avoid collisions with walls when it chases player and player goes inside a loop of walls(maze) or something - e.g. when player runs away now and goes inside such a loop, instead of enemy following the player through that path it goes through the wall and gets infront of the player - e.g. by using addLocal and subtractLocal
Yeah I know… Primarily I just want it to detect the wall and find a safe point I dont care greatly about a chase as I know pathfinding algorithm is required for one. If its moving in a specific direction and it detects a wall at equal to or less than tolerable distance just move to a suitable point. If I implement this then if there is a chase if will create a bouncing movement as it will try to chase a player using addLocal/subtractLocal and this algorithm will set its location to a specific point. I dont mind that for the timebeing. First priority is Collision Avoidance/Detection.
Sorry if I was unclear. I just want simple collision check first and foremost.