/// <summary> /// Creates a capsule body. /// </summary> /// <param name="simulation">The simulation to use.</param> /// <param name="position">The position of the body.</param> /// <param name="rotation">The rotation of the body.</param> /// <param name="radius">The radius of the capsule.</param> /// <param name="height">The height of the cylinder part of the capsule.</param> /// <returns>The capsule body that was created.</returns> public static CapsuleBody Create(PhysicsSimulation simulation, Vector3 position, Quaternion rotation, float radius, float height) { var obj = new CapsuleBody(simulation); obj.Position = position; obj.Rotation = rotation; obj.Radius = radius; obj.Height = height; simulation.Register(obj); return(obj); }
/// <summary> /// Determine whether a capsule and a sphere intersect. /// </summary> /// <param name="capsule">Capsule physics object</param> /// <param name="sphere">Sphere physics object</param> public static bool CapsuleSphereCollision(CapsuleBody capsule, SphereBody sphere) { // reference frame: capsule at (0, 0, 0), pointing in positive y direction var sphereCoordsRelative = sphere.Position - capsule.Position; // to transform coordinate system to have capsule be axis-aligned, we apply // the reverse of the rotation to the sphere coordinates var inverse = new Quaternion(capsule.Rotation.X, capsule.Rotation.Y, capsule.Rotation.Z, -capsule.Rotation.W); var sphereCoordsTransformed = Vector3.Transform(sphereCoordsRelative, inverse); // coordinates of the line in the centre of the cylinder part const int capsuleMinY = 0; var capsuleMaxY = capsule.Height; const int closestPointToSphereX = 0; var closestPointToSphereY = Math.Clamp(sphereCoordsTransformed.Y, capsuleMinY, capsuleMaxY); const int closestPointToSphereZ = 0; return(Vector3.DistanceSquared(sphereCoordsTransformed, new Vector3(closestPointToSphereX, closestPointToSphereY, closestPointToSphereZ)) < Math.Pow(capsule.Radius + sphere.Radius, 2)); }
public static CapsuleBody Create(PhysicsSimulation simulation, Vector3 position, Quaternion rotation, Vector2 size) { var shape = new Capsule(size.X, size.Y); shape.ComputeInertia(1, out var inertia); var index = simulation.Simulation.Shapes.Add(shape); var collidable = new CollidableDescription(index, 0.1f); var activity = new BodyActivityDescription(0.01f); var pose = new RigidPose(position, rotation); var descriptor = BodyDescription.CreateDynamic(pose, inertia, collidable, activity); var handle = simulation.Simulation.Bodies.Add(descriptor); var obj = new CapsuleBody(simulation, handle); simulation.Register(obj); return(obj); }
/// <summary> /// Determine whether a capsule and a box intersect. /// </summary> /// <param name="capsule">The capsule</param> /// <param name="box">The box</param> public static bool CapsuleBoxCollision(CapsuleBody capsule, BoxBody box) { throw new NotImplementedException("Capsule-box collision checks are not implemented."); }
/// <summary> /// Determine whether two capsules intersect. /// </summary> /// <param name="firstCapsule">The first capsule</param> /// <param name="secondCapsule">The second capsule</param> public static bool CapsuleCapsuleCollision(CapsuleBody firstCapsule, CapsuleBody secondCapsule) { throw new NotImplementedException("Capsule-capsule collision checks are not implemented."); }