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

View File

@ -4,7 +4,7 @@
NumberOfChildren: 0
Components:
Camera Component:
Position: {x: 1, y: 5, z: 5}
Position: {x: 0, y: 2, z: 5}
Pitch: 0
Yaw: 0
Roll: 0
@ -28,9 +28,9 @@
Collider Component:
Colliders:
- Is Trigger: false
Collision Tag: 6
Type: Sphere
Radius: 1
Collision Tag: 0
Type: Box
Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006
Bounciness: 0
Density: 1
@ -44,9 +44,9 @@
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 0, y: 5, z: 0}
Rotate: {x: -0, y: 0.785398006, z: -0}
Scale: {x: 1, y: 1, z: 1}
Translate: {x: 0.141888797, y: 5, z: 0}
Rotate: {x: -0, y: 0, z: 0.490080774}
Scale: {x: 0.999999702, y: 0.999999702, z: 1}
IsActive: true
RigidBody Component:
Type: Dynamic
@ -82,3 +82,26 @@
Enabled: true
forceAmount: 50
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
* \author Ryan Wang Nian Jing
* \brief The implemented behaviour tree for the homeowner

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -49,11 +49,14 @@ namespace SHADE
return;
}
collider->broadphase = &broadphase;
if (collider->IsActive())
{
collider->broadphase = &broadphase;
// Add all existing shapes to the broadphase
for (const auto* shape : collider->shapes)
broadphase.Insert(shape->id, shape->ComputeAABB());
// Attempt to add all existing shapes to the broadphase
for (const auto* shape : collider->shapes)
broadphase.Insert(shape->id, shape->ComputeAABB());
}
}
void SHCollisionSpace::RemoveCollider(SHCollider* collider) noexcept
@ -64,13 +67,17 @@ namespace SHADE
if (NUM_SHAPES == 0)
return;
for (uint32_t i = 0; i < NUM_SHAPES; ++i)
broadphase.Remove(collider->shapes[i]->id);
if (contactManager)
// If collider is inactive, skip removals since they were already removed
if (collider->IsActive())
{
contactManager->RemoveInvalidatedTrigger(collider->entityID);
contactManager->RemoveInvalidatedManifold(collider->entityID);
for (uint32_t i = 0; i < NUM_SHAPES; ++i)
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);
if (broadphase)
// Only add shapes to the broadphase if the collider is active
if (IsActive() && broadphase)
broadphase->Insert(NEW_SHAPE_ID, sphere->ComputeAABB());
// Broadcast Event for adding a shape
@ -153,7 +154,7 @@ namespace SHADE
shapes.emplace_back(box);
if (broadphase)
if (IsActive() && broadphase)
broadphase->Insert(NEW_SHAPE_ID, box->ComputeAABB());
// 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.
// If it already in enter, move to stay
if (state == SHCollisionState::ENTER)
state = SHCollisionState::STAY;
if (state == SHCollisionState::INVALID)
state = SHCollisionState::ENTER;
state = state == SHCollisionState::INVALID ? SHCollisionState::ENTER : SHCollisionState::STAY;
}
else
{
// If already exited and still not colliding, the collision has expired.
// Invalid states are removed in the next frame
if (state == SHCollisionState::EXIT)
state = SHCollisionState::INVALID;
// If previously colliding, move to exit.
if (state == SHCollisionState::ENTER || state == SHCollisionState::STAY)
state = SHCollisionState::EXIT;
state = state == SHCollisionState::EXIT ? SHCollisionState::INVALID : SHCollisionState::EXIT;
}
}

View File

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

View File

@ -57,7 +57,8 @@ namespace SHADE
for (const auto& COLLIDER_COMPONENT : COLLIDER_COMPONENT_DENSE)
{
const auto* COLLIDER = COLLIDER_COMPONENT.GetCollider();
drawCollider(debugDrawSystem, *COLLIDER);
if (COLLIDER->IsActive())
drawCollider(debugDrawSystem, *COLLIDER);
}
}
else if (!physicsDebugDrawSystem->collidersToDraw.empty())
@ -65,7 +66,8 @@ namespace SHADE
for (const auto EID : physicsDebugDrawSystem->collidersToDraw)
{
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->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();
if (IS_ACTIVE != RIGIDBODY_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)
{
const SHVec3& WORLD_POS = TRANSFORM_COMPONENT->GetWorldPosition();
@ -71,8 +82,19 @@ namespace SHADE
const bool COLLIDER_ACTIVE = physicsObject.collider->IsActive();
if (IS_ACTIVE != COLLIDER_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)
{
const SHVec3& WORLD_POS = TRANSFORM_COMPONENT->GetWorldPosition();