Merge pull request #273 from SHADE-DP/SP3-2-Physics

Added extensive Vector3 Rotation methods in Managed code & Debug draw is always available (temporarily).
This commit is contained in:
XiaoQiDigipen 2022-11-24 12:25:48 +08:00 committed by GitHub
commit 982760fb20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 44 deletions

View File

@ -120,21 +120,25 @@ namespace SHADE
} }
rp3d::DebugRenderer* rp3dRenderer = nullptr; rp3d::DebugRenderer* rp3dRenderer = nullptr;
#ifdef SHEDITOR if (system->physicsSystem->worldState.world)
const auto* EDITOR = SHSystemManager::GetSystem<SHEditor>();
if (EDITOR && EDITOR->editorState != SHEditor::State::STOP)
{
rp3dRenderer = &system->physicsSystem->worldState.world->getDebugRenderer(); rp3dRenderer = &system->physicsSystem->worldState.world->getDebugRenderer();
rp3dRenderer->setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_POINT, false);
rp3dRenderer->setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_NORMAL, false);
}
#endif
for (int i = 0; i < SHUtilities::ConvertEnum(DebugDrawFlags::NUM_FLAGS); ++i) for (int i = 0; i < SHUtilities::ConvertEnum(DebugDrawFlags::NUM_FLAGS); ++i)
{ {
const bool DRAW = (system->debugDrawFlags & (1U << i)) > 0; const bool DRAW = (system->debugDrawFlags & (1U << i)) > 0;
if (DRAW) if (DRAW)
{
drawFunctions[i](debugDrawSystem, rp3dRenderer); drawFunctions[i](debugDrawSystem, rp3dRenderer);
}
else
{
if (rp3dRenderer && (i == 3 || i == 4))
{
rp3dRenderer->setIsDebugItemDisplayed(reactphysics3d::DebugRenderer::DebugItem::CONTACT_POINT, false);
rp3dRenderer->setIsDebugItemDisplayed(reactphysics3d::DebugRenderer::DebugItem::CONTACT_NORMAL, false);
}
}
} }
// Automatically clear the container of raycasts despite debug drawing state // Automatically clear the container of raycasts despite debug drawing state
@ -180,6 +184,7 @@ namespace SHADE
void SHPhysicsDebugDrawSystem::drawContactPoints(SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept void SHPhysicsDebugDrawSystem::drawContactPoints(SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept
{ {
#ifdef SHEDITOR #ifdef SHEDITOR
const auto* EDITOR = SHSystemManager::GetSystem<SHEditor>(); const auto* EDITOR = SHSystemManager::GetSystem<SHEditor>();
if (EDITOR && EDITOR->editorState != SHEditor::State::STOP) if (EDITOR && EDITOR->editorState != SHEditor::State::STOP)
{ {
@ -192,6 +197,18 @@ namespace SHADE
for (int i = 0; i < NUM_TRIS; ++i) for (int i = 0; i < NUM_TRIS; ++i)
debugRenderer->DrawTri(SHColour::RED, TRI_ARRAY[i].point1, TRI_ARRAY[i].point2, TRI_ARRAY[i].point3); debugRenderer->DrawTri(SHColour::RED, TRI_ARRAY[i].point1, TRI_ARRAY[i].point2, TRI_ARRAY[i].point3);
} }
#else
rp3dRenderer->setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_POINT, true);
const int NUM_TRIS = static_cast<int>(rp3dRenderer->getNbTriangles());
if (NUM_TRIS == 0)
return;
const auto& TRI_ARRAY = rp3dRenderer->getTrianglesArray();
for (int i = 0; i < NUM_TRIS; ++i)
debugRenderer->DrawTri(SHColour::RED, TRI_ARRAY[i].point1, TRI_ARRAY[i].point2, TRI_ARRAY[i].point3);
#endif #endif
} }
@ -210,6 +227,18 @@ namespace SHADE
for (int i = 0; i < NUM_LINES; ++i) for (int i = 0; i < NUM_LINES; ++i)
debugRenderer->DrawLine(SHColour::RED, LINE_ARRAY[i].point1, LINE_ARRAY[i].point2); debugRenderer->DrawLine(SHColour::RED, LINE_ARRAY[i].point1, LINE_ARRAY[i].point2);
} }
#else
rp3dRenderer->setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_NORMAL, true);
const int NUM_LINES = static_cast<int>(rp3dRenderer->getNbLines());
if (NUM_LINES == 0)
return;
const auto& LINE_ARRAY = rp3dRenderer->getLinesArray();
for (int i = 0; i < NUM_LINES; ++i)
debugRenderer->DrawLine(SHColour::RED, LINE_ARRAY[i].point1, LINE_ARRAY[i].point2);
#endif #endif
} }

