Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PhysX integration #27

Merged
merged 34 commits into from
Nov 12, 2023
Merged

Added PhysX integration #27

merged 34 commits into from
Nov 12, 2023

Conversation

AtlantiaKing
Copy link
Collaborator

Added physics integration with a PhysX service.

Features:

  • BoxCollider, CapsuleCollider and SphereCollider components that can be triggers
  • Collision & Trigger callbacks inside the collider components
  • Physics Materials
  • Rigidbody component with mass, forces, torque, constraints and can be set to kinematic
  • Rigidbody has its own SetPosition & SetRotation functions to move a physics actor since Transform doesn't work anymore

Added IPhysics interface with DefaultPhysics class
Added PhysXEngine which sets up top-level PhysX objects at construction and releases them on destruction
Added physics to the servicelocator from the engine
Fixed additional physX libraries not being included by cmake
Fixed physX header files not being included by cmake
Added IPhysicsScene interface and added PhysXScene class that derives from the interface
Added CreateScene function to the IPhysics and PhysXEngine classes
Scene constructor calls CreateScene function from the IPhysics interface
Added cube obj
Added PhysicsSync class with static function to sync from physX to transform component
Added IPhysicsObject interface and PhysXObject classe which is a wrapper around a rigidactor
Added IShape interface and PhysXShapes file which will hold all the PhysX shapes as different classes
Modified physics engine to be able to add actors and simulate the scene
Added physics to the main game loop
Added BoxCollider & Rigidbody components to the main engine
Added sphere shape to PhysXShapes
Added SphereCollider component
Added SetSize and SetRadius functions to the IShape class
Added SetSize function to the BoxCollider component
Added SetRadius function to the SphereCollider component
Added sphere obj
Added base collider component, BoxCollider and SphereCollider now derive from this Collider component
Rigidbody now makes sure child colliders have the same rigid actor
Transforms of static colliders are now no longer being updated by the physics system, instead the physics bodies of static colliders are being updated by the transforms
Fixed compile error in the GameObject GetComponentInParent class
Fixed PhysicsObjects not being deleted from the scene properly when they are destroyed
When a shape is added or removed from a rigidbody, the center of mass (COM) is now being recalculated
Added GetVolume and GetRelativePosition methods to the IShape interface and its derived classes
Calcualte CenterOfMass functino in the PhysXObject class now uses the PxRigidBodyExt::setMassAndUpdateIntertia function, which sets more parameters then just the COM for correct behaviour.
Moved Rigidbody from inside the IPhysicsObject interface to its own file
Added SetMass functino to Rigidbody component
Replaced the calculations from the glm library to convert quaterions to euler and euler to quaterions with my own implementation.
Removed the yaw rotation on the parent transform of the camera object. Each object should only do one rotation, the parent does yaw, the camera itself does pitch.
Moved all the quaternion/euler angles calculations to a Quaternion class inside the utils engine, it is non constructable and has some static helper functions to correctly convert from euler to quaternion and from quaternion to euler
Added overloads for functions like SetSize and SetVelocity to also take the seperate floats instead of a glm::vec3 object
Added Visual Leak Detector to the 3rdParty folder and included it only into the game project
Included vld into main.cpp of the game project
Fixed memory leaks in PhysXEngine.cpp
… capsule collider

Added rule of 5 to IPhysXShape
Added PhysXCapsuleShape
Added GetPosition and GetRotation to IPhysicsObject and PhysXObject
Fixed order of PhysXObject updates (Rigidbody was being updated after the transform was updated)
Added SetPosition, Translate, SetRotation and Rotate functions to the Rigidbody component
Fixed direction vectors of child transform not being updated after rotation has changed in parent
Added IPhysicsMaterial interface and PhysXMaterial class
Added CreateMaterial function to the IPhysics interface and PhysXEngine class
Added SetMaterial function to the Collider base class
Colliders that get created without a physics collider set will be created using a default material created by the physics engine
Added PhysXSimulationCallbacks & PhysXSimulationFilterShader which are owned by the PhysXEngine and passed to the PxScene on creation. PhysXSimulationFilterShader currently lets all the collisions succeed and notify
A pointer to the component collider is now stored inside the userdata of the corresponding PxShape
Added static OnCollision method to the PhysicsSync class to handle incoming collision events from the physics engine
Added Subject to the Collider component which gets called when that collider receives a collision event
Added PrintCollision component which subscribes to the OnCollision subject of the collider on the same gameobject and prints the names of the gameobjects that are colliding
Moved temporary rigidbody creation to a CheckExistence function instead of copy pasting the code in each function
Added Force struct, which stores the value, if it is a torque or not, and the force mode
Added AddForce and AddTorque to the Rigidbody class and the Rigidbody component
PhysXObject now applies forces stored in the rigidbody class
Added ApplyForces component to test force functions
Moved some logic to IPhysXShape and added SetTrigger function to IShape and IPhysXShape
Added SetTrigger function to base collider component
Added trigger checking to the PhysXSimulationFilterShader
Renamed OnCollision to OnCollisionEnter, Added OnCollisionStay and OnCollisionExit
Added PhysXSimulationfilterCallback class which gives us more control
Added setup for OnTriggerEnter, OnTriggerStay and OnTriggerExit but the SimulationfilterCallback isn't being called for trigger shapes yet, that's the next step.
SimulationFilterShader now returns default filter flag for triggers
Moved SimulationEvent and SimulationEventType from PhysXSimulationFilterCallback.h to PhysXSimulationData.h and SimulationEventData.h
Moved SimulationPair from PhysXSimulationFilterCallback.h to PhysXSimulationData.h
Trigger events are now being handled by the PhysxSimulationCallbacks class instead of by the PhysXSimulationFilterCallback class
Added RescaleShape pure virtual method to the base collider and added overrides to all the collider components
RescaleShape is now being called when scale has changed on the transform of the component
Added capsule.obj
Renamed Observer and Subject to TObserver and TSubject and added a non-templated Observer & Subject class

