Physics Data

PhysicsData describes the physical shape of an object: its convex hull, volume, surface area, centre of volume and the cross section it presents to a flow from any direction. Wind uses it to find how much of a body the wind actually meets. Add it to any GameObject with a Rigidbody when you need shape-aware physics.

The hull #

The hull is the convex envelope of the object’s shape, the same idea as Unity’s own convex mesh collider. It is built once from either:

  • RenderedMeshes (the default): the mesh filters in the object’s hierarchy, the shape you see. Only the highest detail LOD is used.
  • Colliders: the colliders in the hierarchy, the shape the physics engine sees.
SettingDefaultWhat it does
Source (HullSource)RenderedMeshesWhere the hull is built from.
Max Hull Vertices96The most vertices a computed hull may have. Zero for no limit. Larger meshes are reduced to this.
customHullMeshnoneAn optional mesh to use as the hull instead of the hierarchy, in the local space of the object. It is wrapped in a convex hull.
ignoredMeshemptyMesh filters to leave out when the hull is built from the rendered meshes.
debugoffDraw the hull, bounds and velocity in the scene view when the object is selected.

Baking #

Computing a hull takes time, so PhysicsData bakes it in the editor and stores the result on the component. A baked hull costs nothing at runtime.

  • Click Bake Hull in the component’s inspector. Clear removes the stored hull so it is computed again.
  • Help, Heathen, Unity Physics, Bake All PhysicsData in Open Scenes and Bake All PhysicsData in Project Prefabs bake everything at once.
  • Builds bake any component that was never baked automatically, so nothing is computed at runtime in a player.
  • An unbaked hull is computed the first time it is used and shared between instances with the same meshes, which needs readable meshes (Read/Write enabled). Baking avoids that requirement at runtime.

Call Bake() and ClearBake() from editor code, or RegisterGeometry(IEnumerable<MeshFilter>) to build the hull from meshes that change at runtime. IsBaked tells you whether a hull is stored.

Scale #

The hull is stored at unit scale in local space and every query applies the object’s current lossy scale for you, including non-uniform scale and nested objects. Do not multiply by the scale yourself.

Reading the shape #

MemberReturns
VolumeThe volume of the hull in cubic metres at the current scale.
AreaThe surface area in square metres at the current scale.
DensityMass divided by hull volume, zero when the volume is zero.
xCrossSection, yCrossSection, zCrossSectionThe area of the object seen along its local X, Y or Z axis.
CrossSection(direction)The area of the shadow the object casts along any world direction (it need not be normalised). Exact for any convex hull and any scale.
CenterOfVolumeThe centre of volume in world space.
Bounds, GetBoundCorners()The local bounds at unit scale, and the eight corners of the hull bounds in world space.
ShapeThe HullShape itself, computed and cached on first use if it was not baked.
HullGeometryThe hull as a Unity Mesh, created on demand. Prefer Shape, which needs no allocation.
AttachedRigidbody, MassThe body and its mass.
LinearVelocity, LinearSpeed, LinearHeadingThe body’s velocity, its speed in metres per second, and its direction of travel.
AngularVelocity, AngularSpeed, AngularHeadingThe angular velocity, its speed in radians per second, and its axis of rotation.
LinearDragCoefficient, AngularDragCoefficientThe Rigidbody’s linear and angular damping.
LinearDragEffect, AngularDragEffectThe force and torque Unity’s damping is applying right now.

PhysicsData.For(Rigidbody) finds the enabled PhysicsData of a body without a component lookup, and returns null when it has none. The volume, area and cross sections are read-only properties, set the shape through the source and the bake.

var data = PhysicsData.For(rigidbody);
if (data != null)
{
    float shadow = data.CrossSection(windDirection);
    Vector3 pressureCentre = data.CenterOfVolume;
}

HullShape #

HullShape is the hull as plain serialisable data, at unit scale. You can use it on its own without a component.

MemberReturns
HullShape.Create(points, maxVertices = 0)A hull around a set of points. Duplicate points are welded first. If the points are fewer than four, coplanar or otherwise cannot form a solid, the bounding box of the points is used so the result is always usable.
HullShape.FromBounds(Bounds)A hull in the shape of a box.
Vertices, Triangles, TriangleCountThe hull vertices in local space and its outward-wound triangles.
Volume, Area, Centroid, BoundsThe hull’s volume and surface area at unit scale, its centre of volume and its bounds.
IsValidTrue if the shape holds a usable hull.
ScaledVolume(scale), ScaledArea(scale)Volume and surface area for an instance with the given scale. Area is exact for non-uniform scale.
ProjectedArea(localDirection, scale)The area of the shadow the hull casts along a direction. Exact for any convex hull and scale.
ToMesh()A new Unity mesh of the hull, for display or for use as a collider.

System Log #

PhysicsData logs through System Log under UnityPhysics.PhysicsData, gated by Heathen.LogGates.UnityPhysics.PhysicsData.

Rate This Article!