Class UserData
- Namespace
- Box3D
- Assembly
- Box3D.NET.dll
Application data attached to physics objects.
public static class UserDataInheritance
Inherited Members
Examples
The pattern this is designed for. Physics reports what moved, and the identifier indexes straight into the application's own storage:
Body body = world.CreateDynamicBody(spawnPoint);
body.UserData = entityId;
world.Step(1.0f / 60.0f);
foreach (BodyMoveEvent moved in world.Events.BodyMoves)
{
ref Transform transform = ref transforms[moved.Body.UserData];
transform.Position = moved.Position;
transform.Rotation = moved.Rotation;
}
Working out which entity a ray hit:
RaycastHit hit = world.RaycastClosest(muzzle, aim * range);
if (hit.Hit)
{
ulong entity = hit.Shape.Body.UserData;
Damage(entity, amount: 25);
}
Remarks
Every world, body, shape and joint carries one System.UInt64 that Box3D stores and never interprets. It is the intended way to get from a physics object back to whatever your game calls it.
Treat it as an identifier: an entity id, an index into your own array, a handle from your own table. It is deliberately not an object reference, so there is nothing to keep alive and nothing to free.