Skip to content

Commit

Permalink
Update to latest native, add more bindings, add ApplyBuoyancyImpulse …
Browse files Browse the repository at this point in the history
…and DebugRenderer (WIP), bump version to 2.9.1
  • Loading branch information
amerkoleci committed Sep 16, 2024
1 parent 7987b36 commit 6236bfc
Show file tree
Hide file tree
Showing 23 changed files with 517 additions and 62 deletions.
Binary file modified native/android-arm/libjoltc.so
Binary file not shown.
Binary file modified native/android-arm64/libjoltc.so
Binary file not shown.
Binary file modified native/android-x64/libjoltc.so
Binary file not shown.
Binary file modified native/linux-arm64/libjoltc.so
Binary file not shown.
Binary file modified native/linux-x64/libjoltc.so
Binary file not shown.
Binary file modified native/osx/libjoltc.dylib
Binary file not shown.
Binary file modified native/win-arm64/joltc.dll
Binary file not shown.
Binary file modified native/win-arm64/joltc_double.dll
Binary file not shown.
Binary file modified native/win-x64/joltc.dll
Binary file not shown.
Binary file modified native/win-x64/joltc_double.dll
Binary file not shown.
52 changes: 52 additions & 0 deletions src/JoltPhysicsSharp/Body.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,58 @@ public void AddAngularImpulse(in Vector3 angularImpulse)
JPH_Body_AddAngularImpulse(Handle, angularImpulse);
}

public void MoveKinematic(in Vector3 targetPosition, in Quaternion targetRotation, float deltaTime)
{
if (DoublePrecision)
throw new InvalidOperationException($"Double precision is enabled: use {nameof(MoveKinematic)}");

JPH_Body_MoveKinematic(Handle, in targetPosition, in targetRotation, deltaTime);
}

public void MoveKinematic(in Double3 targetPosition, in Quaternion targetRotation, float deltaTime)
{
if (!DoublePrecision)
throw new InvalidOperationException($"Double precision is disabled: use {nameof(MoveKinematic)}");

JPH_Body_MoveKinematic(Handle, in targetPosition, in targetRotation, deltaTime);
}

public bool ApplyBuoyancyImpulse(
in Vector3 surfacePosition,
in Vector3 surfaceNormal,
float buoyancy, float linearDrag, float angularDrag,
in Vector3 fluidVelocity,
in Vector3 gravity,
float deltaTime)
{
if (DoublePrecision)
throw new InvalidOperationException($"Double precision is enabled: use {nameof(ApplyBuoyancyImpulse)}");

return JPH_Body_ApplyBuoyancyImpulse(Handle, in surfacePosition, in surfaceNormal,
buoyancy, linearDrag, angularDrag,
in fluidVelocity,
in gravity,
deltaTime);
}

public bool ApplyBuoyancyImpulse(
in Double3 surfacePosition,
in Vector3 surfaceNormal,
float buoyancy, float linearDrag, float angularDrag,
in Vector3 fluidVelocity,
in Vector3 gravity,
float deltaTime)
{
if (!DoublePrecision)
throw new InvalidOperationException($"Double precision is disabled: use {nameof(ApplyBuoyancyImpulse)}");

return JPH_Body_ApplyBuoyancyImpulse(Handle, in surfacePosition, in surfaceNormal,
buoyancy, linearDrag, angularDrag,
in fluidVelocity,
in gravity,
deltaTime);
}

