Fixed auto mass serialisation and colliders not recomputing mass on size change

This commit is contained in:
Diren D Bharwani 2023-03-04 20:28:09 +08:00
parent 9b2c5a1804
commit 02b21f2694
7 changed files with 54 additions and 21 deletions

View File

@ -50,8 +50,8 @@
IsActive: true
RigidBody Component:
Type: Dynamic
Auto Mass: false
Mass: 1
Auto Mass: false
Drag: 0.00999999978
Angular Drag: 0.00999999978
Use Gravity: true
@ -188,8 +188,8 @@
IsActive: true
RigidBody Component:
Type: Dynamic
Auto Mass: false
Mass: 1
Auto Mass: false
Drag: 0.00999999978
Angular Drag: 0.00999999978
Use Gravity: true
@ -228,8 +228,8 @@
IsActive: true
RigidBody Component:
Type: Dynamic
Auto Mass: false
Mass: 1
Auto Mass: false
Drag: 0.00999999978
Angular Drag: 0.00999999978
Use Gravity: true
@ -268,8 +268,8 @@
IsActive: true
RigidBody Component:
Type: Dynamic
Auto Mass: false
Mass: 1
Auto Mass: false
Drag: 0.00999999978
Angular Drag: 0.00999999978
Use Gravity: true
@ -308,8 +308,8 @@
IsActive: true
RigidBody Component:
Type: Dynamic
Auto Mass: false
Mass: 1
Auto Mass: false
Drag: 0.00999999978
Angular Drag: 0.00999999978
Use Gravity: true
@ -328,7 +328,7 @@
- Is Trigger: false
Collision Tag: 0
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Half Extents: {x: 1, y: 2, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1

View File

@ -326,6 +326,17 @@ namespace SHADE
shape->Update();
}
void SHCollider::UpdateBroadphase() noexcept
{
flags |= MOVED_FLAG;
}
void SHCollider::UpdateBody() noexcept
{
if (rigidBody)
rigidBody->ComputeMassData();
}
/*-----------------------------------------------------------------------------------*/
/* Private Member Function Definitions */
/*-----------------------------------------------------------------------------------*/

View File

@ -149,6 +149,18 @@ namespace SHADE
*/
void Update () noexcept;
/**
* @brief
* Updates the broadphase proxy of this collider.
*/
void UpdateBroadphase() noexcept;
/**
* @brief
* Recomputes the mass of the body if one exists.
*/
void UpdateBody() noexcept;
protected:
/*---------------------------------------------------------------------------------*/
/* Data Members */

View File

@ -207,9 +207,11 @@ namespace SHADE
// Recompute Relative radius
relativeExtents = 2.0f * Extents / scale;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
{
collider->UpdateBroadphase();
collider->UpdateBody();
}
}
void SHBox::SetRelativeExtents(const SHVec3& newRelativeExtents) noexcept
@ -219,9 +221,11 @@ namespace SHADE
// Recompute world radius
Extents = relativeExtents * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
{
collider->UpdateBroadphase();
collider->UpdateBody();
}
}
void SHBox::SetScale(const SHVec3& newScale) noexcept
@ -231,9 +235,11 @@ namespace SHADE
// Recompute world radius
Extents = relativeExtents * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
{
collider->UpdateBroadphase();
collider->UpdateBody();
}
}
/*-----------------------------------------------------------------------------------*/

View File

@ -168,9 +168,11 @@ namespace SHADE
// Recompute Relative radius
relativeRadius = 2.0f * Radius / scale;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
{
collider->UpdateBroadphase();
collider->UpdateBody();
}
}
void SHSphere::SetRelativeRadius(float newRelativeRadius) noexcept
@ -180,9 +182,11 @@ namespace SHADE
// Recompute world radius
Radius = relativeRadius * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
{
collider->UpdateBroadphase();
collider->UpdateBody();
}
}
void SHSphere::SetScale(float maxScale) noexcept
@ -192,9 +196,11 @@ namespace SHADE
// Recompute world radius
Radius = relativeRadius * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider)
collider->SetScale(collider->GetScale());
{
collider->UpdateBroadphase();
collider->UpdateBody();
}
}
/*-----------------------------------------------------------------------------------*/

View File

@ -40,8 +40,6 @@ namespace SHADE
flags |= 1U << 0; // Body is active
flags |= 1U << 2; // Sleeping is enabled
flags |= 1U << 3; // Gravity is enabled
// TODO: Compute inertia if body is dynamic
}
SHRigidBody::SHRigidBody(const SHRigidBody& rhs) noexcept

View File

@ -360,8 +360,8 @@ RTTR_REGISTRATION
registration::class_<SHRigidBodyComponent>("RigidBody Component")
.property("Type" , &SHRigidBodyComponent::GetType , &SHRigidBodyComponent::SetType )
.property("Auto Mass" , &SHRigidBodyComponent::GetAutoMass , &SHRigidBodyComponent::SetAutoMass )
.property("Mass" , &SHRigidBodyComponent::GetMass , &SHRigidBodyComponent::SetMass )
.property("Auto Mass" , &SHRigidBodyComponent::GetAutoMass , &SHRigidBodyComponent::SetAutoMass )
.property("Drag" , &SHRigidBodyComponent::GetDrag , &SHRigidBodyComponent::SetDrag )
.property("Angular Drag" , &SHRigidBodyComponent::GetAngularDrag , &SHRigidBodyComponent::SetAngularDrag )
.property("Use Gravity" , &SHRigidBodyComponent::IsGravityEnabled , &SHRigidBodyComponent::SetIsGravityEnabled )