Add drawing RigidBody center of mass and attached colliders

#2325 #3247
This commit is contained in:
Wojtek Figat
2025-10-08 22:16:16 +02:00
parent 73976f3ed9
commit e2fc8a6283
11 changed files with 75 additions and 11 deletions

View File

@@ -338,6 +338,34 @@ void RigidBody::AddMovement(const Vector3& translation, const Quaternion& rotati
SetTransform(t); SetTransform(t);
} }
#if USE_EDITOR
#include "Engine/Debug/DebugDraw.h"
void RigidBody::OnDebugDrawSelected()
{
// Draw center of mass
if (!_centerOfMassOffset.IsZero())
{
DEBUG_DRAW_WIRE_SPHERE(BoundingSphere(GetPosition() + (GetOrientation() * (GetCenterOfMass() - GetCenterOfMassOffset())), 5.0f), Color::Red, 0, false);
}
DEBUG_DRAW_WIRE_SPHERE(BoundingSphere(GetPosition() + (GetOrientation() * GetCenterOfMass()), 2.5f), Color::Aqua, 0, false);
// Draw all attached colliders
for (Actor* child : Children)
{
const auto collider = Cast<Collider>(child);
if (collider && collider->GetAttachedRigidBody() == this)
{
collider->OnDebugDrawSelf();
}
}
Actor::OnDebugDrawSelected();
}
#endif
void RigidBody::OnCollisionEnter(const Collision& c) void RigidBody::OnCollisionEnter(const Collision& c)
{ {
CollisionEnter(c); CollisionEnter(c);

View File

@@ -487,6 +487,9 @@ public:
void Serialize(SerializeStream& stream, const void* otherObj) override; void Serialize(SerializeStream& stream, const void* otherObj) override;
void Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) override; void Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) override;
void AddMovement(const Vector3& translation, const Quaternion& rotation) override; void AddMovement(const Vector3& translation, const Quaternion& rotation) override;
#if USE_EDITOR
void OnDebugDrawSelected() override;
#endif
// [IPhysicsActor] // [IPhysicsActor]
void* GetPhysicsActor() const override; void* GetPhysicsActor() const override;

View File

@@ -111,18 +111,11 @@ namespace
} }
} }
void BoxCollider::OnDebugDrawSelected() void BoxCollider::OnDebugDrawSelf()
{ {
const Color color = Color::GreenYellow; const Color color = Color::GreenYellow;
DEBUG_DRAW_WIRE_BOX(_bounds, color * 0.3f, 0, false); DEBUG_DRAW_WIRE_BOX(_bounds, color * 0.3f, 0, false);
if (_contactOffset > 0)
{
OrientedBoundingBox contactBounds = _bounds;
contactBounds.Extents += Vector3(_contactOffset) / contactBounds.Transformation.Scale;
DEBUG_DRAW_WIRE_BOX(contactBounds, Color::Blue.AlphaMultiplied(0.2f), 0, false);
}
Vector3 corners[8]; Vector3 corners[8];
_bounds.GetCorners(corners); _bounds.GetCorners(corners);
const float margin = Math::Min(1.0f, _bounds.GetSize().MinValue() * 0.01f); const float margin = Math::Min(1.0f, _bounds.GetSize().MinValue() * 0.01f);
@@ -146,8 +139,19 @@ void BoxCollider::OnDebugDrawSelected()
{ {
DEBUG_DRAW_WIRE_BOX(_bounds, wiresColor, 0, true); DEBUG_DRAW_WIRE_BOX(_bounds, wiresColor, 0, true);
} }
}
void BoxCollider::OnDebugDrawSelected()
{
OnDebugDrawSelf();
if (_contactOffset > 0)
{
OrientedBoundingBox contactBounds = _bounds;
contactBounds.Extents += Vector3(_contactOffset) / contactBounds.Transformation.Scale;
DEBUG_DRAW_WIRE_BOX(contactBounds, Color::Blue.AlphaMultiplied(0.2f), 0, false);
}
// Base
Collider::OnDebugDrawSelected(); Collider::OnDebugDrawSelected();
} }

View File

@@ -52,6 +52,7 @@ public:
// [Collider] // [Collider]
#if USE_EDITOR #if USE_EDITOR
void OnDebugDraw() override; void OnDebugDraw() override;
void OnDebugDrawSelf() override;
void OnDebugDrawSelected() override; void OnDebugDrawSelected() override;
#endif #endif
bool IntersectsItself(const Ray& ray, Real& distance, Vector3& normal) override; bool IntersectsItself(const Ray& ray, Real& distance, Vector3& normal) override;

View File

