mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-29 06:28:25 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
19 changed files with 784842 additions and 0 deletions
58
Brutal.Jolt/Manual/BodyAddBatch.cs
Normal file
58
Brutal.Jolt/Manual/BodyAddBatch.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
/// <summary>
|
||||
/// A batch of created bodies being added to the physics system in one go, from
|
||||
/// <see cref="BodyInterface.PrepareAddBodies"/>. <see cref="Commit"/> adds them; <see cref="Dispose"/> frees the
|
||||
/// batch, and aborts the add first if it was never committed. Dispose it either way.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Adding bodies in a batch builds their broad-phase nodes once for the whole set, which is much cheaper than one
|
||||
/// <see cref="BodyInterface.AddBody"/> per body, and the prepare step can run on a background thread. The batch
|
||||
/// keeps its own copy of the IDs, in the order Jolt prepared them. It holds on to the body interface it came from,
|
||||
/// so dispose it before the physics system goes; it has no finalizer for that reason. <see cref="Commit"/> and
|
||||
/// <see cref="Dispose"/> are safe to call from different threads: whichever runs second sees the first's result.
|
||||
/// </remarks>
|
||||
public sealed class BodyAddBatch : IDisposable, INullable
|
||||
{
|
||||
// Commit and Dispose both reach the native batch, so they take turns: Dispose can't free it mid-commit
|
||||
private readonly Lock _gate = new();
|
||||
private nint _batch;
|
||||
private bool _committed;
|
||||
|
||||
internal BodyAddBatch(nint batch) => _batch = batch;
|
||||
|
||||
/// <summary>Whether this is the empty (already disposed) batch.</summary>
|
||||
public bool IsNull() => _batch == 0;
|
||||
|
||||
/// <summary>Whether <see cref="Commit"/> has run; until then, <see cref="Dispose"/> aborts the add.</summary>
|
||||
public bool IsCommitted => _committed;
|
||||
|
||||
/// <summary>
|
||||
/// Adds every body in the batch to the physics system, under one lock. Committing again does nothing.
|
||||
/// </summary>
|
||||
/// <param name="activation">Whether the bodies start active.</param>
|
||||
/// <exception cref="ObjectDisposedException">The batch has been disposed.</exception>
|
||||
public void Commit(Activation activation = Activation.Activate)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_batch == 0, this);
|
||||
if (_committed)
|
||||
return;
|
||||
Jolt.PInvoke.BodyAddBatchCommit(_batch, activation);
|
||||
_committed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Frees the batch, aborting the add if it was never committed. Idempotent.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (_batch == 0)
|
||||
return;
|
||||
Jolt.PInvoke.BodyAddBatchDestroy(_batch);
|
||||
_batch = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Brutal.Jolt/Manual/BodyInterface.PrepareAddBodies.cs
Normal file
24
Brutal.Jolt/Manual/BodyInterface.PrepareAddBodies.cs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
// Hand-written wrapper for BodyInterface.PrepareAddBodies (the batch add API from JoltBodies.h).
|
||||
public readonly partial struct BodyInterface
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Starts adding many created bodies to the physics system at once, which is much cheaper than adding them
|
||||
/// one at a time. The IDs are copied, so bodyIDs can be reused straight away. Preparing touches nothing the
|
||||
/// simulation reads, so it can run on a background thread; BodyAddBatch.Commit then adds them all under one
|
||||
/// lock. Bodies must have been created (CreateBody) and not added yet.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <returns>The returned batch is yours: Commit it, then Dispose it (Dispose alone aborts the add).</returns>
|
||||
public unsafe BodyAddBatch PrepareAddBodies(ReadOnlySpan<uint> bodyIDs)
|
||||
{
|
||||
fixed (uint* bodyIDs_fixed = bodyIDs)
|
||||
{
|
||||
return new BodyAddBatch(
|
||||
Jolt.PInvoke.BodyInterface_PrepareAddBodies(this, bodyIDs_fixed, (nuint)bodyIDs.Length)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
60
Brutal.Jolt/Manual/BodyState.cs
Normal file
60
Brutal.Jolt/Manual/BodyState.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
/// <summary>What <see cref="BodyState.Flags"/> says about the body it was read from.</summary>
|
||||
[Flags]
|
||||
public enum BodyStateFlags : uint
|
||||
{
|
||||
/// <summary>The ID named no body (removed, or a stale sequence number); the rest of the state is zeroed.</summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>The ID named a body and the state was read from it.</summary>
|
||||
Valid = 1,
|
||||
|
||||
/// <summary>The body is awake and being simulated.</summary>
|
||||
Active = 2,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One body's pose, velocities, user data and activation, as <see cref="BodyLockInterface.GetBodyStates"/> reads them
|
||||
/// for many bodies under one lock.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A snapshot for syncing an ECS, a network state or render interpolation from the simulation. A static body reads
|
||||
/// as zero velocity. An ID that no longer names a body reads as the origin with the identity rotation and
|
||||
/// <see cref="Flags"/> clear of <see cref="BodyStateFlags.Valid"/>.
|
||||
/// </remarks>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 64)]
|
||||
public struct BodyState
|
||||
{
|
||||
// Laid out like the shim's JoltBodyState, 64 bytes (asserted there and in the tests)
|
||||
|
||||
/// <summary>The body's position (its centre of mass is offset from this by the shape).</summary>
|
||||
[FieldOffset(0)]
|
||||
public float3 Position;
|
||||
|
||||
/// <summary>Whether the ID named a body, and whether that body is active.</summary>
|
||||
[FieldOffset(12)]
|
||||
public BodyStateFlags Flags;
|
||||
|
||||
/// <summary>The body's rotation.</summary>
|
||||
[FieldOffset(16)]
|
||||
public floatQuat Rotation;
|
||||
|
||||
/// <summary>Linear velocity of the centre of mass, in m/s.</summary>
|
||||
[FieldOffset(32)]
|
||||
public float3 LinearVelocity;
|
||||
|
||||
/// <summary>Angular velocity, in rad/s.</summary>
|
||||
[FieldOffset(44)]
|
||||
public float3 AngularVelocity;
|
||||
|
||||
/// <summary>The body's user data, as <see cref="BodyInterface.GetUserData"/> reads it.</summary>
|
||||
[FieldOffset(56)]
|
||||
public ulong UserData;
|
||||
|
||||
/// <summary>Whether the ID named a body.</summary>
|
||||
public readonly bool IsValid => (Flags & BodyStateFlags.Valid) != 0;
|
||||
|
||||
/// <summary>Whether the body is awake and being simulated.</summary>
|
||||
public readonly bool IsActive => (Flags & BodyStateFlags.Active) != 0;
|
||||
}
|
||||
51
Brutal.Jolt/Manual/ClosestPoint.cs
Normal file
51
Brutal.Jolt/Manual/ClosestPoint.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
using Brutal.Numerics;
|
||||
|
||||
namespace Brutal.JoltApi;
|
||||
|
||||
// Hand-written wrappers for JPH::ClosestPoint free functions (templates the parse can't bind).
|
||||
public static partial class Jolt
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// The barycentric coordinates of the point on segment ab closest to the origin, so that u * a + v * b is
|
||||
/// that point. False when the segment is degenerate, which leaves the closest vertex.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static bool GetBaryCentricCoordinates(float3 a, float3 b, out float u, out float v) =>
|
||||
Jolt.PInvoke.ClosestPointGetBaryCentricCoordinates(a, b, out u, out v);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// The barycentric coordinates of the origin projected onto triangle abc, so that u * a + v * b + w * c is
|
||||
/// that point. False when the triangle is degenerate.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static bool GetBaryCentricCoordinates(float3 a, float3 b, float3 c, out float u, out float v, out float w) =>
|
||||
Jolt.PInvoke.ClosestPointGetBaryCentricCoordinates_2(a, b, c, out u, out v, out w);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// The point on segment ab closest to the origin; set gets a bit per vertex that is part of the result (1 =
|
||||
/// a, 2 = b).
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static float3 GetClosestPointOnLine(float3 a, float3 b, out uint set) =>
|
||||
Jolt.PInvoke.ClosestPointGetClosestPointOnLine(a, b, out set);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// The point on triangle abc closest to the origin; set gets a bit per vertex that is part of the result.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static float3 GetClosestPointOnTriangle(float3 a, float3 b, float3 c, out uint set) =>
|
||||
Jolt.PInvoke.ClosestPointGetClosestPointOnTriangle(a, b, c, out set);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// The point on (or in) tetrahedron abcd closest to the origin; set gets a bit per vertex that is part of
|
||||
/// the result, all four when the origin is inside.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static float3 GetClosestPointOnTetrahedron(float3 a, float3 b, float3 c, float3 d, out uint set) =>
|
||||
Jolt.PInvoke.ClosestPointGetClosestPointOnTetrahedron(a, b, c, d, out set);
|
||||
}
|
||||
89
Brutal.Jolt/Manual/ContactEvent.cs
Normal file
89
Brutal.Jolt/Manual/ContactEvent.cs
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
/// <summary>Which contact callback a <see cref="ContactEvent"/> was recorded from.</summary>
|
||||
public enum ContactEventKind : uint
|
||||
{
|
||||
/// <summary>Two bodies started touching (<see cref="ContactListener.OnContactAdded"/>).</summary>
|
||||
Added = 0,
|
||||
|
||||
/// <summary>Two bodies are still touching (<see cref="ContactListener.OnContactPersisted"/>).</summary>
|
||||
Persisted = 1,
|
||||
|
||||
/// <summary>Two bodies stopped touching (<see cref="ContactListener.OnContactRemoved"/>).</summary>
|
||||
Removed = 2,
|
||||
}
|
||||
|
||||
/// <summary>Which kinds of event a <see cref="ContactEventListener"/> records.</summary>
|
||||
[Flags]
|
||||
public enum ContactEventMask : uint
|
||||
{
|
||||
/// <summary>Nothing.</summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>Contacts starting.</summary>
|
||||
Added = 1 << (int)ContactEventKind.Added,
|
||||
|
||||
/// <summary>Contacts continuing, one event per touching pair per step: the noisy one.</summary>
|
||||
Persisted = 1 << (int)ContactEventKind.Persisted,
|
||||
|
||||
/// <summary>Contacts ending.</summary>
|
||||
Removed = 1 << (int)ContactEventKind.Removed,
|
||||
|
||||
/// <summary>Contacts starting and ending, without the per-step persisted events.</summary>
|
||||
AddedAndRemoved = Added | Removed,
|
||||
|
||||
/// <summary>Every kind.</summary>
|
||||
All = Added | Persisted | Removed,
|
||||
}
|
||||
|
||||
/// <summary>One contact event, as <see cref="ContactEventListener.Drain"/> hands it back.</summary>
|
||||
/// <remarks>
|
||||
/// Everything is copied at the time of the callback, so an event stays valid after the bodies move or go away.
|
||||
/// A <see cref="ContactEventKind.Removed"/> event only carries the body and sub-shape IDs, since that's all Jolt
|
||||
/// reports when a contact ends; its geometry and user data are zero.
|
||||
/// </remarks>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 64)]
|
||||
public struct ContactEvent
|
||||
{
|
||||
// Laid out like the shim's JoltContactEvent, 64 bytes (asserted there and in the tests)
|
||||
|
||||
/// <summary>Whether the contact started, continued or ended.</summary>
|
||||
[FieldOffset(0)]
|
||||
public ContactEventKind Kind;
|
||||
|
||||
/// <summary>The first body's ID.</summary>
|
||||
[FieldOffset(4)]
|
||||
public uint Body1;
|
||||
|
||||
/// <summary>The second body's ID.</summary>
|
||||
[FieldOffset(8)]
|
||||
public uint Body2;
|
||||
|
||||
/// <summary>The sub-shape of the first body that's touching.</summary>
|
||||
[FieldOffset(12)]
|
||||
public uint SubShape1;
|
||||
|
||||
/// <summary>The sub-shape of the second body that's touching.</summary>
|
||||
[FieldOffset(16)]
|
||||
public uint SubShape2;
|
||||
|
||||
/// <summary>How far the shapes overlap along <see cref="Normal"/>.</summary>
|
||||
[FieldOffset(20)]
|
||||
public float PenetrationDepth;
|
||||
|
||||
/// <summary>The contact normal in world space, pointing from body 1 towards body 2.</summary>
|
||||
[FieldOffset(24)]
|
||||
public float3 Normal;
|
||||
|
||||
/// <summary>The first contact point, on body 1, in world space.</summary>
|
||||
[FieldOffset(36)]
|
||||
public float3 Point;
|
||||
|
||||
/// <summary>The first body's user data.</summary>
|
||||
[FieldOffset(48)]
|
||||
public ulong UserData1;
|
||||
|
||||
/// <summary>The second body's user data.</summary>
|
||||
[FieldOffset(56)]
|
||||
public ulong UserData2;
|
||||
}
|
||||
94
Brutal.Jolt/Manual/ContactEventListener.cs
Normal file
94
Brutal.Jolt/Manual/ContactEventListener.cs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
/// <summary>
|
||||
/// A contact listener that records contacts natively for you to <see cref="Drain"/> after
|
||||
/// <see cref="PhysicsSystem.Update"/>, instead of calling into C# for every contact.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Observe-only: it can't reject a contact or change its <see cref="ContactSettings"/>. Use a
|
||||
/// <see cref="ManagedContactListener"/> for that. What it saves is the managed callback per contact, which Jolt makes
|
||||
/// from its worker threads while holding body locks, so it suits audio, damage, sensors and gameplay triggers.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Install <see cref="Handle"/> with <see cref="PhysicsSystem.SetContactListener"/>. The buffer holds
|
||||
/// <see cref="Capacity"/> events; ones past that are counted in <see cref="Dropped"/> and lost, so size it for your
|
||||
/// busiest step or drain every step. Drain, <see cref="Clear"/> and <see cref="Pending"/> must only be called between
|
||||
/// updates, never while one is running. Take the listener off the system before disposing it; it has no finalizer
|
||||
/// for that reason.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed unsafe class ContactEventListener : IDisposable, INullable
|
||||
{
|
||||
private nint _listener;
|
||||
|
||||
/// <summary>Creates a listener buffering up to <paramref name="capacity"/> events of the kinds in
|
||||
/// <paramref name="mask"/>.</summary>
|
||||
/// <param name="capacity">How many events the buffer holds between drains.</param>
|
||||
/// <param name="mask">Which kinds to record. Leaving out <see cref="ContactEventMask.Persisted"/> keeps only
|
||||
/// the starts and ends of contacts, which is usually what gameplay wants.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is zero.</exception>
|
||||
public ContactEventListener(uint capacity, ContactEventMask mask = ContactEventMask.AddedAndRemoved)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfZero(capacity);
|
||||
_listener = Jolt.PInvoke.ContactEventListenerCreate(capacity, (uint)mask);
|
||||
Handle = Jolt.PInvoke.ContactEventListenerGetBase(_listener);
|
||||
Mask = mask;
|
||||
}
|
||||
|
||||
/// <summary>The listener as the <see cref="ContactListener"/> <see cref="PhysicsSystem.SetContactListener"/>
|
||||
/// takes. Borrowed from this listener: don't Dispose it.</summary>
|
||||
public ContactListener Handle { get; private set; }
|
||||
|
||||
/// <summary>Which kinds of event are recorded.</summary>
|
||||
public ContactEventMask Mask { get; }
|
||||
|
||||
/// <summary>Whether this listener has been disposed.</summary>
|
||||
public bool IsNull() => _listener == 0;
|
||||
|
||||
/// <summary>How many events the buffer holds.</summary>
|
||||
public uint Capacity => Jolt.PInvoke.ContactEventListenerGetCapacity(Live());
|
||||
|
||||
/// <summary>How many events are waiting to be drained.</summary>
|
||||
public uint Pending => Jolt.PInvoke.ContactEventListenerGetPending(Live());
|
||||
|
||||
/// <summary>How many events were lost over this listener's life because the buffer was full. It only grows,
|
||||
/// so compare it with the last value you read.</summary>
|
||||
public uint Dropped => Jolt.PInvoke.ContactEventListenerGetDropped(Live());
|
||||
|
||||
/// <summary>
|
||||
/// Copies pending events into <paramref name="events"/>, oldest first, and returns how many were copied. What
|
||||
/// doesn't fit stays for the next call, so draining in a loop until <paramref name="remaining"/> is zero is fine.
|
||||
/// </summary>
|
||||
/// <param name="events">Where to copy the events.</param>
|
||||
/// <param name="remaining">How many events are still pending after this call.</param>
|
||||
public uint Drain(Span<ContactEvent> events, out uint remaining)
|
||||
{
|
||||
var listener = Live();
|
||||
fixed (ContactEvent* eventsFixed = events)
|
||||
fixed (uint* remainingFixed = &remaining)
|
||||
return Jolt.PInvoke.ContactEventListenerDrain(listener, eventsFixed, (uint)events.Length, remainingFixed);
|
||||
}
|
||||
|
||||
/// <summary>Throws away every pending event.</summary>
|
||||
public void Clear() => Jolt.PInvoke.ContactEventListenerClear(Live());
|
||||
|
||||
/// <summary>Frees the listener. It must not still be installed on a physics system. Idempotent.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
var listener = Interlocked.Exchange(ref _listener, 0);
|
||||
if (listener == 0)
|
||||
return;
|
||||
Handle = default;
|
||||
Jolt.PInvoke.ContactEventListenerDestroy(listener);
|
||||
}
|
||||
|
||||
/// <summary>The listener as the <see cref="ContactListener"/> a physics system takes.</summary>
|
||||
public static implicit operator ContactListener(ContactEventListener listener) => listener.Handle;
|
||||
|
||||
private nint Live()
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_listener == 0, this);
|
||||
return _listener;
|
||||
}
|
||||
}
|
||||
28
Brutal.Jolt/Manual/ContactManifold.cs
Normal file
28
Brutal.Jolt/Manual/ContactManifold.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
// Jolt's inline world-space accessors, done in C# over the fields the struct already holds rather than a native
|
||||
// call per point. The generator leaves them out (see _manualValueMembers).
|
||||
public partial struct ContactManifold
|
||||
{
|
||||
/// <summary>The world-space position of contact point <paramref name="index"/> on shape 1.</summary>
|
||||
/// <exception cref="ArgumentOutOfRangeException">The index is at or past the live contact point count.</exception>
|
||||
public readonly float3 GetWorldSpaceContactPointOn1(uint index) =>
|
||||
BaseOffset + PointAt(in RelativeContactPointsOn1, index, "contact points on shape 1");
|
||||
|
||||
/// <summary>The world-space position of contact point <paramref name="index"/> on shape 2.</summary>
|
||||
/// <exception cref="ArgumentOutOfRangeException">The index is at or past the live contact point count.</exception>
|
||||
public readonly float3 GetWorldSpaceContactPointOn2(uint index) =>
|
||||
BaseOffset + PointAt(in RelativeContactPointsOn2, index, "contact points on shape 2");
|
||||
|
||||
// Only the first Size slots are live, so the bound is the count rather than the 64-slot capacity
|
||||
private static float3 PointAt(in StaticArrayVec3_64 points, uint index, string what)
|
||||
{
|
||||
if (index >= points.Size)
|
||||
throw new ArgumentOutOfRangeException(
|
||||
nameof(index),
|
||||
index,
|
||||
$"Expected an index below the number of {what}."
|
||||
);
|
||||
return points.Elements[(int)index].XYZ;
|
||||
}
|
||||
}
|
||||
63
Brutal.Jolt/Manual/ConvexObject.cs
Normal file
63
Brutal.Jolt/Manual/ConvexObject.cs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Brutal.JoltApi;
|
||||
|
||||
/// <summary>
|
||||
/// A convex object for <see cref="Jolt.GetPenetrationDepth"/>: a box (with an optional convex radius), a sphere, or a
|
||||
/// point with a radius, placed by a transform.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A sphere and a point with a radius describe the same volume, but the solver sees them differently: the sphere is
|
||||
/// one support shape, the point hands the radius to GJK as a convex radius, the way Jolt's own shapes do.
|
||||
/// </remarks>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ConvexObject
|
||||
{
|
||||
// Laid out like the shim's JoltConvexObject, 112 bytes (asserted there and in the tests)
|
||||
private readonly uint _kind;
|
||||
private readonly float _radius;
|
||||
private readonly float _convexRadius;
|
||||
private readonly float4x4 _transform;
|
||||
private readonly float3 _center;
|
||||
private readonly float3 _min;
|
||||
private readonly float3 _max;
|
||||
|
||||
private ConvexObject(
|
||||
uint kind,
|
||||
float radius,
|
||||
float convexRadius,
|
||||
float4x4 transform,
|
||||
float3 center,
|
||||
float3 min,
|
||||
float3 max
|
||||
)
|
||||
{
|
||||
_kind = kind;
|
||||
_radius = radius;
|
||||
_convexRadius = convexRadius;
|
||||
_transform = transform;
|
||||
_center = center;
|
||||
_min = min;
|
||||
_max = max;
|
||||
}
|
||||
|
||||
/// <summary>A box from <paramref name="min"/> to <paramref name="max"/> in <paramref name="transform"/>'s space,
|
||||
/// grown by <paramref name="convexRadius"/>.</summary>
|
||||
public static ConvexObject Box(float3 min, float3 max, float4x4 transform, float convexRadius = 0f) =>
|
||||
new(0, 0f, convexRadius, transform, float3.Zero, min, max);
|
||||
|
||||
/// <summary>A box in <paramref name="transform"/>'s space, grown by <paramref name="convexRadius"/>.</summary>
|
||||
public static ConvexObject Box(AABox box, float4x4 transform, float convexRadius = 0f) =>
|
||||
Box(box.Min, box.Max, transform, convexRadius);
|
||||
|
||||
/// <summary>A sphere in <paramref name="transform"/>'s space.</summary>
|
||||
public static ConvexObject Sphere(float3 center, float radius, float4x4 transform) =>
|
||||
new(1, radius, 0f, transform, center, float3.Zero, float3.Zero);
|
||||
|
||||
/// <summary>A sphere in world space.</summary>
|
||||
public static ConvexObject Sphere(float3 center, float radius) => Sphere(center, radius, float4x4.Identity);
|
||||
|
||||
/// <summary>A point in world space, grown by <paramref name="radius"/> as a convex radius.</summary>
|
||||
public static ConvexObject Point(float3 position, float radius) =>
|
||||
new(2, radius, 0f, float4x4.Identity, position, float3.Zero, float3.Zero);
|
||||
}
|
||||
40
Brutal.Jolt/Manual/Handles.cs
Normal file
40
Brutal.Jolt/Manual/Handles.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
// Hand-written bits the generator can't express (see Generator.Interop.cs for what it does emit):
|
||||
// Factory.Instance, and the one down-cast Jolt keeps no type information for (the checked AsX() casts on
|
||||
// Shape, Constraint and VehicleController are generated).
|
||||
|
||||
public readonly partial struct Factory
|
||||
{
|
||||
/// <summary>Mirrors <c>JPH::Factory::sInstance</c>.</summary>
|
||||
/// <remarks>
|
||||
/// Assigning hands the factory to the global, and the getter borrows it back. Tear it down with
|
||||
/// <see cref="DestroyInstance"/>; never <c>Dispose</c> the handle you assigned or the one you get back.
|
||||
/// </remarks>
|
||||
public static Factory Instance
|
||||
{
|
||||
get => GetInstance();
|
||||
set => SetInstance(value);
|
||||
}
|
||||
|
||||
/// <summary>Deletes the global factory instance and clears <see cref="Instance"/>.</summary>
|
||||
public static void DestroyInstance()
|
||||
{
|
||||
var instance = GetInstance();
|
||||
if (!instance.IsNull())
|
||||
instance.Dispose();
|
||||
SetInstance(default);
|
||||
}
|
||||
}
|
||||
|
||||
public readonly partial struct MotionProperties
|
||||
{
|
||||
/// <summary>Views these motion properties as the soft-body ones they are.</summary>
|
||||
/// <remarks>
|
||||
/// <see cref="Body.GetMotionProperties"/> hands back the base type, so this is how a soft body's
|
||||
/// vertices are reached. Same handle, not a new reference, so don't <c>Dispose</c> it. Unchecked:
|
||||
/// only valid on a soft body.
|
||||
/// </remarks>
|
||||
public SoftBodyMotionProperties AsSoftBodyMotionProperties() =>
|
||||
Unsafe.BitCast<MotionProperties, SoftBodyMotionProperties>(this);
|
||||
}
|
||||
30
Brutal.Jolt/Manual/JoltException.cs
Normal file
30
Brutal.Jolt/Manual/JoltException.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
/// <summary>
|
||||
/// The exception thrown when a Jolt operation fails - a shape, constraint or settings object that won't build, or
|
||||
/// an object stream that won't restore. Its <see cref="Exception.Message"/> carries the error text Jolt reported
|
||||
/// (<see cref="Jolt.GetLastError"/>) at the point of failure.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A bad argument the binding catches before calling into Jolt still throws the usual
|
||||
/// <see cref="ArgumentException"/> or <see cref="InvalidOperationException"/>; this one means Jolt itself refused.
|
||||
/// </remarks>
|
||||
public class JoltException : Exception
|
||||
{
|
||||
/// <summary>Initializes a new <see cref="JoltException"/> with the given message.</summary>
|
||||
/// <param name="message">The failure description, typically Jolt's own error text.</param>
|
||||
public JoltException(string message)
|
||||
: base(message) { }
|
||||
|
||||
/// <summary>Initializes a new <see cref="JoltException"/> with the given message and inner exception.</summary>
|
||||
/// <param name="message">The failure description, typically Jolt's own error text.</param>
|
||||
/// <param name="innerException">The underlying exception that caused this failure.</param>
|
||||
public JoltException(string message, Exception innerException)
|
||||
: base(message, innerException) { }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="JoltException"/> whose message is the calling thread's last Jolt error
|
||||
/// (<see cref="Jolt.GetLastError"/>).
|
||||
/// </summary>
|
||||
public static JoltException FromLastError() => new(Jolt.GetLastError());
|
||||
}
|
||||
65
Brutal.Jolt/Manual/NativeArray.cs
Normal file
65
Brutal.Jolt/Manual/NativeArray.cs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
/// <summary>A contiguous buffer of <typeparamref name="T"/> the native side allocated for this call.</summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// A copy of a native <c>JPH::Array</c>, so it stays valid after the physics system moves on. This object
|
||||
/// is the block's sole owner: <see cref="Dispose"/> (idempotent) frees it, after which every span from
|
||||
/// <see cref="AsSpan"/> dangles and the buffer reads as empty. Never dispose one while a span it handed out is
|
||||
/// still in use.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// There is no finalizer, so an undisposed buffer leaks rather than being freed behind a span that's still
|
||||
/// being read. Dispose every one, typically with <c>using</c>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// When <typeparamref name="T"/> is a handle, what the buffer holds depends on how Jolt stored it. A
|
||||
/// container of <c>Ref<T></c> is copied with its references intact, so the handles live as long as the
|
||||
/// buffer. A container of raw pointers is copied as pointers only - those handles belong to whatever owned
|
||||
/// them originally (the <c>PhysicsSystem</c>, the <c>Factory</c>) and die with it, buffer or no buffer.
|
||||
/// Either way the handles are borrowed: never dispose one individually.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The block is freed through Jolt's allocator, so dispose every buffer before
|
||||
/// <see cref="Jolt.UnregisterTypes"/> / <see cref="Factory.DestroyInstance"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class NativeArray<T> : IDisposable, INullable
|
||||
where T : unmanaged
|
||||
{
|
||||
private nint _block;
|
||||
|
||||
// Read once from the block's header: both are fixed for the life of the block, and holding them here means
|
||||
// nothing reads the header again once Dispose has freed it
|
||||
private readonly unsafe T* _data;
|
||||
private readonly int _length;
|
||||
|
||||
internal unsafe NativeArray(nint block)
|
||||
{
|
||||
_block = block;
|
||||
if (block == 0)
|
||||
return;
|
||||
|
||||
var header = (NativeArrayBlock*)block;
|
||||
_data = (T*)header->Data;
|
||||
_length = checked((int)header->Count);
|
||||
}
|
||||
|
||||
/// <summary>Whether this is the empty (never allocated, or already disposed) buffer.</summary>
|
||||
public bool IsNull() => _block == 0;
|
||||
|
||||
/// <summary>The number of elements in the buffer; 0 once it has been disposed.</summary>
|
||||
public int Length => _block == 0 ? 0 : _length;
|
||||
|
||||
/// <summary>The buffer's contents, valid until <see cref="Dispose"/>; empty afterwards.</summary>
|
||||
[return: Brutal.Attributes.Fixed]
|
||||
public unsafe ReadOnlySpan<T> AsSpan() => _block == 0 ? default : new ReadOnlySpan<T>(_data, _length);
|
||||
|
||||
/// <summary>Frees the buffer. Idempotent; the spans it handed out are dangling afterwards.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
var block = Interlocked.Exchange(ref _block, 0);
|
||||
if (block != 0)
|
||||
Jolt.PInvoke.NativeArrayDestroy(block);
|
||||
}
|
||||
}
|
||||
13
Brutal.Jolt/Manual/NativeArrayBlock.cs
Normal file
13
Brutal.Jolt/Manual/NativeArrayBlock.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
// The shim's JoltArrayBlock header (Generated/Shim/JoltInterop.h, which static_asserts this layout), read in place
|
||||
// by NativeArray and NativeHandleArray instead of a native call per field
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct NativeArrayBlock
|
||||
{
|
||||
public nint Data;
|
||||
public ulong Count;
|
||||
public nint Owner;
|
||||
public nint Destroy;
|
||||
public ulong Stride;
|
||||
}
|
||||
67
Brutal.Jolt/Manual/NativeHandleArray.cs
Normal file
67
Brutal.Jolt/Manual/NativeHandleArray.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
namespace Brutal.JoltApi;
|
||||
|
||||
/// <summary>
|
||||
/// An owned native buffer of objects Jolt stores by value and C# sees as handles - a query's
|
||||
/// <see cref="TransformedShape"/> hits, for one. Dispose it when done.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The buffer owns its elements, and whatever they reference: a <see cref="TransformedShape"/> holds a reference to
|
||||
/// its shape, so the shapes stay alive until the buffer is disposed even if the bodies they came from are gone.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The handles it hands out are borrowed - each points at an element inside the buffer, so never Dispose one, and
|
||||
/// don't use one after the buffer is disposed.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// There is no finalizer, so an undisposed buffer leaks rather than being freed behind a handle that's still in
|
||||
/// use. Dispose every one, typically with <c>using</c>, and before <see cref="Jolt.UnregisterTypes"/> /
|
||||
/// <see cref="Factory.DestroyInstance"/>, since the block is freed through Jolt's allocator.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class NativeHandleArray<T> : IDisposable, INullable
|
||||
where T : unmanaged
|
||||
{
|
||||
private nint _block;
|
||||
|
||||
// Read once: data, count and stride are fixed for the life of the block
|
||||
private readonly nint _data;
|
||||
private readonly int _length;
|
||||
private readonly int _stride;
|
||||
|
||||
internal unsafe NativeHandleArray(nint block)
|
||||
{
|
||||
_block = block;
|
||||
if (block == 0)
|
||||
return;
|
||||
|
||||
var header = (NativeArrayBlock*)block;
|
||||
_data = header->Data;
|
||||
_length = checked((int)header->Count);
|
||||
_stride = checked((int)header->Stride);
|
||||
}
|
||||
|
||||
/// <summary>Whether this is the empty (never allocated, or already disposed) buffer.</summary>
|
||||
public bool IsNull() => _block == 0;
|
||||
|
||||
/// <summary>The number of elements; 0 once the buffer has been disposed.</summary>
|
||||
public int Length => _block == 0 ? 0 : _length;
|
||||
|
||||
/// <summary>A borrowed handle onto element <paramref name="index"/>.</summary>
|
||||
/// <exception cref="ArgumentOutOfRangeException">The index is outside the buffer.</exception>
|
||||
public T this[int index] => AsSpan()[index];
|
||||
|
||||
/// <summary>The elements as borrowed handles, valid until <see cref="Dispose"/>; empty afterwards.</summary>
|
||||
public NativeHandleSpan<T> AsSpan() => _block == 0 ? default : new NativeHandleSpan<T>(_data, _length, _stride);
|
||||
|
||||
/// <summary>Walks the elements in order, as borrowed handles.</summary>
|
||||
public NativeHandleSpan<T>.Enumerator GetEnumerator() => AsSpan().GetEnumerator();
|
||||
|
||||
/// <summary>Frees the buffer and releases what its elements reference. Idempotent.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
var block = Interlocked.Exchange(ref _block, 0);
|
||||
if (block != 0)
|
||||
Jolt.PInvoke.NativeArrayDestroy(block);
|
||||
}
|
||||
}
|
||||
105
Brutal.Jolt/Manual/NativeHandleSpan.cs
Normal file
105
Brutal.Jolt/Manual/NativeHandleSpan.cs
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Brutal.JoltApi;
|
||||
|
||||
/// <summary>
|
||||
/// A borrowed view over a native container that stores <typeparamref name="T"/> <em>by value</em>, handing
|
||||
/// out one borrowed handle per element.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Jolt stores some containers as whole objects laid end to end (<c>PhysicsScene</c>'s body settings, for
|
||||
/// instance), while C# only ever sees those types as handles - an address. A <see cref="Span{T}"/> of
|
||||
/// handles would be wrong, because the memory holds N objects of the native element size rather than N
|
||||
/// pointers, and a copied buffer would be worse: an edit through a copy never reaches the container. So
|
||||
/// this type keeps the storage base together with the element stride the native side measured, and
|
||||
/// <see cref="this[int]"/> forms the handle for element <c>i</c> as <c>base + i * stride</c>. Reading or
|
||||
/// writing through that handle touches the owner's own element.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Every handle it hands out is <em>borrowed</em>: it belongs to the container, so it must never be
|
||||
/// disposed. <c>Dispose</c> on one of these handles is a native <c>delete</c>, and the pointer is an
|
||||
/// element <em>inside</em> the container's own allocation rather than an object of its own, so deleting it
|
||||
/// corrupts the heap. Never write <c>using var settings = scene.GetBodies()[0];</c> and never pass one to
|
||||
/// anything that takes ownership - the same rule every borrowed handle in this binding carries
|
||||
/// (<see cref="NativeArray{T}"/>, <c>BodyManager.GetBodies()</c>, and every getter whose doc says "the
|
||||
/// returned handle is borrowed from its owner").
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// A handle also dangles the moment the container is resized (anything that adds or removes elements) or
|
||||
/// its owner is destroyed. The view itself has the same lifetime: being a <c>ref struct</c> keeps it off
|
||||
/// the heap and out of closures, but the compiler can't prove the owner outlives the stack frame, so treat
|
||||
/// it exactly like a <c>[Fixed]</c> span - read it inside the scope that produced it, and take it again
|
||||
/// after anything that could have grown the container rather than reusing the old one.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public readonly ref struct NativeHandleSpan<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
private readonly nint _data;
|
||||
private readonly int _count;
|
||||
private readonly int _stride;
|
||||
|
||||
internal NativeHandleSpan(NativeStridedView view)
|
||||
{
|
||||
_data = view.Data;
|
||||
_count = view.Data == 0 ? 0 : checked((int)view.Count);
|
||||
_stride = checked((int)view.Stride);
|
||||
}
|
||||
|
||||
// A view of an owned NativeHandleArray's storage
|
||||
internal NativeHandleSpan(nint data, int count, int stride)
|
||||
{
|
||||
_data = data;
|
||||
_count = data == 0 ? 0 : count;
|
||||
_stride = stride;
|
||||
}
|
||||
|
||||
/// <summary>The number of elements the container holds.</summary>
|
||||
public int Count => _count;
|
||||
|
||||
/// <summary>Whether the container is empty (or the view was never filled).</summary>
|
||||
public bool IsEmpty => _count == 0;
|
||||
|
||||
/// <summary>
|
||||
/// A borrowed handle onto element <paramref name="index"/> of the container's own storage. Writing
|
||||
/// through it edits the container; never Dispose it - the handle addresses an element inside the
|
||||
/// container's allocation, so deleting it corrupts the heap.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentOutOfRangeException">The index is outside the container.</exception>
|
||||
public T this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((uint)index >= (uint)_count)
|
||||
throw new ArgumentOutOfRangeException(nameof(index), index, $"Expected an index below {_count}.");
|
||||
// Every handle struct is a single nint, so the bit cast is the whole conversion
|
||||
return Unsafe.BitCast<nint, T>(_data + (nint)index * _stride);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Walks the elements in order, yielding the same borrowed handles as <see cref="this[int]"/>.</summary>
|
||||
public Enumerator GetEnumerator() => new(this);
|
||||
|
||||
/// <summary>
|
||||
/// Enumerates a <see cref="NativeHandleSpan{T}"/>. Each <see cref="Current"/> is the same borrowed
|
||||
/// handle the indexer gives: never Dispose one, so never <c>foreach</c> with a <c>using</c> body.
|
||||
/// </summary>
|
||||
public ref struct Enumerator
|
||||
{
|
||||
private readonly NativeHandleSpan<T> _view;
|
||||
private int _index;
|
||||
|
||||
internal Enumerator(NativeHandleSpan<T> view)
|
||||
{
|
||||
_view = view;
|
||||
_index = -1;
|
||||
}
|
||||
|
||||
/// <summary>The borrowed handle at the current position; never Dispose it.</summary>
|
||||
public readonly T Current => _view[_index];
|
||||
|
||||
/// <summary>Advances to the next element; false at the end.</summary>
|
||||
public bool MoveNext() => ++_index < _view.Count;
|
||||
}
|
||||
}
|
||||
42
Brutal.Jolt/Manual/PInvoke.BodyAddBatch.cs
Normal file
42
Brutal.Jolt/Manual/PInvoke.BodyAddBatch.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Brutal.JoltApi;
|
||||
|
||||
// Hand-written LibraryImports for the batch body add (JoltBodyAddBatchShim.cpp).
|
||||
// PrepareAddBodies is on BodyInterface; Commit and Destroy are free functions.
|
||||
public static partial class Jolt
|
||||
{
|
||||
public static unsafe partial class PInvoke
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Starts adding many created bodies to the physics system at once, which is much cheaper than adding them
|
||||
/// one at a time. The IDs are copied, so bodyIDs can be reused straight away. Preparing touches nothing the
|
||||
/// simulation reads, so it can run on a background thread; BodyAddBatch.Commit then adds them all under one
|
||||
/// lock. Bodies must have been created (CreateBody) and not added yet.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "BodyInterface_PrepareAddBodies")]
|
||||
public static partial nint BodyInterface_PrepareAddBodies(
|
||||
BodyInterface self,
|
||||
uint* bodyIDs,
|
||||
nuint bodyIDsCount
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Finalizes a prepared batch add; a batch already committed is left alone.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_BodyAddBatchCommit")]
|
||||
public static partial void BodyAddBatchCommit(nint batch, Activation activation);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Frees a batch add, aborting it first when it was never committed.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_BodyAddBatchDestroy")]
|
||||
public static partial void BodyAddBatchDestroy(nint batch);
|
||||
}
|
||||
}
|
||||
45
Brutal.Jolt/Manual/PInvoke.ClosestPoint.cs
Normal file
45
Brutal.Jolt/Manual/PInvoke.ClosestPoint.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using Brutal.Numerics;
|
||||
|
||||
namespace Brutal.JoltApi;
|
||||
|
||||
// Hand-written LibraryImports for JPH::ClosestPoint free functions (JoltClosestPointShim.cpp).
|
||||
// These are templates the parse can't bind.
|
||||
public static partial class Jolt
|
||||
{
|
||||
public static unsafe partial class PInvoke
|
||||
{
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ClosestPointGetBaryCentricCoordinates")]
|
||||
public static partial Brutal.Bool8 ClosestPointGetBaryCentricCoordinates(
|
||||
float3 a,
|
||||
float3 b,
|
||||
out float u,
|
||||
out float v
|
||||
);
|
||||
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ClosestPointGetBaryCentricCoordinates_2")]
|
||||
public static partial Brutal.Bool8 ClosestPointGetBaryCentricCoordinates_2(
|
||||
float3 a,
|
||||
float3 b,
|
||||
float3 c,
|
||||
out float u,
|
||||
out float v,
|
||||
out float w
|
||||
);
|
||||
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ClosestPointGetClosestPointOnLine")]
|
||||
public static partial float3 ClosestPointGetClosestPointOnLine(float3 a, float3 b, out uint set);
|
||||
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ClosestPointGetClosestPointOnTriangle")]
|
||||
public static partial float3 ClosestPointGetClosestPointOnTriangle(float3 a, float3 b, float3 c, out uint set);
|
||||
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ClosestPointGetClosestPointOnTetrahedron")]
|
||||
public static partial float3 ClosestPointGetClosestPointOnTetrahedron(
|
||||
float3 a,
|
||||
float3 b,
|
||||
float3 c,
|
||||
float3 d,
|
||||
out uint set
|
||||
);
|
||||
}
|
||||
}
|
||||
80
Brutal.Jolt/Manual/PInvoke.ContactEvents.cs
Normal file
80
Brutal.Jolt/Manual/PInvoke.ContactEvents.cs
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Brutal.JoltApi;
|
||||
|
||||
// Hand-written LibraryImports for the contact event listener (JoltContactEventsShim.cpp).
|
||||
// These are fixed-shape, not derived from the parse.
|
||||
public static partial class Jolt
|
||||
{
|
||||
public static unsafe partial class PInvoke
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Creates a buffering contact listener holding up to capacity events of the kinds in mask.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ContactEventListenerCreate")]
|
||||
public static partial nint ContactEventListenerCreate(uint capacity, uint mask);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Frees a buffering contact listener. It must not be installed on a physics system.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ContactEventListenerDestroy")]
|
||||
public static partial void ContactEventListenerDestroy(nint listener);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// The listener as the ContactListener PhysicsSystem.SetContactListener takes; borrowed from it.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ContactEventListenerGetBase")]
|
||||
public static partial ContactListener ContactEventListenerGetBase(nint listener);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Copies up to capacity pending events into events, oldest first, and returns how many were copied.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ContactEventListenerDrain")]
|
||||
public static partial uint ContactEventListenerDrain(
|
||||
nint listener,
|
||||
ContactEvent* events,
|
||||
uint capacity,
|
||||
uint* remaining
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Throws away every pending event.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ContactEventListenerClear")]
|
||||
public static partial void ContactEventListenerClear(nint listener);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// How many events are waiting to be drained.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ContactEventListenerGetPending")]
|
||||
public static partial uint ContactEventListenerGetPending(nint listener);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// How many events were dropped over the listener's life because the buffer was full.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ContactEventListenerGetDropped")]
|
||||
public static partial uint ContactEventListenerGetDropped(nint listener);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// How many events the buffer holds.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_ContactEventListenerGetCapacity")]
|
||||
public static partial uint ContactEventListenerGetCapacity(nint listener);
|
||||
}
|
||||
}
|
||||
23
Brutal.Jolt/Manual/PInvoke.PenetrationDepth.cs
Normal file
23
Brutal.Jolt/Manual/PInvoke.PenetrationDepth.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using Brutal.Numerics;
|
||||
|
||||
namespace Brutal.JoltApi;
|
||||
|
||||
// Hand-written LibraryImport for the GJK/EPA penetration depth test (JoltPenetrationDepthShim.cpp).
|
||||
// EPAPenetrationDepth is a template the parse can't bind.
|
||||
public static partial class Jolt
|
||||
{
|
||||
public static unsafe partial class PInvoke
|
||||
{
|
||||
[LibraryImport("jolt", EntryPoint = "Jolt_GetPenetrationDepth")]
|
||||
public static partial Brutal.Bool8 GetPenetrationDepth(
|
||||
ref ConvexObject a,
|
||||
ref ConvexObject b,
|
||||
float collisionToleranceSq,
|
||||
float penetrationTolerance,
|
||||
ref float3 v,
|
||||
out float3 pointA,
|
||||
out float3 pointB
|
||||
);
|
||||
}
|
||||
}
|
||||
44
Brutal.Jolt/Manual/PenetrationDepth.cs
Normal file
44
Brutal.Jolt/Manual/PenetrationDepth.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using Brutal.Numerics;
|
||||
|
||||
namespace Brutal.JoltApi;
|
||||
|
||||
// Hand-written wrapper for GetPenetrationDepth (EPAPenetrationDepth is a template the parse can't bind).
|
||||
public static partial class Jolt
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Whether two convex objects intersect, by GJK and then EPA when GJK can't tell, and if so the
|
||||
/// penetration: v is the separating direction (initialise it to any non-zero guess, it is refined in
|
||||
/// place), pointA and pointB the deepest points on each (zero when they don't intersect).
|
||||
/// collisionToleranceSq is the squared distance counted as touching, penetrationTolerance the EPA
|
||||
/// convergence tolerance (at least FLT_EPSILON, 1.1920929E-07; C#'s float.Epsilon is far smaller and
|
||||
/// throws).
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static bool GetPenetrationDepth(
|
||||
ConvexObject a,
|
||||
ConvexObject b,
|
||||
float collisionToleranceSq,
|
||||
float penetrationTolerance,
|
||||
ref float3 v,
|
||||
out float3 pointA,
|
||||
out float3 pointB
|
||||
)
|
||||
{
|
||||
if (!(penetrationTolerance >= 1.1920929E-07f))
|
||||
throw new ArgumentOutOfRangeException(
|
||||
nameof(penetrationTolerance),
|
||||
penetrationTolerance,
|
||||
"Expected at least FLT_EPSILON (1.1920929E-07) - float.Epsilon is far smaller."
|
||||
);
|
||||
return Jolt.PInvoke.GetPenetrationDepth(
|
||||
ref a,
|
||||
ref b,
|
||||
collisionToleranceSq,
|
||||
penetrationTolerance,
|
||||
ref v,
|
||||
out pointA,
|
||||
out pointB
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue