Optimize collider cached scale

This commit is contained in:
Wojtek Figat
2025-06-05 15:06:03 +02:00
parent ba75fd5882
commit f6feae5cf2
8 changed files with 17 additions and 23 deletions

View File

@@ -162,7 +162,7 @@ void BoxCollider::UpdateBounds()
void BoxCollider::GetGeometry(CollisionShape& collision)
{
Float3 size = _size * _cachedScale;
Float3 size = _size * _transform.Scale;
const float minSize = 0.001f;
size = Float3::Max(size.GetAbsolute() * 0.5f, Float3(minSize));
collision.SetBox(size.Raw);

View File

@@ -43,10 +43,9 @@ void CapsuleCollider::DrawPhysicsDebug(RenderView& view)
return;
Quaternion rotation;
Quaternion::Multiply(_transform.Orientation, Quaternion::Euler(0, 90, 0), rotation);
const float scaling = _cachedScale.GetAbsolute().MaxValue();
const float minSize = 0.001f;
const float radius = Math::Max(Math::Abs(_radius) * scaling, minSize);
const float height = Math::Max(Math::Abs(_height) * scaling, minSize);
const float radius = Math::Max(Math::Abs(_radius) * _cachedScale, minSize);
const float height = Math::Max(Math::Abs(_height) * _cachedScale, minSize);
if (view.Mode == ViewMode::PhysicsColliders && !GetIsTrigger())
DEBUG_DRAW_CAPSULE(_transform.LocalToWorld(_center), rotation, radius, height, _staticActor ? Color::CornflowerBlue : Color::Orchid, 0, true);
else
@@ -57,10 +56,9 @@ void CapsuleCollider::OnDebugDrawSelected()
{
Quaternion rotation;
Quaternion::Multiply(_transform.Orientation, Quaternion::Euler(0, 90, 0), rotation);
const float scaling = _cachedScale.GetAbsolute().MaxValue();
const float minSize = 0.001f;
const float radius = Math::Max(Math::Abs(_radius) * scaling, minSize);
const float height = Math::Max(Math::Abs(_height) * scaling, minSize);
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);
@@ -92,9 +90,8 @@ void CapsuleCollider::UpdateBounds()
void CapsuleCollider::GetGeometry(CollisionShape& collision)
{
const float scaling = _cachedScale.GetAbsolute().MaxValue();
const float minSize = 0.001f;
const float radius = Math::Max(Math::Abs(_radius) * scaling, minSize);
const float height = Math::Max(Math::Abs(_height) * scaling, minSize);
const float radius = Math::Max(Math::Abs(_radius) * _cachedScale, minSize);
const float height = Math::Max(Math::Abs(_height) * _cachedScale, minSize);
collision.SetCapsule(radius, height * 0.5f);
}

View File

@@ -215,8 +215,7 @@ void CharacterController::CreateController()
{
// Create controller
ASSERT(_controller == nullptr && _shape == nullptr);
_cachedScale = GetScale();
const float scaling = _cachedScale.GetAbsolute().MaxValue();
_cachedScale = GetScale().GetAbsolute().MaxValue();
const Vector3 position = _transform.LocalToWorld(_center);
_controller = PhysicsBackend::CreateController(GetPhysicsScene()->GetPhysicsScene(), this, this, _contactOffset, position, _slopeLimit, (int32)_nonWalkableMode, Material, Math::Abs(_radius) * scaling, Math::Abs(_height) * scaling, _stepOffset, _shape);
@@ -334,7 +333,7 @@ void CharacterController::UpdateGeometry()
return;
// Setup shape geometry
_cachedScale = GetScale();
_cachedScale = GetScale().GetAbsolute().MaxValue();
UpdateSize();
}
@@ -398,7 +397,7 @@ void CharacterController::OnTransformChanged()
if (!_isUpdatingTransform && _controller)
{
PhysicsBackend::SetControllerPosition(_controller, position);
const Float3 scale = GetScale();
const float scale = GetScale().GetAbsolute().MaxValue();
if (_cachedScale != scale)
UpdateGeometry();
UpdateBounds();

View File

@@ -205,7 +205,7 @@ void Collider::CreateShape()
ASSERT(_shape == nullptr);
// Setup shape geometry
_cachedScale = GetScale();
_cachedScale = GetScale().GetAbsolute().MaxValue();
CollisionShape shape;
GetGeometry(shape);
@@ -222,7 +222,7 @@ void Collider::UpdateGeometry()
return;
// Setup shape geometry
_cachedScale = GetScale();
_cachedScale = GetScale().GetAbsolute().MaxValue();
CollisionShape shape;
GetGeometry(shape);
@@ -427,7 +427,7 @@ void Collider::OnTransformChanged()
}
}
const Float3 scale = GetScale();
const float scale = GetScale().GetAbsolute().MaxValue();
if (_cachedScale != scale)
UpdateGeometry();
UpdateBounds();

View File

@@ -24,7 +24,7 @@ protected:
bool _isTrigger;
void* _shape;
void* _staticActor;
Float3 _cachedScale;
float _cachedScale;
float _contactOffset;
Vector3 _cachedLocalPosePos;
Quaternion _cachedLocalPoseRot;

View File

@@ -131,7 +131,7 @@ void MeshCollider::UpdateBounds()
void MeshCollider::GetGeometry(CollisionShape& collision)
{
// Prepare scale
Float3 scale = _cachedScale;
Float3 scale = _transform.Scale;
const float minSize = 0.001f;
Float3 scaleAbs = scale.GetAbsolute();
if (scaleAbs.X < minSize)

View File

@@ -67,8 +67,7 @@ void SphereCollider::UpdateBounds()
void SphereCollider::GetGeometry(CollisionShape& collision)
{
const float scaling = _cachedScale.GetAbsolute().MaxValue();
const float radius = Math::Abs(_radius) * scaling;
const float radius = Math::Abs(_radius) * _cachedScale;
const float minSize = 0.001f;
collision.SetSphere(Math::Max(radius, minSize));
}

View File

@@ -259,8 +259,7 @@ void SplineCollider::GetGeometry(CollisionShape& collision)
}
// Prepare scale
Float3 scale = _cachedScale;
scale = Float3::Max(scale.GetAbsolute(), minSize);
Float3 scale = Float3::Max(_transform.Scale.GetAbsolute(), minSize);
// TODO: add support for cooking collision for static splines in editor and reusing it in game