View File

@ -163,8 +163,6 @@ namespace SHADE
// Destroy an existing world // Destroy an existing world
if (worldState.world != nullptr) if (worldState.world != nullptr)
{ {
objectManager.RemoveAllObjects(); objectManager.RemoveAllObjects();
objectManager.SetWorld(nullptr); objectManager.SetWorld(nullptr);
@ -174,12 +172,8 @@ namespace SHADE
worldState.DestroyWorld(factory); worldState.DestroyWorld(factory);
} }
worldState.CreateWorld(factory); worldState.CreateWorld(factory);
#ifdef _PUBLISH worldState.world->setIsDebugRenderingEnabled(true);
worldState.world->setIsDebugRenderingEnabled(false);
#else
worldState.world->setIsDebugRenderingEnabled(true);
#endif
// Link Collision Listener & Raycaster // Link Collision Listener & Raycaster
collisionListener.BindToWorld(worldState.world); collisionListener.BindToWorld(worldState.world);
@ -444,11 +438,7 @@ namespace SHADE
return onPlayEvent->handle; return onPlayEvent->handle;
worldState.CreateWorld(factory); worldState.CreateWorld(factory);
#ifdef _PUBLISH
worldState.world->setIsDebugRenderingEnabled(false);
#else
worldState.world->setIsDebugRenderingEnabled(true); worldState.world->setIsDebugRenderingEnabled(true);
#endif
// Link Collision Listener & Raycaster // Link Collision Listener & Raycaster
collisionListener.BindToWorld(worldState.world); collisionListener.BindToWorld(worldState.world);

View File

