# SDF (Signed Distance Field) used to Detect Overlapping Objects

SDF is short for signed distance field and describes the interior volume of an object. How thick an object is from the middle to the surface? How deep does the cylinder extend? From any point inside an object, using the SDF, it is possible to easily grab the distance to the surface of the object in order to know the depth.

By knowing the depth of every point on a geometry, it makes it very easy to calculate collision and identify all the points that are colliding between objects. This makes it much easier to create collision animation when object collide, the collision data can be used to deform other objects in contact. Perfect for creating complex simulated animation scenes.

## Positional Field of Values

SDF is a field of values, where the field revolves around the object in question and all the values are distances from a certain point in the volume of the object to the surface of the object. These field of values correspond to positional values. For example, you would query the SDF value of a certain P position of an object and the returning result would be a number that describes the distance it is from the surface of the object.

## Negative SDF Values & Positive SDF Values

The negative and positive signs are used to denote where the position is located from the surface of the object. Is it located inside the interior of the object? Or is it located outside, somewhere exterior from the object? This signed value is necessary because just by returning the distance value from the surface of the object, it isn’t possible to tell if that distance is away from the surface of the object or embedded deep inside the volume of the object.

In Houdini, the SDF volume values of an object are negative if position is located inside the interior volume of an object. And positive if the P position is located away from the exterior surface of the object.

## How is SDF Useful?

The SDF is often used in CG (computer graphics) software applications to calculate collision or when certain objects overlap each other. The SDF values are perfect for this because of the distance value can provide an offset of when the distance of two objects get close and the SDF distance tells us exactly how close objects are to each other.

In a tutorial video, the SDF is used to detect when a torus object bouncing up and down collides with a ground plane in order to create footprints by deforming the ground plane. The SDF provides a way to detect when these two objects collide and how far the torus presses onto the ground plane in order to calculate the displacement values to deform the ground plane.

In the screenshot on the right, the P positions come from the ground plane and we’re asking those P position of the Volume Sample of the SDF of Torus object. So in other words, we’re querying what is the SDF value of the Torus object at position P of the ground plane.

**Now you might be wondering, why are we using P positions from the ground plane when we’re querying the SDF of the Torus?**

This is because we’re trying to detect when these two objects collide or overlap. When these two objects overlap, the SDF values of the torus will be negative. Why?

Let’s look at this another way. Consider the scene where the Torus is high above the ground plane and not even touching the ground plane, no where near it. If we use the P positions of the ground plane and use that P value to query the SDF value and the returned SDF value will be positive.

**How do I know that?** That’s because what was discussed in the above paragraphs.

**Quote from above:**

*“In Houdini, the SDF volume values of an object are negative if position is located inside the interior volume of an object. And positive if the P position is located away from the exterior surface of the object.”*

In our hypothetical scene where the Torus and ground plane is no where near each other, if we query P values of the ground plane and ask for the SDF value of the Torus using that P, and since the Torus and ground plane is no where near each other, the SDF is definitely positive. Because the P position is located away from the exteriod of the surface of the Torus.

## SDF Detecting Collision

Now when the Torus and ground plane collide, that means some of the P positions of the ground plane and the Torus are the same. We have overlapping P positions, because both objects are colliding.

What happens when we query the SDF values now?

Let’s query a P position from the ground plane that is overlapping with the Torus, let’s call it **P_overlap**. This **P_overlap **is used to query the SDF of the Torus.

What is the resulting value? Positive or negative? Is the **P_overlap **inside the Torus or outside the Torus?

Remember we are considering the scene where the Torus and ground collides, so the P_overlap value will be located inside the interior of the Torus or at least touching on the surface. The SDF values will be negative or zero.

## Zero SDF Value

There’s a possibility for the SDF values to be zero because they can be touching exactly on the surface of both objects, but this is a very small case and even if you neglect this case in the Houdini scene, it probably won’t show in the resulting animation.

## Using the SDF to Help Deform the Ground Plane

We know the positive and negative values of the SDF can detect when the objects collide, but we also need to deform the ground plane to get a foot print. The SDF value is the distance from the surface of the object from the P position. This gives us magnitude of how much to deform the ground plane, because we know when P intersects with the ground plane and how deep the Torus is pressing onto the ground plane because of the SDF distance value from the surface.

We have magnitude or how deep the foot print is in the ground and we know when both objects collide. All we’re missing is the direction. How the direction is calculated is covered in the next blog post titled, “SDF used to Deform Geometry”. Check out the animation I posted on the top of the next blog post where the torus collides with the ground plane and creates a footprint by deforming the ground plane.