public ulong GetUserData()
{
return JPH_Body_GetUserData(Handle);
Expand Down
44 changes: 12 additions & 32 deletions src/JoltPhysicsSharp/BodyInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,23 +488,15 @@ public void MoveKinematic(in BodyID bodyId, in Vector3 targetPosition, in Quater
if (DoublePrecision)
throw new InvalidOperationException($"Double precision is enabled: use {nameof(MoveKinematic)}");

fixed (Vector3* targetPositionPtr = &targetPosition)
fixed (Quaternion* targetRotationPtr = &targetRotation)
{
JPH_BodyInterface_MoveKinematic(Handle, bodyId, targetPositionPtr, targetRotationPtr, deltaTime);
}
JPH_BodyInterface_MoveKinematic(Handle, bodyId, in targetPosition, in targetRotation, deltaTime);
}

public void MoveKinematic(in BodyID bodyId, in Double3 targetPosition, in Quaternion targetRotation, float deltaTime)
{
if (!DoublePrecision)
throw new InvalidOperationException($"Double precision is disabled: use {nameof(MoveKinematic)}");

fixed (Double3* targetPositionPtr = &targetPosition)
fixed (Quaternion* targetRotationPtr = &targetRotation)
{
JPH_BodyInterface_MoveKinematicDouble(Handle, bodyId, targetPositionPtr, targetRotationPtr, deltaTime);
}
JPH_BodyInterface_MoveKinematic(Handle, bodyId, in targetPosition, in targetRotation, deltaTime);
}

public bool ApplyBuoyancyImpulse(
Expand All @@ -519,17 +511,11 @@ public bool ApplyBuoyancyImpulse(
if (DoublePrecision)
throw new InvalidOperationException($"Double precision is enabled: use {nameof(ApplyBuoyancyImpulse)}");

fixed (Vector3* surfacePositionPtr = &surfacePosition)
fixed (Vector3* surfaceNormalPtr = &surfaceNormal)
fixed (Vector3* fluidVelocityPtr = &fluidVelocity)
fixed (Vector3* gravityPtr = &gravity)
{
return JPH_BodyInterface_ApplyBuoyancyImpulse(Handle, bodyId, surfacePositionPtr, surfaceNormalPtr,
buoyancy, linearDrag, angularDrag,
fluidVelocityPtr,
gravityPtr,
deltaTime);
}
return JPH_BodyInterface_ApplyBuoyancyImpulse(Handle, bodyId, in surfacePosition, in surfaceNormal,
buoyancy, linearDrag, angularDrag,
in fluidVelocity,
in gravity,
deltaTime);
}

public bool ApplyBuoyancyImpulse(
Expand All @@ -544,17 +530,11 @@ public bool ApplyBuoyancyImpulse(
if (!DoublePrecision)
throw new InvalidOperationException($"Double precision is disabled: use {nameof(ApplyBuoyancyImpulse)}");

fixed (Double3* surfacePositionPtr = &surfacePosition)
fixed (Vector3* surfaceNormalPtr = &surfaceNormal)
fixed (Vector3* fluidVelocityPtr = &fluidVelocity)
fixed (Vector3* gravityPtr = &gravity)
{
return JPH_BodyInterface_ApplyBuoyancyImpulseDouble(Handle, bodyId, surfacePositionPtr, surfaceNormalPtr,
buoyancy, linearDrag, angularDrag,
fluidVelocityPtr,
gravityPtr,
deltaTime);
}
return JPH_BodyInterface_ApplyBuoyancyImpulse(Handle, bodyId, in surfacePosition, in surfaceNormal,
buoyancy, linearDrag, angularDrag,
in fluidVelocity,
in gravity,
deltaTime);
}

public void SetLinearAndAngularVelocity(in BodyID bodyId, in Vector3 linearVelocity, in Vector3 angularVelocity)
Expand Down
101 changes: 101 additions & 0 deletions src/JoltPhysicsSharp/DebugRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.

using System.Numerics;
using System.Runtime.InteropServices;
using static JoltPhysicsSharp.JoltApi;

namespace JoltPhysicsSharp;

public abstract class DebugRenderer : NativeObject
{
public enum CastShadow : uint
{
/// <summary>
/// This shape should cast a shadow
/// </summary>
On,
/// <summary>
/// This shape should not cast a shadow
/// </summary>
Off
}

public enum DrawMode : uint
{
/// <summary>
/// Draw as a solid shape
/// </summary>
Solid = 0,
/// <summary>
/// Draw as wireframe
/// </summary>
Wireframe = 1,
}


private readonly unsafe JPH_DebugRenderer_Procs _listener_Procs = new()
{
DrawLine = &OnDrawLine,
DrawTriangle = &OnDrawTriangle,
DrawText3D = &OnDrawText3D,
};

protected DebugRenderer()
{
nint listenerContext = DelegateProxies.CreateUserData(this, true);
Handle = JPH_DebugRenderer_Create(_listener_Procs, listenerContext);
}

protected DebugRenderer(nint handle)
: base(handle)
{
}

~DebugRenderer() => Dispose(disposing: false);

protected override void Dispose(bool disposing)
{
if (disposing)
{
JPH_DebugRenderer_Destroy(Handle);
}
}

public void NextFrame() => JPH_DebugRenderer_NextFrame(Handle);

protected abstract void DrawLine(Vector3 from, Vector3 to, uint color);

protected virtual void DrawTriangle(Vector3 v1, Vector3 v2, Vector3 v3, uint color, CastShadow castShadow = CastShadow.Off)
{

}

protected abstract void DrawText3D(Vector3 position, string? text, uint color, float height = 0.5f);

#region DebugRendererListener
[UnmanagedCallersOnly]
private static unsafe void OnDrawLine(nint context, Vector3* from, Vector3* to, uint color)
{
DebugRenderer listener = DelegateProxies.GetUserData<DebugRenderer>(context, out _);

listener.DrawLine(*from, *to, color);
}

[UnmanagedCallersOnly]
private static unsafe void OnDrawTriangle(nint context, Vector3* v1, Vector3* v2, Vector3* v3, uint color, CastShadow castShadow)
{
DebugRenderer listener = DelegateProxies.GetUserData<DebugRenderer>(context, out _);

listener.DrawTriangle(*v1, *v2, *v3, color, castShadow);
}

[UnmanagedCallersOnly]
private static unsafe void OnDrawText3D(nint context, Vector3* position, byte* textPtr, uint color, float height)
{
DebugRenderer listener = DelegateProxies.GetUserData<DebugRenderer>(context, out _);

listener.DrawText3D(*position, ConvertToManaged(textPtr), color, height);
}
#endregion
}
99 changes: 99 additions & 0 deletions src/JoltPhysicsSharp/DrawSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.

using System.Numerics;

namespace JoltPhysicsSharp;

public struct DrawSettings
{
/// <summary>
/// Draw the GetSupport() function, used for convex collision detection
/// </summary>
public Bool8 DrawGetSupportFunction = false;
/// <summary>
/// When drawing the support function, also draw which direction mapped to a specific support point
/// </summary>
public Bool8 DrawSupportDirection = false;
/// <summary>
/// Draw the faces that were found colliding during collision detection
/// </summary>
public Bool8 DrawGetSupportingFace = false;
/// <summary>
/// Draw the shapes of all bodies
/// </summary>
public Bool8 DrawShape = true;
/// <summary>
/// When mDrawShape is true and this is true, the shapes will be drawn in wireframe instead of solid.
/// </summary>
public Bool8 DrawShapeWireframe = false;
/// <summary>
/// Coloring scheme to use for shapes
/// </summary>
public ShapeColor DrawShapeColor = ShapeColor.MotionTypeColor;
/// <summary>
/// Draw a bounding box per body
/// </summary>
public Bool8 DrawBoundingBox = false;
/// <summary>
/// Draw the center of mass for each body
/// </summary>
public Bool8 DrawCenterOfMassTransform = false;
/// <summary>
/// Draw the world transform (which can be different than the center of mass) for each body
/// </summary>
public Bool8 DrawWorldTransform = false;
/// <summary>
/// Draw the velocity vector for each body
/// </summary>
public Bool8 DrawVelocity = false;
/// <summary>
/// Draw the mass and inertia (as the box equivalent) for each body
/// </summary>
public Bool8 DrawMassAndInertia = false;
/// <summary>
/// Draw stats regarding the sleeping algorithm of each body
/// </summary>
public Bool8 DrawSleepStats = false;
/// <summary>
/// Draw the vertices of soft bodies
/// </summary>
public Bool8 DrawSoftBodyVertices = false;
/// <summary>
/// Draw the velocities of the vertices of soft bodies
/// </summary>
public Bool8 DrawSoftBodyVertexVelocities = false;
/// <summary>
/// Draw the edge constraints of soft bodies
/// </summary>
public Bool8 DrawSoftBodyEdgeConstraints = false;
/// <summary>
/// Draw the bend constraints of soft bodies
/// </summary>
public Bool8 DrawSoftBodyBendConstraints = false;
/// <summary>
/// Draw the volume constraints of soft bodies
/// </summary>
public Bool8 DrawSoftBodyVolumeConstraints = false;
/// <summary>
/// Draw the skin constraints of soft bodies
/// </summary>
public Bool8 DrawSoftBodySkinConstraints = false;
/// <summary>
/// Draw the LRA constraints of soft bodies
/// </summary>
public Bool8 DrawSoftBodyLRAConstraints = false;
/// <summary>
/// Draw the predicted bounds of soft bodies
/// </summary>
public Bool8 DrawSoftBodyPredictedBounds = false;
/// <summary>
/// Coloring scheme to use for soft body constraints
/// </summary>
public SoftBodyConstraintColor DrawSoftBodyConstraintColor = SoftBodyConstraintColor.ConstraintType;

public DrawSettings()
{

}
}
8 changes: 4 additions & 4 deletions src/JoltPhysicsSharp/Foundation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ private static unsafe void OnNativeTraceCallback(byte* messagePtr)
}

[UnmanagedCallersOnly]
private static unsafe Bool8 OnNativeAssertCallback(sbyte* expressionPtr, sbyte* messagePtr, sbyte* filePtr, uint line)
private static unsafe Bool8 OnNativeAssertCallback(byte* expressionPtr, byte* messagePtr, byte* filePtr, uint line)
{
string expression = new(expressionPtr);
string message = new(messagePtr);
string file = new(filePtr);
string expression = ConvertToManaged(expressionPtr)!;
string message = ConvertToManaged(messagePtr)!;
string file = ConvertToManaged(filePtr)!;

if (s_assertCallback != null)
{
Expand Down
Loading

0 comments on commit 6236bfc

Please sign in to comment.