Camera collision detection?

Godot Version

4.3

Question

Okay I’m making a 3d hidden objects game. The camera is on a gimbal system that’s placed at the center of the scene. There’s a horizontal rotation node a vertical rotation node and then a camera 3d. Obviously the named node handle the relevant rotation and the zooming is just the cameras z axis moved in and out. This works fantastic for what I want all except for the camera being able to just go through the objects in my scene. All of the objects have static bodies and collision shapes. I’ve tried everything I can think of to stop the camera from clipping. I’ve tried shape cast, ray cast, adding the different physics bodies to the camera. The closest I’ve gotten to this working is a sphere shape cast that’s in the camera and setting the radius and margin up to be about 0.5 radius. The problem is the camera can still clip through the objects bc they aren’t solid for example I have buildings that are planes for the walls, etc. Idk if I’m over thinking this or missing something obvious but all I want is that when my camera collides with an object it just stops moving in that direction so it doesn’t clip through.

Could you use a CharacterBody3D on the camera, instead of setting position directly use move_and_collide(movement_vector)?

I did try a character body 3d but I never could get it to behave properly. I thought the move and slide or move and collide would be the best option for this but I don’t know how to set up the camera with the character body to work the exact way it does with the camera gimbal system. Character bodies need velocity to move don’t they?

Yeah you would have to calculate the difference in position instead of setting the position directly, it sounds like a fairly un-fun equation. You can use move_and_collide anywhere and it won’t need delta time or velocity like move_and_slide.

Mm I’m pretty new to 3d so I’ll have to do some digging I guess. You’d think it’d be as simple as slapping a body with a collision shape on the camera but that just doesn’t work of course lol

Right, well a CharacterBody will get you the most physics/collisions while still retaining control. If you post a snippet of your rotation script I can help further, I think the majority of the change would be like so

# instead of:
position = new_position

# find the difference:
var diff: Vector3 = new_position - position
var collision := move_and_collide(diff)
if collision:
    # might need to implement sliding against walls?


So as I stated this is a gimbal system where I have a node3d, node3d, node3d, camera 3d. So it goes camera gimbal, horizontal rotation, vertical rotation, camera 3d. I’m sure you can see which thing does which here. I would be able to use a CharacterBody if I knew how to take this code and apply it to the velocity of the body I guess?

I guess it also goes without mentioning, this is controlled by holding the left mouse button and dragging the mouse around.