• Register
Post news RSS DevBlog 15 - Binary Search Camera Collision System

Using binary search to determine camera distance with collision shapes.

Posted by on

Update Video:

How I stopped my camera from clipping into models; without using raycasts (because my engine doesn't have easy an easy generic ray cast system).

My firsts idea was to just slap a collision shape around a camera, but this has some issues.
By surrounding the camera with an OBB (oriented bounding box), we can use that to collide with shapes.
When a collision occurs, through Separating Axis Theorem (SAT), we get a Minimum Translation Vector (MTV) that will get the moving shape out of collision.
We can offset the camera with the MTV, and then project the new position onto a vector from the original camera location toward the player ship, to get a new position to place the camera.
This works in many cases, but the MTV approach has some issues.
Most noticeable is when the camera is far behind the colliding object; in this case the MTV can actually create more distance between the camera and the player, rather than shortening the distance.

So, my second idea to fix this was to ensure that we are always moving the camera towards the player ship.
When a collision occurs, we do not know how far towards the ship we should move.
So, I take inspiration from the binary search algorithm, and move the camera half of the distance between its current position and the player position.
We then check for collision again.
If we collide, we move it another half distance towards the player and repeat.
if we do not collide, we move it a half distance away from the player (half of the distance between camera current position and previous position).
This allows the camera to home in on the correction location that is not colliding.

This binary searching camera works surprisingly well, but it does have issues.
When there is collision between the ship and camera, but the camera doesn't collide, we can end up in a situation where we cannot see the player ship.
So, rather than surrounding the camera with a collision shape, we create a collision shape from the player to the camera.
We only shrink this shape from the end on the camera.
This means we will never get collisions between the camera and the player ship.
This is the camera system I am currently using.

Unrelated to cameras, I also fixed the collision on the large ships so the the small fighter ships would not clip into them as easily.

Collision Shape Debugging: Camera boom collision shape is drawn red to signal a collision was found
sa vid15 debug collision

Tweaking Collision Shapes For Camera Collision
sa vid15 add collision shapes

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.