@ -334,10 +334,6 @@ namespace SHADE
if (physicsObject.GetRigidBody()->isActive()) if (physicsObject.GetRigidBody()->isActive())
physicsObject.prevTransform = CURRENT_TF; physicsObject.prevTransform = CURRENT_TF;
// Skip sleeping objects
if (physicsObject.GetRigidBody()->isSleeping())
return;
// Sync with rigid bodies // Sync with rigid bodies
if (rigidBodyComponent && SHSceneManager::CheckNodeAndComponentsActive<SHRigidBodyComponent>(physicsObject.entityID)) if (rigidBodyComponent && SHSceneManager::CheckNodeAndComponentsActive<SHRigidBodyComponent>(physicsObject.entityID))
{ {
@ -369,7 +365,7 @@ namespace SHADE
colliderComponent->orientation = CURRENT_TF.getOrientation(); colliderComponent->orientation = CURRENT_TF.getOrientation();
} }
// Set transform for rendering // Set transform for rendering
if (transformComponent) if (transformComponent)
{ {
transformComponent->SetWorldPosition(renderPos); transformComponent->SetWorldPosition(renderPos);

View File

@ -21,6 +21,9 @@ of DigiPen Institute of Technology is prohibited.
#include <algorithm> #include <algorithm>
// Project Headers // Project Headers
#include "Math.hxx" #include "Math.hxx"
#include "Quaternion.hxx"
#include "Math/Vector/SHVec3.h"
#include "Utility/Convert.hxx"
namespace SHADE namespace SHADE
{ {
@ -148,21 +151,21 @@ namespace SHADE
{ {
return vec - (Project(vec, normal.GetNormalised()) * 2.0f); return vec - (Project(vec, normal.GetNormalised()) * 2.0f);
} }
Vector3 Vector3::RotateRadians(Vector3 vec, float radians) Vector3 Vector3::RotateX(Vector3 vec, float radians)
{ {
const float SINE = sin(radians); return Convert::ToCLI(SHVec3::RotateX(Convert::ToNative(vec), radians));
const float COSINE = cos(radians);
return Vector3
(
vec.x * COSINE - vec.y * SINE,
vec.x * SINE + vec.y * COSINE,
vec.z
);
} }
Vector3 Vector3::RotateDegrees(Vector3 vec, float degrees) Vector3 Vector3::RotateY(Vector3 vec, float radians)
{ {
return RotateRadians(vec, Math::DegreesToRadians(degrees)); return Convert::ToCLI(SHVec3::RotateY(Convert::ToNative(vec), radians));
}
Vector3 Vector3::RotateZ(Vector3 vec, float radians)
{
return Convert::ToCLI(SHVec3::RotateZ(Convert::ToNative(vec), radians));
}
Vector3 Vector3::Rotate(Vector3 vec, Vector3 axis, float radians)
{
return Convert::ToCLI(SHVec3::Rotate(Convert::ToNative(vec), Convert::ToNative(axis), radians));
} }
Vector3 Vector3::Min(Vector3 lhs, Vector3 rhs) Vector3 Vector3::Min(Vector3 lhs, Vector3 rhs)
{ {

View File

@ -19,6 +19,8 @@ of DigiPen Institute of Technology is prohibited.
// Project Includes // Project Includes
#include "Vector2.hxx" #include "Vector2.hxx"
value struct Quaternion;
namespace SHADE namespace SHADE
{ {
///<summary> ///<summary>
@ -266,7 +268,7 @@ namespace SHADE
/// <returns>The Vector3 that represents vec reflected across normal.</returns> /// <returns>The Vector3 that represents vec reflected across normal.</returns>
static Vector3 Reflect(Vector3 vec, Vector3 normal); static Vector3 Reflect(Vector3 vec, Vector3 normal);
/// <summary> /// <summary>
/// Rotates a Vector3 on the Z-axis by a specified angle in an anti-clockwise /// Rotates a Vector3 about the X-axis by a specified angle in an anti-clockwise
/// direction. /// direction.
/// </summary> /// </summary>
/// <param name="vec">A Vector3 to rotate.</param> /// <param name="vec">A Vector3 to rotate.</param>
@ -274,17 +276,37 @@ namespace SHADE
/// Angle to rotate the vector by in an anti-clockwise direction in radians. /// Angle to rotate the vector by in an anti-clockwise direction in radians.
/// </param> /// </param>
/// <returns>The Vector3 that represents the rotated vector.</returns> /// <returns>The Vector3 that represents the rotated vector.</returns>
static Vector3 RotateRadians(Vector3 vec, float radians); static Vector3 RotateX(Vector3 vec, float radians);
/// <summary> /// <summary>
/// Rotates a Vector3 on the Z-axis by a specified angle in an anti-clockwise /// Rotates a Vector3 about the Y-axis by a specified angle in an anti-clockwise
/// direction. /// direction.
/// </summary> /// </summary>
/// <param name="vec">A Vector3 to rotate.</param> /// <param name="vec">A Vector3 to rotate.</param>
/// <param name="degrees"> /// <param name="radians">
/// Angle to rotate the vector by in an anti-clockwise direction in degrees. /// Angle to rotate the vector by in an anti-clockwise direction in radians.
/// </param> /// </param>
/// <returns>The Vector3 that represents the rotated vector.</returns> /// <returns>The Vector3 that represents the rotated vector.</returns>
static Vector3 RotateDegrees(Vector3 vec, float degrees); static Vector3 RotateY(Vector3 vec, float radians);
/// <summary>
/// Rotates a Vector3 about the Z-axis by a specified angle in an anti-clockwise
/// direction.
/// </summary>
/// <param name="vec">A Vector3 to rotate.</param>
/// <param name="radians">
/// Angle to rotate the vector by in an anti-clockwise direction in radians.
/// </param>
/// <returns>The Vector3 that represents the rotated vector.</returns>
static Vector3 RotateZ(Vector3 vec, float radians);
/// <summary>
/// Rotates a Vector3 about an arbitrary axis by a specified angle in an anti-clockwise
/// direction.
/// </summary>
/// <param name="vec">A Vector3 to rotate.</param>
/// <param name="radians">
/// Angle to rotate the vector by in an anti-clockwise direction in radians.
/// </param>
/// <returns>The Vector3 that represents the rotated vector.</returns>
static Vector3 Rotate(Vector3 vec, Vector3 axis, float radians);
/// <summary> /// <summary>
/// Computes and returns a Vector3 that is made from the smallest components of /// Computes and returns a Vector3 that is made from the smallest components of
/// the two specified Vector3s. /// the two specified Vector3s.

View File

@ -25,7 +25,6 @@ of DigiPen Institute of Technology is prohibited.
// Project Includes // Project Includes
#include "Engine/Entity.hxx" #include "Engine/Entity.hxx"
#include "Math/Vector2.hxx" #include "Math/Vector2.hxx"
#include "Math/Vector3.hxx"
#include "Math/Quaternion.hxx" #include "Math/Quaternion.hxx"
#include "Math/Ray.hxx" #include "Math/Ray.hxx"
#include "Physics/RaycastHit.hxx" #include "Physics/RaycastHit.hxx"
@ -34,6 +33,8 @@ of DigiPen Institute of Technology is prohibited.
#include "Graphics/Color.hxx" #include "Graphics/Color.hxx"
#include "Physics/Collision/SHPhysicsRaycastResult.h" #include "Physics/Collision/SHPhysicsRaycastResult.h"
value struct Vector3;
namespace SHADE namespace SHADE
{ {
/// <summary> /// <summary>