Camera and obstacles

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);

            }
        }

Hi. I don’t see anything obvious about why your limit is being ignored, but I can tell you that a single ray trace is not going to give you great results. I have a good bit of experience with camera implementations and you are going to want to ray trace at least 4 different directs (probably more) in order to avoid objects and terrain. Creating a fluid Camera that avoid obstacles and eases around them is quite an art in itself.

As far as your issue, are you certain the results are outside of your limit? To me it is looking more like you might be colliding with something you don’t expect to because of the direction of the camera.

You are right. Because checking the camera view line, by checking the rays is not very accurate for me , I’m thinking about using a ghost object. Can the ghost object be used like a regular object, e.g. with RigidBodyControl? It would be useful if I placed the camera in the center of the ball that would move away from obstacles.