@@ -52,6 +52,17 @@ void CapsuleCollider::DrawPhysicsDebug(RenderView& view)
DEBUG_DRAW_WIRE_CAPSULE(_transform.LocalToWorld(_center), rotation, radius, height, Color::GreenYellow * 0.8f, 0, true); DEBUG_DRAW_WIRE_CAPSULE(_transform.LocalToWorld(_center), rotation, radius, height, Color::GreenYellow * 0.8f, 0, true);
} }
void CapsuleCollider::OnDebugDrawSelf()
{
Quaternion rotation;
Quaternion::Multiply(_transform.Orientation, Quaternion::Euler(0, 90, 0), rotation);
const float minSize = 0.001f;
const float radius = Math::Max(Math::Abs(_radius) * _cachedScale, minSize);
const float height = Math::Max(Math::Abs(_height) * _cachedScale, minSize);
const Vector3 position = _transform.LocalToWorld(_center);
DEBUG_DRAW_WIRE_CAPSULE(position, rotation, radius, height, Color::GreenYellow, 0, false);
}
void CapsuleCollider::OnDebugDrawSelected() void CapsuleCollider::OnDebugDrawSelected()
{ {
Quaternion rotation; Quaternion rotation;

View File

@@ -56,6 +56,7 @@ public:
public: public:
// [Collider] // [Collider]
#if USE_EDITOR #if USE_EDITOR
void OnDebugDrawSelf() override;
void OnDebugDrawSelected() override; void OnDebugDrawSelected() override;
#endif #endif
bool IntersectsItself(const Ray& ray, Real& distance, Vector3& normal) override; bool IntersectsItself(const Ray& ray, Real& distance, Vector3& normal) override;

View File

@@ -160,6 +160,10 @@ protected:
private: private:
void OnMaterialChanged(); void OnMaterialChanged();
friend RigidBody;
#if USE_EDITOR
virtual void OnDebugDrawSelf() {}
#endif
public: public:
// [PhysicsColliderActor] // [PhysicsColliderActor]

View File

@@ -85,13 +85,18 @@ void MeshCollider::DrawPhysicsDebug(RenderView& view)
} }
} }
void MeshCollider::OnDebugDrawSelected() void MeshCollider::OnDebugDrawSelf()
{ {
if (CollisionData && CollisionData->IsLoaded()) if (CollisionData && CollisionData->IsLoaded())
{ {
const auto& debugLines = CollisionData->GetDebugLines(); const auto& debugLines = CollisionData->GetDebugLines();
DEBUG_DRAW_LINES(Span<Float3>(debugLines.Get(), debugLines.Count()), _transform.GetWorld(), Color::GreenYellow, 0, false); DEBUG_DRAW_LINES(Span<Float3>(debugLines.Get(), debugLines.Count()), _transform.GetWorld(), Color::GreenYellow, 0, false);
} }
}
void MeshCollider::OnDebugDrawSelected()
{
OnDebugDrawSelf();
// Base // Base
Collider::OnDebugDrawSelected(); Collider::OnDebugDrawSelected();

View File

@@ -31,6 +31,7 @@ public:
bool CanAttach(RigidBody* rigidBody) const override; bool CanAttach(RigidBody* rigidBody) const override;
bool CanBeTrigger() const override; bool CanBeTrigger() const override;
#if USE_EDITOR #if USE_EDITOR
void OnDebugDrawSelf() override;
void OnDebugDrawSelected() override; void OnDebugDrawSelected() override;
#endif #endif
bool IntersectsItself(const Ray& ray, Real& distance, Vector3& normal) override; bool IntersectsItself(const Ray& ray, Real& distance, Vector3& normal) override;

View File

@@ -35,9 +35,14 @@ void SphereCollider::DrawPhysicsDebug(RenderView& view)
DEBUG_DRAW_WIRE_SPHERE(_sphere, Color::GreenYellow * 0.8f, 0, true); DEBUG_DRAW_WIRE_SPHERE(_sphere, Color::GreenYellow * 0.8f, 0, true);
} }
void SphereCollider::OnDebugDrawSelected() void SphereCollider::OnDebugDrawSelf()
{ {
DEBUG_DRAW_WIRE_SPHERE(_sphere, Color::GreenYellow, 0, false); DEBUG_DRAW_WIRE_SPHERE(_sphere, Color::GreenYellow, 0, false);
}
void SphereCollider::OnDebugDrawSelected()
{
OnDebugDrawSelf();
if (_contactOffset > 0) if (_contactOffset > 0)
{ {

View File

@@ -36,6 +36,7 @@ public:
public: public:
// [Collider] // [Collider]
#if USE_EDITOR #if USE_EDITOR
void OnDebugDrawSelf() override;
void OnDebugDrawSelected() override; void OnDebugDrawSelected() override;
#endif #endif
bool IntersectsItself(const Ray& ray, Real& distance, Vector3& normal) override; bool IntersectsItself(const Ray& ray, Real& distance, Vector3& normal) override;