3b9762142a
Using triangle mesh with a kinematic rigid body is allowed, but the options "Compute COM", "Compute Mass" and "Compute Inertia" are not supported by PhysX and an error in logged that default values for COM, Mass and Inertia will be used. Now this situation is captured and an explanatory warning is used instead. - Improved RigidBody::UpdateMassProperties function to apply the same logic in the treatment of shapes for all three parameters: COM, Mass and Inertia. - Improved UpdateMassProperties function by using references for the override parameters instead of pointers. - Improved function that computes the Center of Mass UpdateCenterOfMass (renamed from UpdateComputedCenterOfMass), to include the same shapes that the compute mass and inertia functions in physx updateMassAndInertia, which is to include all shapes if includeAllShapesInMassCalculation is true, else include only the shapes with eSIMULATION_SHAPE flag. - Removed unused private function RigidBody::ComputeInertia. - Added unit test to check when the warnings are fired correctly when COM, Mass or Inertia are asked to be computed on a rigid body with triangle mesh shapes. - Improved MassComputeFixture tests by not only using Box shape, but also sphere and capture, plus improved the PossibleMassComputeFlags parameters to include all possible variations of the MassComputeFlags flags. Fixes #3322 Fixes #3979 Signed-off-by: moraaar <moraaar@amazon.com>
131 lines
4.9 KiB
C++
131 lines
4.9 KiB
C++
/*
|
|
* Copyright (c) Contributors to the Open 3D Engine Project.
|
|
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
*
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <PxPhysicsAPI.h>
|
|
#include <Utils.h>
|
|
#include <AzFramework/Physics/SimulatedBodies/RigidBody.h>
|
|
#include <AzFramework/Physics/Common/PhysicsTypes.h>
|
|
#include <PhysX/UserDataTypes.h>
|
|
|
|
namespace AzPhysics
|
|
{
|
|
struct RigidBodyConfiguration;
|
|
}
|
|
|
|
namespace PhysX
|
|
{
|
|
class RigidBodyComponent;
|
|
class Shape;
|
|
|
|
AZ_PUSH_DISABLE_WARNING(4996, "-Wdeprecated-declarations")
|
|
|
|
/// PhysX specific implementation of generic physics API RigidBody class.
|
|
class RigidBody
|
|
: public AzPhysics::RigidBody
|
|
{
|
|
public:
|
|
friend class RigidBodyComponent;
|
|
|
|
AZ_CLASS_ALLOCATOR(RigidBody, AZ::SystemAllocator, 0);
|
|
AZ_RTTI(PhysX::RigidBody, "{30CD41DD-9783-47A1-B935-9E5634238F45}", AzPhysics::RigidBody);
|
|
|
|
RigidBody() = default;
|
|
RigidBody(const AzPhysics::RigidBodyConfiguration& configuration);
|
|
~RigidBody();
|
|
|
|
static void Reflect(AZ::ReflectContext* context);
|
|
|
|
AZ::u32 GetShapeCount() override;
|
|
AZStd::shared_ptr<Physics::Shape> GetShape(AZ::u32 index) override;
|
|
|
|
AZ::Vector3 GetCenterOfMassWorld() const override;
|
|
AZ::Vector3 GetCenterOfMassLocal() const override;
|
|
|
|
AZ::Matrix3x3 GetInverseInertiaWorld() const override;
|
|
AZ::Matrix3x3 GetInverseInertiaLocal() const override;
|
|
|
|
float GetMass() const override;
|
|
float GetInverseMass() const override;
|
|
void SetMass(float mass) override;
|
|
void SetCenterOfMassOffset(const AZ::Vector3& comOffset) override;
|
|
AZ::Vector3 GetLinearVelocity() const override;
|
|
void SetLinearVelocity(const AZ::Vector3& velocity) override;
|
|
AZ::Vector3 GetAngularVelocity() const override;
|
|
void SetAngularVelocity(const AZ::Vector3& angularVelocity) override;
|
|
AZ::Vector3 GetLinearVelocityAtWorldPoint(const AZ::Vector3& worldPoint) const override;
|
|
void ApplyLinearImpulse(const AZ::Vector3& impulse) override;
|
|
void ApplyLinearImpulseAtWorldPoint(const AZ::Vector3& impulse, const AZ::Vector3& worldPoint) override;
|
|
void ApplyAngularImpulse(const AZ::Vector3& angularImpulse) override;
|
|
|
|
bool IsKinematic() const override;
|
|
void SetKinematic(bool isKinematic) override;
|
|
void SetKinematicTarget(const AZ::Transform& targetPosition) override;
|
|
|
|
bool IsGravityEnabled() const override;
|
|
void SetGravityEnabled(bool enabled) override;
|
|
void SetSimulationEnabled(bool enabled) override;
|
|
void SetCCDEnabled(bool enabled) override;
|
|
|
|
// AzPhysics::SimulatedBody
|
|
AZ::Transform GetTransform() const override;
|
|
void SetTransform(const AZ::Transform& transform) override;
|
|
AZ::Vector3 GetPosition() const override;
|
|
AZ::Quaternion GetOrientation() const override;
|
|
AZ::Aabb GetAabb() const override;
|
|
AZ::EntityId GetEntityId() const override;
|
|
|
|
AzPhysics::SceneQueryHit RayCast(const AzPhysics::RayCastRequest& request) override;
|
|
|
|
// Physics::ReferenceBase
|
|
AZ::Crc32 GetNativeType() const override;
|
|
void* GetNativePointer() const override;
|
|
|
|
// Not in API but needed to support PhysicsComponentBus
|
|
float GetLinearDamping() const override;
|
|
void SetLinearDamping(float damping) override;
|
|
float GetAngularDamping() const override;
|
|
void SetAngularDamping(float damping) override;
|
|
|
|
//sleeping
|
|
bool IsAwake() const override;
|
|
void ForceAsleep() override;
|
|
void ForceAwake() override;
|
|
float GetSleepThreshold() const override;
|
|
void SetSleepThreshold(float threshold) override;
|
|
|
|
bool ShouldStartAsleep() const { return m_startAsleep; }
|
|
|
|
void SetName(const AZStd::string& entityName);
|
|
const AZStd::string& GetName() const;
|
|
|
|
void AddShape(AZStd::shared_ptr<Physics::Shape> shape) override;
|
|
void RemoveShape(AZStd::shared_ptr<Physics::Shape> shape) override;
|
|
|
|
void UpdateMassProperties(AzPhysics::MassComputeFlags flags = AzPhysics::MassComputeFlags::DEFAULT,
|
|
const AZ::Vector3& centerOfMassOffsetOverride = AZ::Vector3::CreateZero(),
|
|
const AZ::Matrix3x3& inertiaTensorOverride = AZ::Matrix3x3::CreateIdentity(),
|
|
const float massOverride = 1.0f) override;
|
|
|
|
private:
|
|
void CreatePhysXActor(const AzPhysics::RigidBodyConfiguration& configuration);
|
|
|
|
void UpdateCenterOfMass(bool includeAllShapesInMassCalculation);
|
|
void SetInertia(const AZ::Matrix3x3& inertia);
|
|
|
|
AZStd::shared_ptr<physx::PxRigidDynamic> m_pxRigidActor;
|
|
AZStd::vector<AZStd::shared_ptr<PhysX::Shape>> m_shapes;
|
|
AZStd::string m_name;
|
|
PhysX::ActorData m_actorUserData;
|
|
bool m_startAsleep = false;
|
|
};
|
|
|
|
AZ_POP_DISABLE_WARNING
|
|
} // namespace PhysX
|