Fixed various physics bugs and added Physics Material to Inspector #249

Merged
direnbharwani merged 7 commits from SP3-2-Physics into main 2022-11-22 20:14:31 +08:00
8 changed files with 453 additions and 43 deletions

View File

@ -4,7 +4,7 @@
NumberOfChildren: 0
Components:
Camera Component:
Position: {x: 0, y: 0, z: 8}
Position: {x: 0, y: 3, z: 8}
Pitch: 0
Yaw: 0
Roll: 0
@ -13,6 +13,7 @@
Near: 0.00999999978
Far: 10000
Perspective: true
IsActive: true
Light Component:
Position: {x: 0, y: 0, z: 0}
Type: Directional
@ -20,6 +21,7 @@
Color: {x: 0.951541841, y: 0.921719015, z: 0.553319454, w: 1}
Layer: 4294967295
Strength: 0
IsActive: true
Scripts: ~
- EID: 7
Name: BigBoi
@ -30,9 +32,11 @@
Translate: {x: 0, y: -16.8647861, z: -14.039052}
Rotate: {x: -0, y: 0, z: -0}
Scale: {x: 28.1434975, y: 28.1434975, z: 28.1434975}
IsActive: true
Renderable Component:
Mesh: 149697411
Material: 126974645
IsActive: true
Scripts: ~
- EID: 8
Name: AmbientLight
@ -46,4 +50,335 @@
Color: {x: 1, y: 1, z: 1, w: 1}
Layer: 4294967295
Strength: 0.25
IsActive: true
Scripts: ~
- EID: 65542
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 2.5, y: 0.5, z: -2.5}
Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 5, y: 0.5, z: 5}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 65538
Name: Movable
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 0, y: 4.28833103, z: 0}
Rotate: {x: -0, y: 0, z: -0}
Scale: {x: 1, y: 1, z: 1}
IsActive: true
RigidBody Component:
Type: Dynamic
Drag: 0.00999999978
Angular Drag: 0.00999999978
Use Gravity: true
Interpolate: true
Sleeping Enabled: true
Freeze Position X: false
Freeze Position Y: false
Freeze Position Z: false
Freeze Rotation X: false
Freeze Rotation Y: false
Freeze Rotation Z: false
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
- Is Trigger: false
Type: Box
Half Extents: {x: 2, y: 2, z: 2}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 65537
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 5.25, y: 2.75, z: 2.5}
Rotate: {x: 0, y: 0, z: 1.57079637}
Scale: {x: 5, y: 0.5, z: 5}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 9
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 5.25, y: 2.75, z: -2.5}
Rotate: {x: 0, y: 0, z: 1.57079637}
Scale: {x: 5, y: 0.5, z: 5}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 10
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: -5.25, y: 2.75, z: 2.5}
Rotate: {x: 0, y: 0, z: 1.57079637}
Scale: {x: 5, y: 0.5, z: 5}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 11
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: -5.25, y: 2.75, z: -2.5}
Rotate: {x: 0, y: 0, z: 1.57079637}
Scale: {x: 5, y: 0.5, z: 5}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 12
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 2.5, y: 2.75, z: 5.25}
Rotate: {x: 7.15255737e-07, y: 1.57079566, z: 1.57079589}
Scale: {x: 4.99999809, y: 0.499999762, z: 4.99999809}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 65551
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: -2.5, y: 2.75, z: 5.25}
Rotate: {x: 7.15255737e-07, y: 1.57079566, z: 1.57079589}
Scale: {x: 4.99999809, y: 0.499999762, z: 4.99999809}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 65550
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: -2.5, y: 2.75, z: -5.25}
Rotate: {x: 7.15255737e-07, y: 1.57079566, z: 1.57079589}
Scale: {x: 4.99999809, y: 0.499999762, z: 4.99999809}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 65549
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 2.5, y: 2.75, z: -5.25}
Rotate: {x: 7.15255737e-07, y: 1.57079566, z: 1.57079589}
Scale: {x: 4.99999809, y: 0.499999762, z: 4.99999809}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 16
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 0, y: 2.5, z: 0}
Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 2.5, y: 0.5, z: 2.5}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 5
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 2.5, y: 0.5, z: 2.5}
Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 5, y: 0.5, z: 5}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 4
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: -2.5, y: 0.5, z: -2.5}
Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 5, y: 0.5, z: 5}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~
- EID: 3
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: -2.5, y: 0.5, z: 2.5}
Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 5, y: 0.5, z: 5}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0, z: 0}
Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true
Scripts: ~

View File

@ -44,6 +44,8 @@ namespace Sandbox
{
sceneName = SHSerialization::DeserializeSceneFromFile(sceneAssetID);
#ifndef SHEDITOR
auto* physicsSystem = SHSystemManager::GetSystem<SHPhysicsSystem>();
if (!physicsSystem)
{
@ -53,6 +55,8 @@ namespace Sandbox
physicsSystem->BuildScene(SHSceneManager::GetCurrentSceneGraph());
#endif
/*-----------------------------------------------------------------------*/
/* TESTING CODE */
/*-----------------------------------------------------------------------*/

View File

@ -333,10 +333,10 @@ namespace SHADE
auto cursorPos = ImGui::GetCursorPos();
//collider->IsTrigger
if (collider->GetType() == SHCollisionShape::Type::BOX)
{
SHEditorWidgets::BeginPanel(std::format("{} Box #{}", ICON_FA_CUBE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y });
const auto* BOX = reinterpret_cast<const SHBox*>(collider->GetShape());
SHEditorWidgets::DragVec3
(
@ -360,6 +360,15 @@ namespace SHADE
}
{
SHEditorWidgets::CheckBox("Is Trigger", [collider] { return collider->IsTrigger(); }, [collider](bool value) { collider->SetIsTrigger(value); });
if(ImGui::CollapsingHeader("Physics Material"))
{
SHEditorWidgets::DragFloat("Friction", [collider] { return collider->GetFriction(); }, [collider](float value) { collider->SetFriction(value); }, "Friction", 0.05f, 0.0f, 1.0f);
SHEditorWidgets::DragFloat("Bounciness", [collider] { return collider->GetBounciness(); }, [collider](float value) { collider->SetBounciness(value); }, "Bounciness", 0.05f, 0.0f, 1.0f);
SHEditorWidgets::DragFloat("Mass Density", [collider] { return collider->GetDensity(); }, [collider](float value) { collider->SetDensity(value); }, "Mass Density", 0.1f, 0.0f);
}
SHEditorWidgets::BeginPanel("Offsets",{ ImGui::GetContentRegionAvail().x, 30.0f });
SHEditorWidgets::DragVec3("Position", { "X", "Y", "Z" }, [&collider] {return collider->GetPositionOffset(); }, [&collider](SHVec3 const& vec) {collider->SetPositionOffset(vec); });
SHEditorWidgets::DragVec3("Rotation", { "X", "Y", "Z" },
@ -375,8 +384,6 @@ namespace SHADE
SHEditorWidgets::EndPanel();
}
SHEditorWidgets::CheckBox("Is Trigger", [collider] { return collider->IsTrigger(); }, [collider](bool value) { collider->SetIsTrigger(value); });
if (ImGui::Button(std::format("{} Remove Collider #{}", ICON_MD_REMOVE, i).data()))
{
colliderToDelete = i;

View File

@ -359,6 +359,10 @@ RTTR_REGISTRATION
);
registration::class_<SHCollisionShape>("Collider")
.property("Position Offset", &SHCollisionShape::GetPositionOffset, &SHCollisionShape::SetPositionOffset)
.property("Rotation Offset", &SHCollisionShape::GetRotationOffset, &SHCollisionShape::SetRotationOffset) (metadata(META::angleInRad, true));
.property("IsTrigger" , &SHCollisionShape::IsTrigger , &SHCollisionShape::SetIsTrigger )
.property("Friction" , &SHCollisionShape::GetFriction , &SHCollisionShape::SetFriction )
.property("Bounciness" , &SHCollisionShape::GetBounciness , &SHCollisionShape::SetBounciness )
.property("Density" , &SHCollisionShape::GetDensity , &SHCollisionShape::SetDensity )
.property("Position Offset" , &SHCollisionShape::GetPositionOffset, &SHCollisionShape::SetPositionOffset)
.property("Rotation Offset" , &SHCollisionShape::GetRotationOffset, &SHCollisionShape::SetRotationOffset) (metadata(META::angleInRad, true));
}

View File

@ -123,12 +123,14 @@ namespace SHADE
default: break;
}
auto* rp3dCollider = rp3dBody->getCollider(rp3dBody->getNbColliders() - 1);
syncColliderProperties(collisionShape, rp3dCollider);
if (rp3dBody->getType() == rp3d::BodyType::DYNAMIC)
{
rp3dBody->updateMassPropertiesFromColliders();
auto* rigidBodyComponent = SHComponentManager::GetComponent_s<SHRigidBodyComponent>(entityID);
if (rigidBodyComponent)
if (auto* rigidBodyComponent = SHComponentManager::GetComponent_s<SHRigidBodyComponent>(entityID); rigidBodyComponent)
rigidBodyComponent->mass = rp3dBody->getMass();
}
@ -326,28 +328,42 @@ namespace SHADE
default: break;
}
// Sync material
auto* rp3dCollider = rp3dBody->getCollider(i);
auto& rp3dMaterial = rp3dCollider->getMaterial();
rp3dMaterial.setFrictionCoefficient(collisionShape.GetFriction());
rp3dMaterial.setBounciness(collisionShape.GetBounciness());
rp3dMaterial.setMassDensity(collisionShape.GetDensity());
// Sync tags
const unsigned short MASK_BITS = collisionShape.GetCollisionTag();
rp3dCollider->setCollisionCategoryBits(MASK_BITS);
rp3dCollider->setCollideWithMaskBits(MASK_BITS);
syncColliderProperties(collisionShape, rp3dCollider);
collisionShape.dirty = false;
}
// Set rigidbody mass if dynamic
auto* rigidBodyComponent = SHComponentManager::GetComponent_s<SHRigidBodyComponent>(entityID);
if (rigidBodyComponent)
{
// This is generally expensive, will be optimised in the future with my own engine.
rp3dBody->updateMassPropertiesFromColliders();
rigidBodyComponent->mass = rp3dBody->getMass();
}
}
/*-----------------------------------------------------------------------------------*/
/* Private Function Member Definitions */
/*-----------------------------------------------------------------------------------*/
void SHPhysicsObject::addBoxShape(SHCollisionShape& boxShape) const noexcept
void SHPhysicsObject::syncColliderProperties(const SHCollisionShape& collisionShape, rp3d::Collider* rp3dCollider) const noexcept
{
rp3dCollider->setIsTrigger(collisionShape.IsTrigger());
auto& rp3dMaterial = rp3dCollider->getMaterial();
rp3dMaterial.setFrictionCoefficient(collisionShape.GetFriction());
rp3dMaterial.setBounciness(collisionShape.GetBounciness());
rp3dMaterial.setMassDensity(collisionShape.GetDensity());
const unsigned short MASK_BITS = collisionShape.GetCollisionTag();
rp3dCollider->setCollisionCategoryBits(MASK_BITS);
rp3dCollider->setCollideWithMaskBits(MASK_BITS);
}
void SHPhysicsObject::addBoxShape(const SHCollisionShape& boxShape) const noexcept
{
const rp3d::Transform OFFSETS
{
@ -361,7 +377,7 @@ namespace SHADE
rp3dBody->addCollider(newBox, OFFSETS);
}
void SHPhysicsObject::syncBoxShape(int index, SHCollisionShape& boxShape) const noexcept
void SHPhysicsObject::syncBoxShape(int index, const SHCollisionShape& boxShape) const noexcept
{
const auto* BOX = reinterpret_cast<const SHBox*>(boxShape.GetShape());
@ -380,7 +396,7 @@ namespace SHADE
rp3dBox->setHalfExtents(BOX->GetWorldExtents());
}
void SHPhysicsObject::addSphereShape(SHCollisionShape& sphereShape) const noexcept
void SHPhysicsObject::addSphereShape(const SHCollisionShape& sphereShape) const noexcept
{
const rp3d::Transform OFFSETS
{
@ -394,7 +410,7 @@ namespace SHADE
rp3dBody->addCollider(newSphere, OFFSETS);
}
void SHPhysicsObject::syncSphereShape(int index, SHCollisionShape& sphereShape) const noexcept
void SHPhysicsObject::syncSphereShape(int index, const SHCollisionShape& sphereShape) const noexcept
{
const auto* SPHERE = reinterpret_cast<const SHSphere*>(sphereShape.GetShape());

View File

@ -96,14 +96,16 @@ namespace SHADE
/* Function Members */
/*---------------------------------------------------------------------------------*/
void syncColliderProperties (const SHCollisionShape& collisionShape, rp3d::Collider* rp3dCollider) const noexcept;
// Box Shapes
void addBoxShape (SHCollisionShape& boxShape) const noexcept;
void syncBoxShape (int index, SHCollisionShape& boxShape) const noexcept;
void addBoxShape (const SHCollisionShape& boxShape) const noexcept;
void syncBoxShape (int index, const SHCollisionShape& boxShape) const noexcept;
// Sphere Shapes
void addSphereShape (SHCollisionShape& sphereShape) const noexcept;
void syncSphereShape (int index, SHCollisionShape& sphereShape) const noexcept;
void addSphereShape (const SHCollisionShape& sphereShape) const noexcept;
void syncSphereShape (int index, const SHCollisionShape& sphereShape) const noexcept;
};
} // namespace SHADE

View File

@ -21,6 +21,14 @@
#include "Input/SHInputManager.h"
/*-------------------------------------------------------------------------------------*/
/* Local Functions */
/*-------------------------------------------------------------------------------------*/
void testFunction();
/////////////////////////////////////////////////////////////////////////////////////////
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
@ -134,6 +142,8 @@ namespace SHADE
const double FIXED_DT = physicsSystem->fixedDT;
accumulatedTime += dt;
//testFunction();
int count = 0;
while (accumulatedTime > FIXED_DT)
{
@ -315,7 +325,7 @@ namespace SHADE
if (physicsObject.GetRigidBody()->isActive())
physicsObject.prevTransform = CURRENT_TF;
// Sync with rigid bodies
// Sync with rigid bodies. If an object doesn't have a rigidbody, no update is needed here as it is done in pre-update.
if (rigidBodyComponent && SHSceneManager::CheckNodeAndComponentsActive<SHRigidBodyComponent>(physicsObject.entityID))
{
// Skip static bodies
@ -336,20 +346,52 @@ namespace SHADE
rigidBodyComponent->position = CURRENT_TF.getPosition();
rigidBodyComponent->orientation = CURRENT_TF.getOrientation();
}
// Sync with colliders
if (colliderComponent && SHSceneManager::CheckNodeAndComponentsActive<SHColliderComponent>(physicsObject.entityID))
{
colliderComponent->position = CURRENT_TF.getPosition();
colliderComponent->orientation = CURRENT_TF.getOrientation();
}
// Sync with colliders
if (colliderComponent && SHSceneManager::CheckNodeAndComponentsActive<SHColliderComponent>(physicsObject.entityID))
{
// Skip colliders without rigidbody components. If any transform was updated, it was done in pre-update.
// Set transform for rendering
if (transformComponent)
{
transformComponent->SetWorldPosition(renderPos);
transformComponent->SetWorldOrientation(renderRot);
colliderComponent->position = CURRENT_TF.getPosition();
colliderComponent->orientation = CURRENT_TF.getOrientation();
}
// Set transform for rendering
if (transformComponent)
{
transformComponent->SetWorldPosition(renderPos);
transformComponent->SetWorldOrientation(renderRot);
}
}
}
} // namespace SHADE
/////////////////////////////////////////////////////////////////////////////////////////
void testFunction()
{
using namespace SHADE;
// Test movement
const float forceModifier = 25.0f;
EntityID eid = 65538;
if (SHEntityManager::IsValidEID(eid))
{
auto* rb = SHComponentManager::GetComponent_s<SHRigidBodyComponent>(eid);
if (rb)
{
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::W))
rb->AddForce(-SHVec3::UnitZ * forceModifier);
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::A))
rb->AddForce(-SHVec3::UnitX * forceModifier);
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::S))
rb->AddForce(SHVec3::UnitZ * forceModifier);
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::D))
rb->AddForce(SHVec3::UnitX * forceModifier);
}
}
}

View File

@ -188,7 +188,7 @@ namespace YAML
if (node[Friction].IsDefined())
rhs.SetFriction(node[Friction].as<float>());
if (node[Bounciness].IsDefined())
rhs.SetBounciness(rhs.GetBounciness());
rhs.SetBounciness(node[Bounciness].as<float>());
if (node[Density].IsDefined())
rhs.SetDensity(node[Density].as<float>());
if (node[PositionOffset].IsDefined())