Hi guys
I would like to achieve camera detection of obstacles in front of camera and in theory it is easy, but I’m lost. The camera’s target point is set 2.4 m forward ond 1.8 m above the ground to character. Camera Y is 2.4 m above the ground … and as a result of the ray test I have detected collisions far forward and far behind the character despite limit, and of course the effect is wrong … what I forgot to do?
the code below:
…
Vector3f posPlain = characterNode.getWorldTranslation().clone();
Quaternion rot2 = characterNode.getWorldRotation();
Vector3f dir2 = rot2.getRotationColumn(2);
Vector3f camPos = new Vector3f(dir2);
Ray rayFromCamera = new Ray(posPlain, cam.getDirection().negate());
rayFromCamera.setLimit(2.6f);
CollisionResults resultOfCamera = new CollisionResults();
shootables.collideWith(rayFromCamera, resultOfCamera);
if (resultOfCamera.size() > 0) {
Spatial whatTheSpatial = resultOfCamera.getClosestCollision().getGeometry();
String whatTheSpatialName = whatTheSpatial.getName();
temporaryDistance = resultOfCamera.getClosestCollision().getDistance();
if (temporaryDistance < behindPlayer ) {
behindPlayer = behindPlayer - 0.5f * tpf;
modifyCam = false;
}
}
camPos.normalizeLocal();
camPos.addLocal(+sidePlayer, 0, 0);
camPos.negateLocal();
camPos.multLocal(behindPlayer); // modified cam behind
camPos.setY(overPlayer);
camPos.addLocal(posPlain);
cam.setLocation(camPos);
Vector3f lookAt = new Vector3f(dir2);
lookAt.multLocal(0); // for setting target in character
lookAt.setY(1.8f); // for watching head not legs
lookAt.addLocal(posPlain);
cam.lookAt(lookAt, Vector3f.UNIT_Y);
}
}