Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Prowl.Runtime/Components/Physics/Colliders/BoxCollider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public Float3 Size
}

public override RigidBodyShape[] CreateShapes() => [new BoxShape(Maths.Max(size.X, 0.01f), Maths.Max(size.Y, 0.01f), Maths.Max(size.Z, 0.01f))];

public override void DrawGizmos()
{
Float4x4 matrix = Float4x4.CreateTRS(Transform.Position, Transform.Rotation * Quaternion.FromEuler(Rotation), Transform.LossyScale);
Debug.PushMatrix(matrix);
Debug.DrawWireCube(Center, size * 0.5f, Color.Green);
Debug.PopMatrix();
}
}
8 changes: 8 additions & 0 deletions Prowl.Runtime/Components/Physics/Colliders/CapsuleCollider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public float Height
}

public override RigidBodyShape[] CreateShapes() => [new CapsuleShape(Maths.Max(radius, 0.01f), Maths.Max(height, 0.01f))];

public override void DrawGizmos()
{
Float4x4 matrix = Float4x4.CreateTRS(Transform.Position, Transform.Rotation * Quaternion.FromEuler(Rotation), Transform.LossyScale);
Debug.PushMatrix(matrix);
Debug.DrawWireCapsule(Center + new Float3(0, -height * 0.5f, 0), Center + new Float3(0, height * 0.5f, 0), radius, Color.Green);
Debug.PopMatrix();
}
}
8 changes: 8 additions & 0 deletions Prowl.Runtime/Components/Physics/Colliders/ConeCollider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public float Height
}

public override RigidBodyShape[] CreateShapes() => [new ConeShape(Maths.Max(radius, 0.01f), Maths.Max(height, 0.01f))];

public override void DrawGizmos()
{
Float4x4 matrix = Float4x4.CreateTRS(Transform.Position, Transform.Rotation * Quaternion.FromEuler(Rotation), Transform.LossyScale);
Debug.PushMatrix(matrix);
Debug.DrawWireCone(Center + new Float3(0, -height * 0.5f, 0), new Float3(0, height, 0), radius, Color.Green);
Debug.PopMatrix();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public float Height
}

public override RigidBodyShape[] CreateShapes() => [new CylinderShape(Maths.Max(radius, 0.01f), Maths.Max(height, 0.01f))];

public override void DrawGizmos()
{
Float4x4 matrix = Float4x4.CreateTRS(Transform.Position, Transform.Rotation * Quaternion.FromEuler(Rotation), Transform.LossyScale);
Debug.PushMatrix(matrix);
Debug.DrawWireCylinder(Center, Quaternion.Identity, radius, height, Color.Green);
Debug.PopMatrix();
}
}
85 changes: 85 additions & 0 deletions Prowl.Runtime/Components/Physics/Colliders/MeshCollider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using Prowl.Echo;
using Prowl.Runtime.Resources;
using Prowl.Vector;

namespace Prowl.Runtime;

Expand Down Expand Up @@ -45,6 +46,10 @@ public bool Convex
}
}

// Cached convex hull shape and its tessellation for gizmo drawing — rebuilt when mesh or convex flag changes.
[SerializeIgnore] private ConvexHullShape? _cachedConvexShape;
[SerializeIgnore] private List<JTriangle>? _cachedHullTris;

public override RigidBodyShape[] CreateShapes()
{
var m = mesh.Res;
Expand Down Expand Up @@ -82,6 +87,14 @@ public override RigidBodyShape[] CreateShapes()
}
}

public override void OnValidate()
{
_cachedConvexShape = null;
_cachedHullTris = null;
base.OnValidate();
Debug.Log("OnInvalidate called");
}

public override void OnEnable()
{
base.OnEnable();
Expand All @@ -96,6 +109,78 @@ public override void OnEnable()
}
}

public override void DrawGizmos()
{
var m = mesh.Res;
if (m == null)
{
var mr = GetComponent<MeshRenderer>();
if (mr != null) m = mr.Mesh.Res;
}
if (m == null) return;

Float4x4 matrix = Float4x4.CreateTRS(Transform.Position, Transform.Rotation * Quaternion.FromEuler(Rotation), Transform.LossyScale);
Debug.PushMatrix(matrix);

if (convex)
{
DrawConvexHullGizmo(m);
}
else
{
DrawMeshWireframeGizmo(m);
}

Debug.PopMatrix();
}

