Camera aligned with ray

Hello !

I am casting a ray from a mesh toward the camera, once the ray hits the ground the camera radius is changed to the hit point distance, then a check for intersections is done between a sphere (which has the same position as the camera but it’s distant from the ray) and all the meshes of the scene.

It seems to work fine if I move the camera slowly with the mouse, however, if I move the camera too fast, sometimes, the sphere and the ground will actually intersect.

What can be done to have the sphere always aligned with the ray, so that the sphere wouldn’t actually hit the ground before the ray?

Here’s the PG:

Try to move the camera toward the ground by moving the mouse slowly/quickly, intersections will be logged in the console.

I am quite new to any of this. I am sorry if this was already covered in another question.
Many thanks for any help you can provide.

Hello and welcome!

what you could would be to use camera.onAfterCheckInputsObservable to check if the position of the camera is intersecting and then restore previously known position?

Hi @Deltakosh
Thanks for the welcome and for the help you have provided!

I have tried to use onAfterCheckInputsObservable as you suggested, however without success (most likely because I am not applying logic correctly)

Here’s what I did:
Added a variable that would hold the camera safe position, the safe position changes only when the sphereCollision is not intersecting with anything. Once the sphereCollision intersects with something, the camera and the sphereCollision would be moved to the safe position. All of this happens inside onAfterCheckInputsObservable.

Here’s the PG:

I was wondering if maybe there’s a different way to do what I am trying to achieve. Basically the idea is to make a camera that orbits around the character, however, I don’t want it to slowdown on collisions, get stuck around obstacles or pass thru other meshes.

In my mind this could be achieved by attaching various ray pointing to the camera, then check if the camera is colliding with anything, if so, move the camera to the ray that has the shortest distance from the character. This way the camera would always be fast and rotate without worrying of obstacles or clipping effects.

Any suggestions are very welcome!
Thank you again for the help!

The problem is inertia. The camera to get its smooth movement uses inertia and thus even if you restore the position of the camera, it will keep moving along its inertia vector

To check if this is the case you can set camera.inertia = 0