Hello everybody,
in your opinion what is the best way to understand if an object is in front of or behind the camera?
Method 1, 2 or 3?
An example of use could be removing dead enemy models when they are no longer visible from the player’s camera or adding enemies to the scene when not looking at the spawn point.
// 1.
@Override
public void controlUpdate(float tpf) {
if (isDead && !spatial.checkCulling(cam)) {
spatial.removeFromParent();
}
}
// 2.
@Override
public void controlUpdate(float tpf) {
if (isDead) {
Transform tr = new Transform(cam.getLocation(), cam.getRotation(), Vector3f.UNIT_XYZ);
Vector3f camRelative = tr.transformInverseVector(spatial.getWorldTranslation(), null);
if (camRelative < 0) {
//The object is behind the camera
spatial.removeFromParent();
}
}
}
// 3.
@Override
public void controlUpdate(float tpf) {
if (isDead) {
Vector3f dirToTarget = spatial.getWorldTranslation().subtract(cam.getLocation()).normalizeLocal();
float angleDiff = cam.getDirection().angleBetween(dirToTarget);
float threshold = FastMath.DEG_TO_RAD * 45;
if (angleDiff > threshold) {
spatial.removeFromParent();
}
}
}
Just out of curiosity, what does this comment mean in the Transform
class? Is the formula correct or not?
// The author of this code should look above and take the inverse of that
// But for some reason, they didn't ..
// in.subtract(translation, store).divideLocal(scale);
// rot.inverse().mult(store, store);