private void DrawMeshWireframeGizmo(Mesh m)
{
Float3[] vertices = m.Vertices;
uint[] indices = m.Indices;
if (vertices == null || indices == null) return;

for (int i = 0; i + 2 < indices.Length; i += 3)
{
uint i0 = indices[i], i1 = indices[i + 1], i2 = indices[i + 2];
if (i0 >= vertices.Length || i1 >= vertices.Length || i2 >= vertices.Length)
continue;

Float3 v0 = vertices[i0] + Center;
Float3 v1 = vertices[i1] + Center;
Float3 v2 = vertices[i2] + Center;

Debug.DrawLine(v0, v1, Color.Green);
Debug.DrawLine(v1, v2, Color.Green);
Debug.DrawLine(v2, v0, Color.Green);
}
}

private void DrawConvexHullGizmo(Mesh m)
{
if (_cachedConvexShape == null)
{
var triangles = ToTriangleList(m);
if (triangles.Count == 0) return;
_cachedConvexShape = new ConvexHullShape(triangles);
}

_cachedHullTris ??= ShapeHelper.Tessellate(_cachedConvexShape, 2);
JVector shift = _cachedConvexShape.Shift;

foreach (JTriangle tri in _cachedHullTris)
{
// Hull vertices are CoM-centered; add Shift to convert back to mesh-local space.
Float3 a = new Float3(tri.V0.X + shift.X, tri.V0.Y + shift.Y, tri.V0.Z + shift.Z) + Center;
Float3 b = new Float3(tri.V1.X + shift.X, tri.V1.Y + shift.Y, tri.V1.Z + shift.Z) + Center;
Float3 c = new Float3(tri.V2.X + shift.X, tri.V2.Y + shift.Y, tri.V2.Z + shift.Z) + Center;

Debug.DrawLine(a, b, Color.Green);
Debug.DrawLine(b, c, Color.Green);
Debug.DrawLine(c, a, Color.Green);
}
}

private static List<JTriangle> ToTriangleList(Mesh mesh)
{
var vertices = mesh.Vertices;
Expand Down
8 changes: 8 additions & 0 deletions Prowl.Runtime/Components/Physics/Colliders/SphereCollider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ public float Radius
}

public override RigidBodyShape[] CreateShapes() => [new SphereShape(Maths.Max(radius, 0.01f))];

public override void DrawGizmos()
{
Float4x4 matrix = Float4x4.CreateTRS(Transform.Position, Transform.Rotation * Quaternion.FromEuler(Rotation), Transform.LossyScale);
Debug.PushMatrix(matrix);
Debug.DrawWireSphere(Center, radius, Color.Green);
Debug.PopMatrix();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,7 @@ public override void OnValidate()

public override void DrawGizmos()
{
Constraint constraint = GetConstraint();
if (constraint != null && GameObject?.Scene?.Physics?.World != null)
{
try
{
constraint.DebugDraw(JitterGizmosDrawer.Instance);
}
catch (NotImplementedException)
{ }
}
// TODO DrawGizmos
}

/// <summary>
Expand Down
10 changes: 1 addition & 9 deletions Prowl.Runtime/Components/Physics/Constraints/PhysicsJoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ protected override void DestroyConstraint()

public override void DrawGizmos()
{
if (joint != null && GameObject?.Scene?.Physics?.World != null)
{
try
{
joint.DebugDraw(JitterGizmosDrawer.Instance);
}
catch (NotImplementedException)
{ }
}
// TODO DrawGizmos
}
}
4 changes: 1 addition & 3 deletions Prowl.Runtime/Components/Physics/Rigidbody3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,7 @@ public override void FixedUpdate()

public override void DrawGizmos()
{
if (_body == null || _body.Handle.IsZero) return;

_body.DebugDraw(JitterGizmosDrawer.Instance);
// TODO DrawGizmos
}

public override void OnEnable()
Expand Down
40 changes: 0 additions & 40 deletions Prowl.Runtime/Physics/JitterGizmosDrawer.cs

This file was deleted.

Loading