#include <vec3.hpp>

#include <Observer.h>
Copy link
Owner

@LeeVangraefschepe LeeVangraefschepe Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused include in header file
Base class uses it (Collider) but BoxCollider doesnt

#include "../../SceneGraph/GameObject.h"

#include "../Transform/Transform.h"
#include "Rigidbody.h"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused include


void leap::Collider::SetMaterial(std::shared_ptr<physics::IPhysicsMaterial> pMaterial)
{
m_pMaterial = pMaterial;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing copy from std::shared_ptr. Should be const ref

Rigidbody& operator=(Rigidbody&& other) = delete;

void SetKinematic(bool isKinematic);
void SetVelocity(const glm::vec3& velocity);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set angular velocity is missing


leap::PhysicsSync::ColliderPair leap::PhysicsSync::GetColliders(const physics::CollisionData& collision)
{
Collider* pFirstCollider{ reinterpret_cast<Collider*>(collision.pFirst) };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use static cast

Removed redundant includes
shared_ptr parameter in the SetMaterial function of the Collider class is now passed by reference instead of copy
GetColliders function in the PhysicsSync class now uses static_cast to convert void* to Collider* instead of reinterpret_cast
#include <PxPhysics.h>
#include <PxMaterial.h>

#include <Debug.h>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug include is unused

#pragma once

#include "../Interfaces/IPhysics.h"
#include "../Interfaces/IPhysicsObject.h"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused include

#include <PxRigidActor.h>
#include <PxRigidStatic.h>
#include <PxRigidDynamic.h>
#include <PxRigidActor.h>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Included twice

#include "PhysXScene.h"

#include "../Data/Rigidbody.h"
#include "../Data/ForceMode.h"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused include

#include <PxRigidStatic.h>
#include <PxRigidDynamic.h>
#include <PxRigidActor.h>
#include <PxScene.h>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused include

if (!IsValid()) static_cast<PhysXScene*>(pScene)->RemoveActor(m_pActor);
}

void leap::physics::PhysXObject::Apply(const std::function<void(void*, const glm::vec3&, const glm::quat&)>& setFunc, const std::function<std::pair<const glm::vec3&, const glm::quat&>(void*)> getFunc)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function can be made const

{
if (m_pRigidbody == nullptr)
{
auto transformPair{ getFunc(m_pOwner) };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be made const


#include "../Interfaces/IPhysicsObject.h"

#include <Subject.h>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused include

for (physx::PxU32 i = 0; i < rb.getNbLines(); i++)
{
const physx::PxDebugLine& line = pLines[i];
debugDrawings.emplace_back(std::make_pair(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::make_pair is not needed for emplace_back

return debugDrawings;
}

void leap::physics::PhysXScene::AddActor(physx::PxRigidActor* pActor)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function can be const

m_pScene->addActor(*pActor);
}

void leap::physics::PhysXScene::RemoveActor(physx::PxRigidActor* pActor)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function can be const

physx::PxShape* m_pShape{};
};

class PhysXBoxShape final : public IPhysXShape

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing rule of 5 (destructor is not default)

virtual glm::vec3 GetRelativePosition() override;
};

class PhysXSphereShape final : public IPhysXShape

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing rule of 5 (destructor is not default)

virtual glm::vec3 GetRelativePosition() override;
};

class PhysXCapsuleShape final : public IPhysXShape

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing rule of 5 (destructor is not default)

#include "PhysXSimulationCallbacks.h"

#include <Debug.h>
#include <thread>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused include

@@ -0,0 +1,41 @@
#include "PhysXSimulationFilterCallback.h"

#include <Debug.h>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused include

)
"Components/PrintCollision.cpp"
"Components/ApplyForces.cpp"
"Components/ColliderScaler.cpp" "Components/PrintVelocity.h" "Components/PrintVelocity.cpp")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header in CMake and on same line

// Quaternion is constructable because it is NOT the object representation for a quaternion,
// use glm::quat for this
// This class exists to give some helper functions to generate correct quaternions/eulerangles
Quaternion() = default;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructor can be deleted

Copy link
Owner

@LeeVangraefschepe LeeVangraefschepe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied feedback (some you already did live)
Request again after the components have the physics calls to derive from.

Added OnCollision/OnTrigger methods to the Component class
Added OnCollision/OnTrigger methods to the GameObject class which calls the component versions of the methods
PhysicsSync now calls the gameobject oncollision/ontrigger methods instead of the collider methods
Instead of having private Quaternion constructor/destructor, I changed it so the class deletes its constructor
Instead of having to recreate a CustomMesh object every frame, it can now be cleared which keeps the capacity
Const correctness in PhysXObject & PhysXScene
Removed redundant includes in PhysXEngine, PhysXMaterial, PhysXObject and PhysXSimulationCallbacks
GetConstraints function in the Rigidbody class now returns a const reference instead of a copy
Removed make_pair call for more readable code in the GetDebugDrawings function in the PhysXScene
Refactored IPhysxShape so it deletes the PxShape in its own destructors instead of the derived destructor and made the GetShape method non-virtual.
Cleaned unag CMakeLists
SetEnabledDebugDrawing is now being called in the main.cpp instead of the Leap.cpp
Removed m_Renderer variable in the Leap class
Copy link
Owner

@LeeVangraefschepe LeeVangraefschepe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feedback has been applied

@LeeVangraefschepe LeeVangraefschepe merged commit cd586e8 into main Nov 12, 2023
2 checks passed
@LeeVangraefschepe LeeVangraefschepe deleted the Physics-integration branch November 12, 2023 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants