Merge branch 'SP3-2-Physics' into PlayerControllerWIthNewPhysics

This commit is contained in:
Glence 2023-03-04 15:17:20 +08:00
commit 9a7ad8ee17
17 changed files with 134 additions and 55 deletions

View File

@ -10,12 +10,9 @@
IsActive: true IsActive: true
RigidBody Component: RigidBody Component:
Type: Dynamic Type: Dynamic
Auto Mass: false
Mass: 1
Drag: 0.00999999978 Drag: 0.00999999978
Angular Drag: 0.100000001 Angular Drag: 0.100000001
Use Gravity: true Use Gravity: true
Gravity Scale: 1
Interpolate: false Interpolate: false
Sleeping Enabled: true Sleeping Enabled: true
Freeze Position X: false Freeze Position X: false
@ -38,6 +35,10 @@
Rotation Offset: {x: 0, y: 0, z: 0} Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true IsActive: true
Scripts: Scripts:
- Type: PhysicsTestObj
Enabled: true
forceAmount: 50
torqueAmount: 500
- Type: CollisionTest - Type: CollisionTest
Enabled: true Enabled: true
- EID: 1 - EID: 1
@ -74,10 +75,10 @@
Yaw: 0 Yaw: 0
Roll: 0 Roll: 0
Width: 1920 Width: 1920
Height: 1080
Near: 0.00999999978 Near: 0.00999999978
Far: 10000 Far: 10000
Perspective: true Perspective: true
FOV: 90
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 65539 - EID: 65539
@ -172,6 +173,43 @@
Rotation Offset: {x: 0, y: 0, z: 0} Rotation Offset: {x: 0, y: 0, z: 0}
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 7
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 0, y: 0, z: 3}
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.100000001
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
Collision Tag: 1
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: 8 - EID: 8
Name: Target Name: Target
IsActive: true IsActive: true

View File

@ -4,7 +4,7 @@
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
Camera Component: Camera Component:
Position: {x: 1, y: 5, z: 5} Position: {x: 0, y: 2, z: 5}
Pitch: 0 Pitch: 0
Yaw: 0 Yaw: 0
Roll: 0 Roll: 0
@ -28,9 +28,9 @@
Collider Component: Collider Component:
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Collision Tag: 6 Collision Tag: 0
Type: Sphere Type: Box
Radius: 1 Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006 Friction: 0.400000006
Bounciness: 0 Bounciness: 0
Density: 1 Density: 1
@ -44,9 +44,9 @@
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
Transform Component: Transform Component:
Translate: {x: 0, y: 5, z: 0} Translate: {x: 0.141888797, y: 5, z: 0}
Rotate: {x: -0, y: 0.785398006, z: -0} Rotate: {x: -0, y: 0, z: 0.490080774}
Scale: {x: 1, y: 1, z: 1} Scale: {x: 0.999999702, y: 0.999999702, z: 1}
IsActive: true IsActive: true
RigidBody Component: RigidBody Component:
Type: Dynamic Type: Dynamic
@ -81,4 +81,27 @@
- Type: PhysicsTestObj - Type: PhysicsTestObj
Enabled: true Enabled: true
forceAmount: 50 forceAmount: 50
torqueAmount: 25 torqueAmount: 25
- EID: 2
Name: Default
IsActive: true
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 0.34412086, y: 3.23541069, z: 0}
Rotate: {x: 0, y: 0, z: 0.490080804}
Scale: {x: 0.999999702, y: 0.999999702, z: 1}
IsActive: true
Collider Component:
Colliders:
- Is Trigger: false
Collision Tag: 0
Type: Sphere
Radius: 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: false
Scripts: ~

View File

@ -1,4 +1,4 @@
/********************************************************************* /*********************************************************************
* \file Homeowner1.cs * \file Homeowner1.cs
* \author Ryan Wang Nian Jing * \author Ryan Wang Nian Jing
* \brief The implemented behaviour tree for the homeowner * \brief The implemented behaviour tree for the homeowner

View File

@ -1,4 +1,4 @@
/********************************************************************* /*********************************************************************
* \file LeafAttack.cs * \file LeafAttack.cs
* \author Ryan Wang Nian Jing * \author Ryan Wang Nian Jing
* \brief Leaf node implementation for AI attacking the player * \brief Leaf node implementation for AI attacking the player

View File

@ -1,4 +1,4 @@
using SHADE; using SHADE;
using SHADE_Scripting.Audio; using SHADE_Scripting.Audio;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

View File

@ -1,4 +1,4 @@
using SHADE; using SHADE;
using SHADE_Scripting.Audio; using SHADE_Scripting.Audio;
using System; using System;
public class Item : Script public class Item : Script

View File

@ -1,4 +1,4 @@
using SHADE; using SHADE;
using SHADE_Scripting.Audio; using SHADE_Scripting.Audio;
using System; using System;

View File

@ -1,4 +1,4 @@
using SHADE; using SHADE;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;

View File

@ -432,8 +432,6 @@ namespace SHADE
// TODO: Handle differences between composite & hull collider // TODO: Handle differences between composite & hull collider
if (ImGui::BeginMenu("Add Collider")) if (ImGui::BeginMenu("Add Collider"))
{ {
int newColl = -1;
if (ImGui::Selectable("Box Collider")) if (ImGui::Selectable("Box Collider"))
{ {
auto* compositeCollider = reinterpret_cast<SHCompositeCollider* const>(component->GetCollider()); auto* compositeCollider = reinterpret_cast<SHCompositeCollider* const>(component->GetCollider());
@ -448,7 +446,10 @@ namespace SHADE
ImGui::EndMenu(); ImGui::EndMenu();
} }
} }
else DrawContextMenu(component); else
{
DrawContextMenu(component);
}
ImGui::PopID(); ImGui::PopID();
} }

View File

@ -204,9 +204,9 @@ namespace SHADE
for (auto* shape : shapes) for (auto* shape : shapes)
{ {
if (PREV_STATE) // Previously inactive if (!PREV_STATE) // Previously inactive
broadphase->Insert(shape->id, shape->ComputeAABB()); broadphase->Insert(shape->id, shape->ComputeAABB());
else // Previously active else // Previously active
broadphase->Remove(shape->id); broadphase->Remove(shape->id);
} }
} }
@ -300,7 +300,7 @@ namespace SHADE
}; };
// Remove from broadphase // Remove from broadphase
if (broadphase) if (IsActive() && broadphase)
broadphase->Remove((*shape)->id); broadphase->Remove((*shape)->id);
shapeLibrary->DestroyShape(*shape); shapeLibrary->DestroyShape(*shape);

View File

@ -49,11 +49,14 @@ namespace SHADE
return; return;
} }
collider->broadphase = &broadphase; if (collider->IsActive())
{
collider->broadphase = &broadphase;
// Add all existing shapes to the broadphase // Attempt to add all existing shapes to the broadphase
for (const auto* shape : collider->shapes) for (const auto* shape : collider->shapes)
broadphase.Insert(shape->id, shape->ComputeAABB()); broadphase.Insert(shape->id, shape->ComputeAABB());
}
} }
void SHCollisionSpace::RemoveCollider(SHCollider* collider) noexcept void SHCollisionSpace::RemoveCollider(SHCollider* collider) noexcept
@ -64,13 +67,17 @@ namespace SHADE
if (NUM_SHAPES == 0) if (NUM_SHAPES == 0)
return; return;
for (uint32_t i = 0; i < NUM_SHAPES; ++i) // If collider is inactive, skip removals since they were already removed
broadphase.Remove(collider->shapes[i]->id); if (collider->IsActive())
if (contactManager)
{ {
contactManager->RemoveInvalidatedTrigger(collider->entityID); for (uint32_t i = 0; i < NUM_SHAPES; ++i)
contactManager->RemoveInvalidatedManifold(collider->entityID); broadphase.Remove(collider->shapes[i]->id);
if (contactManager)
{
contactManager->RemoveInvalidatedTrigger(collider->entityID);
contactManager->RemoveInvalidatedManifold(collider->entityID);
}
} }
/* /*

View File

@ -100,7 +100,8 @@ namespace SHADE
shapes.emplace_back(sphere); shapes.emplace_back(sphere);
if (broadphase) // Only add shapes to the broadphase if the collider is active
if (IsActive() && broadphase)
broadphase->Insert(NEW_SHAPE_ID, sphere->ComputeAABB()); broadphase->Insert(NEW_SHAPE_ID, sphere->ComputeAABB());
// Broadcast Event for adding a shape // Broadcast Event for adding a shape
@ -153,7 +154,7 @@ namespace SHADE
shapes.emplace_back(box); shapes.emplace_back(box);
if (broadphase) if (IsActive() && broadphase)
broadphase->Insert(NEW_SHAPE_ID, box->ComputeAABB()); broadphase->Insert(NEW_SHAPE_ID, box->ComputeAABB());
// Broadcast Event for adding a shape // Broadcast Event for adding a shape

View File

@ -252,22 +252,13 @@ namespace SHADE
{ {
// New states start at invalid. In the first frame of collision, move to enter. // New states start at invalid. In the first frame of collision, move to enter.
// If it already in enter, move to stay // If it already in enter, move to stay
if (state == SHCollisionState::ENTER) state = state == SHCollisionState::INVALID ? SHCollisionState::ENTER : SHCollisionState::STAY;
state = SHCollisionState::STAY;
if (state == SHCollisionState::INVALID)
state = SHCollisionState::ENTER;
} }
else else
{ {
// If already exited and still not colliding, the collision has expired. // If already exited and still not colliding, the collision has expired.
// Invalid states are removed in the next frame // Invalid states are removed in the next frame
if (state == SHCollisionState::EXIT) state = state == SHCollisionState::EXIT ? SHCollisionState::INVALID : SHCollisionState::EXIT;
state = SHCollisionState::INVALID;
// If previously colliding, move to exit.
if (state == SHCollisionState::ENTER || state == SHCollisionState::STAY)
state = SHCollisionState::EXIT;
} }
} }

View File

@ -86,8 +86,6 @@ namespace SHADE
void SHPhysicsWorld::Step(float dt) void SHPhysicsWorld::Step(float dt)
{ {
/* /*
* Detect Collisions * Detect Collisions
*/ */

View File

@ -57,7 +57,8 @@ namespace SHADE
for (const auto& COLLIDER_COMPONENT : COLLIDER_COMPONENT_DENSE) for (const auto& COLLIDER_COMPONENT : COLLIDER_COMPONENT_DENSE)
{ {
const auto* COLLIDER = COLLIDER_COMPONENT.GetCollider(); const auto* COLLIDER = COLLIDER_COMPONENT.GetCollider();
drawCollider(debugDrawSystem, *COLLIDER); if (COLLIDER->IsActive())
drawCollider(debugDrawSystem, *COLLIDER);
} }
} }
else if (!physicsDebugDrawSystem->collidersToDraw.empty()) else if (!physicsDebugDrawSystem->collidersToDraw.empty())
@ -65,7 +66,8 @@ namespace SHADE
for (const auto EID : physicsDebugDrawSystem->collidersToDraw) for (const auto EID : physicsDebugDrawSystem->collidersToDraw)
{ {
const auto* COLLIDER = SHComponentManager::GetComponent<SHColliderComponent>(EID)->GetCollider(); const auto* COLLIDER = SHComponentManager::GetComponent<SHColliderComponent>(EID)->GetCollider();
drawCollider(debugDrawSystem, *COLLIDER); if (COLLIDER->IsActive())
drawCollider(debugDrawSystem, *COLLIDER);
} }
} }

View File

@ -87,10 +87,6 @@ namespace SHADE
transformComponent->SetWorldPosition(MOTION_STATE.position); transformComponent->SetWorldPosition(MOTION_STATE.position);
transformComponent->SetWorldOrientation(MOTION_STATE.orientation); transformComponent->SetWorldOrientation(MOTION_STATE.orientation);
} }
/*
* TODO: Test if the scene graph transforms abides by setting world position. Collisions will ignore the scene graph hierarchy.
*/
} }
} }
} }

View File

@ -52,8 +52,19 @@ namespace SHADE
const bool RIGIDBODY_ACTIVE = physicsObject.rigidBody->IsActive(); const bool RIGIDBODY_ACTIVE = physicsObject.rigidBody->IsActive();
if (IS_ACTIVE != RIGIDBODY_ACTIVE) if (IS_ACTIVE != RIGIDBODY_ACTIVE)
{
physicsObject.rigidBody->SetIsActive(IS_ACTIVE); physicsObject.rigidBody->SetIsActive(IS_ACTIVE);
// If previously active, we remove it from the world.
// If previously inactive, we add it back to the world.
// This removes the need to check for inactive objects in the world every frame to remove invalid contacts.
if (IS_ACTIVE) // Previously inactive
physicsSystem->physicsWorld->AddRigidBody(physicsObject.rigidBody);
else // Previously active
physicsSystem->physicsWorld->RemoveRigidBody(physicsObject.rigidBody);
}
if (UPDATE_TRANSFORM) if (UPDATE_TRANSFORM)
{ {
const SHVec3& WORLD_POS = TRANSFORM_COMPONENT->GetWorldPosition(); const SHVec3& WORLD_POS = TRANSFORM_COMPONENT->GetWorldPosition();
@ -71,8 +82,19 @@ namespace SHADE
const bool COLLIDER_ACTIVE = physicsObject.collider->IsActive(); const bool COLLIDER_ACTIVE = physicsObject.collider->IsActive();
if (IS_ACTIVE != COLLIDER_ACTIVE) if (IS_ACTIVE != COLLIDER_ACTIVE)
{
physicsObject.collider->SetIsActive(IS_ACTIVE); physicsObject.collider->SetIsActive(IS_ACTIVE);
// If previously active, we remove it from the world.
// If previously inactive, we add it back to the world.
// This removes the need to check for inactive objects in the world every frame to remove invalid contacts.
if (IS_ACTIVE) // Previously inactive
physicsSystem->collisionSpace->AddCollider(physicsObject.collider);
else // Previously active
physicsSystem->collisionSpace->RemoveCollider(physicsObject.collider);
}
if (UPDATE_TRANSFORM) if (UPDATE_TRANSFORM)
{ {
const SHVec3& WORLD_POS = TRANSFORM_COMPONENT->GetWorldPosition(); const SHVec3& WORLD_POS = TRANSFORM_COMPONENT->GetWorldPosition();