Ajout de Jolt Physics + 1ere version des factory entitecomposants - camera, transform, rigidbody, collider, renderer
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
|
||||
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <TestFramework.h>
|
||||
|
||||
#include <Tests/BroadPhase/BroadPhaseCastRayTest.h>
|
||||
#include <Jolt/Physics/Collision/RayCast.h>
|
||||
#include <Jolt/Physics/Collision/CastResult.h>
|
||||
#include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
|
||||
#include <Utils/DebugRendererSP.h>
|
||||
#include <random>
|
||||
|
||||
JPH_IMPLEMENT_RTTI_VIRTUAL(BroadPhaseCastRayTest)
|
||||
{
|
||||
JPH_ADD_BASE_CLASS(BroadPhaseCastRayTest, BroadPhaseTest)
|
||||
}
|
||||
|
||||
void BroadPhaseCastRayTest::Initialize()
|
||||
{
|
||||
BroadPhaseTest::Initialize();
|
||||
|
||||
int num_bodies = int(mBodyManager->GetMaxBodies());
|
||||
|
||||
// Create random boxes
|
||||
CreateBalancedDistribution(mBodyManager, num_bodies);
|
||||
|
||||
// Add all bodies to the broadphase
|
||||
Body **body_vector = mBodyManager->GetBodies().data();
|
||||
BodyID *bodies_to_add = new BodyID [num_bodies];
|
||||
for (int b = 0; b < num_bodies; ++b)
|
||||
bodies_to_add[b] = body_vector[b]->GetID();
|
||||
BroadPhase::AddState add_state = mBroadPhase->AddBodiesPrepare(bodies_to_add, num_bodies);
|
||||
mBroadPhase->AddBodiesFinalize(bodies_to_add, num_bodies, add_state);
|
||||
delete [] bodies_to_add;
|
||||
|
||||
// Optimize the broadphase
|
||||
mBroadPhase->Optimize();
|
||||
}
|
||||
|
||||
void BroadPhaseCastRayTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
|
||||
{
|
||||
// Create ray
|
||||
default_random_engine random;
|
||||
Vec3 from = 1000.0f * Vec3::sRandom(random);
|
||||
RayCast ray { from, -2.0f * from };
|
||||
|
||||
// Raycast before update
|
||||
AllHitCollisionCollector<RayCastBodyCollector> collector;
|
||||
mBroadPhase->CastRay(ray, collector);
|
||||
int num_hits = (int)collector.mHits.size();
|
||||
BroadPhaseCastResult *results = collector.mHits.data();
|
||||
|
||||
// Draw results
|
||||
for (int i = 0; i < num_hits; ++i)
|
||||
DrawMarkerSP(mDebugRenderer, ray.GetPointOnRay(results[i].mFraction), Color::sGreen, 10.0f);
|
||||
DrawLineSP(mDebugRenderer, ray.mOrigin, ray.mOrigin + ray.mDirection, Color::sRed);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
|
||||
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Tests/BroadPhase/BroadPhaseTest.h>
|
||||
|
||||
class BroadPhaseCastRayTest : public BroadPhaseTest
|
||||
{
|
||||
public:
|
||||
JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, BroadPhaseCastRayTest)
|
||||
|
||||
// Description of the test
|
||||
virtual const char *GetDescription() const override
|
||||
{
|
||||
return "Simple test that casts a ray through the broadphase.";
|
||||
}
|
||||
|
||||
// Initialize the test
|
||||
virtual void Initialize() override;
|
||||
|
||||
// Update the test, called before the physics update
|
||||
virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
|
||||
};
|
||||
@@ -0,0 +1,151 @@
|
||||
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
|
||||
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <TestFramework.h>
|
||||
|
||||
#include <Tests/BroadPhase/BroadPhaseInsertionTest.h>
|
||||
#include <Jolt/Physics/Collision/RayCast.h>
|
||||
#include <Jolt/Physics/Collision/CastResult.h>
|
||||
#include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
|
||||
#include <Jolt/Geometry/RayAABox.h>
|
||||
#include <Utils/Log.h>
|
||||
#include <Utils/DebugRendererSP.h>
|
||||
|
||||
JPH_IMPLEMENT_RTTI_VIRTUAL(BroadPhaseInsertionTest)
|
||||
{
|
||||
JPH_ADD_BASE_CLASS(BroadPhaseInsertionTest, BroadPhaseTest)
|
||||
}
|
||||
|
||||
void BroadPhaseInsertionTest::Initialize()
|
||||
{
|
||||
BroadPhaseTest::Initialize();
|
||||
|
||||
CreateBalancedDistribution(mBodyManager, mBodyManager->GetMaxBodies());
|
||||
}
|
||||
|
||||
void BroadPhaseInsertionTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
|
||||
{
|
||||
// Check if we need to change direction
|
||||
if (mDirection == 1 && mCurrentBody >= mBodyManager->GetMaxBodies())
|
||||
mDirection = -1;
|
||||
if (mDirection == -1 && mCurrentBody == 0)
|
||||
mDirection = 1;
|
||||
|
||||
int num_this_step = int(mBodyManager->GetMaxBodies() / 10);
|
||||
|
||||
if (mDirection < 0)
|
||||
mCurrentBody -= num_this_step;
|
||||
|
||||
Body **body_vector = mBodyManager->GetBodies().data();
|
||||
|
||||
// Randomly move bodies around
|
||||
if (mCurrentBody > 0)
|
||||
{
|
||||
const int cNumBodiesToMove = 100;
|
||||
BodyID *bodies_to_move = new BodyID [cNumBodiesToMove];
|
||||
uniform_int_distribution<int> body_selector(0, (int)mCurrentBody - 1);
|
||||
uniform_real_distribution<float> translation_selector(1.0f, 5.0f);
|
||||
for (int i = 0; i < cNumBodiesToMove; ++i)
|
||||
{
|
||||
Body &body = *body_vector[body_selector(mRandomGenerator)];
|
||||
JPH_ASSERT(body.IsInBroadPhase());
|
||||
body.SetPositionAndRotationInternal(body.GetPosition() + translation_selector(mRandomGenerator) * Vec3::sRandom(mRandomGenerator), Quat::sIdentity());
|
||||
bodies_to_move[i] = body.GetID();
|
||||
}
|
||||
mBroadPhase->NotifyBodiesAABBChanged(bodies_to_move, cNumBodiesToMove);
|
||||
delete [] bodies_to_move;
|
||||
}
|
||||
|
||||
// Create batch of bodies
|
||||
BodyID *bodies_to_add_or_remove = new BodyID [num_this_step];
|
||||
for (int b = 0; b < num_this_step; ++b)
|
||||
bodies_to_add_or_remove[b] = body_vector[mCurrentBody + b]->GetID();
|
||||
|
||||
// Add/remove them
|
||||
if (mDirection == 1)
|
||||
{
|
||||
// Prepare and abort
|
||||
BroadPhase::AddState add_state = mBroadPhase->AddBodiesPrepare(bodies_to_add_or_remove, num_this_step);
|
||||
mBroadPhase->AddBodiesAbort(bodies_to_add_or_remove, num_this_step, add_state);
|
||||
|
||||
// Prepare and add
|
||||
add_state = mBroadPhase->AddBodiesPrepare(bodies_to_add_or_remove, num_this_step);
|
||||
mBroadPhase->AddBodiesFinalize(bodies_to_add_or_remove, num_this_step, add_state);
|
||||
}
|
||||
else
|
||||
mBroadPhase->RemoveBodies(bodies_to_add_or_remove, num_this_step);
|
||||
|
||||
// Delete temp array
|
||||
delete [] bodies_to_add_or_remove;
|
||||
|
||||
// Create ray
|
||||
default_random_engine random;
|
||||
Vec3 from = 1000.0f * Vec3::sRandom(random);
|
||||
RayCast ray { from, -2.0f * from };
|
||||
|
||||
// Raycast before update
|
||||
AllHitCollisionCollector<RayCastBodyCollector> hits_before;
|
||||
mBroadPhase->CastRay(ray, hits_before);
|
||||
int num_before = (int)hits_before.mHits.size();
|
||||
BroadPhaseCastResult *results_before = hits_before.mHits.data();
|
||||
Trace("Before update: %d results found", num_before);
|
||||
|
||||
// Draw results
|
||||
DrawLineSP(mDebugRenderer, ray.mOrigin, ray.mOrigin + ray.mDirection, Color::sRed);
|
||||
for (int i = 0; i < num_before; ++i)
|
||||
DrawMarkerSP(mDebugRenderer, ray.GetPointOnRay(results_before[i].mFraction), Color::sGreen, 10.0f);
|
||||
|
||||
// Update the broadphase
|
||||
mBroadPhase->Optimize();
|
||||
|
||||
// Raycast after update
|
||||
AllHitCollisionCollector<RayCastBodyCollector> hits_after;
|
||||
mBroadPhase->CastRay(ray, hits_after);
|
||||
int num_after = (int)hits_after.mHits.size();
|
||||
BroadPhaseCastResult *results_after = hits_after.mHits.data();
|
||||
Trace("After update: %d results found", num_after);
|
||||
|
||||
// Before update we may have some false hits, check that there are less hits after update than before
|
||||
if (num_after > num_before)
|
||||
FatalError("BroadPhaseInsertionTest: After has more hits than before");
|
||||
for (BroadPhaseCastResult *ra = results_after, *ra_end = results_after + num_after; ra < ra_end; ++ra)
|
||||
{
|
||||
bool found = false;
|
||||
for (BroadPhaseCastResult *rb = results_before, *rb_end = results_before + num_before; rb < rb_end; ++rb)
|
||||
if (ra->mBodyID == rb->mBodyID)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
if (!found)
|
||||
FatalError("BroadPhaseInsertionTest: Result after not found in result before");
|
||||
}
|
||||
|
||||
// Validate with brute force approach
|
||||
for (const Body *b : mBodyManager->GetBodies())
|
||||
{
|
||||
bool found = false;
|
||||
for (BroadPhaseCastResult *r = results_after, *r_end = results_after + num_after; r < r_end; ++r)
|
||||
if (r->mBodyID == b->GetID())
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (b->IsInBroadPhase()
|
||||
&& RayAABoxHits(ray.mOrigin, ray.mDirection, b->GetWorldSpaceBounds().mMin, b->GetWorldSpaceBounds().mMax))
|
||||
{
|
||||
if (!found)
|
||||
FatalError("BroadPhaseInsertionTest: Is intersecting but was not found");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (found)
|
||||
FatalError("BroadPhaseInsertionTest: Is not intersecting but was found");
|
||||
}
|
||||
}
|
||||
|
||||
if (mDirection > 0)
|
||||
mCurrentBody += num_this_step;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
|
||||
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Tests/BroadPhase/BroadPhaseTest.h>
|
||||
#include <random>
|
||||
|
||||
class BroadPhaseInsertionTest : public BroadPhaseTest
|
||||
{
|
||||
public:
|
||||
JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, BroadPhaseInsertionTest)
|
||||
|
||||
// Description of the test
|
||||
virtual const char * GetDescription() const override
|
||||
{
|
||||
return "Test that adds/removes objects to/from the broadphase and casts a ray through the boxes to see if the collision results are correct.";
|
||||
}
|
||||
|
||||
// Initialize the test
|
||||
virtual void Initialize() override;
|
||||
|
||||
// Update the test, called before the physics update
|
||||
virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
|
||||
|
||||
private:
|
||||
default_random_engine mRandomGenerator;
|
||||
size_t mCurrentBody = 0;
|
||||
int mDirection = 1;
|
||||
};
|
||||
@@ -0,0 +1,70 @@
|
||||
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
|
||||
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <TestFramework.h>
|
||||
|
||||
#include <Tests/BroadPhase/BroadPhaseTest.h>
|
||||
#include <Jolt/Physics/Collision/Shape/BoxShape.h>
|
||||
#include <Jolt/Physics/Body/BodyCreationSettings.h>
|
||||
#include <Jolt/Physics/Collision/BroadPhase/BroadPhaseBruteForce.h>
|
||||
#include <Jolt/Physics/Collision/BroadPhase/BroadPhaseQuadTree.h>
|
||||
#include <random>
|
||||
|
||||
JPH_IMPLEMENT_RTTI_ABSTRACT(BroadPhaseTest)
|
||||
{
|
||||
JPH_ADD_BASE_CLASS(BroadPhaseTest, Test)
|
||||
}
|
||||
|
||||
#define NUM_BODIES 10000
|
||||
|
||||
//#define BROAD_PHASE BroadPhaseBruteForce()
|
||||
#define BROAD_PHASE BroadPhaseQuadTree()
|
||||
|
||||
BroadPhaseTest::~BroadPhaseTest()
|
||||
{
|
||||
delete mBroadPhase;
|
||||
delete mBodyManager;
|
||||
}
|
||||
|
||||
void BroadPhaseTest::CreateBalancedDistribution(BodyManager *inBodyManager, int inNumBodies, float inEnvironmentSize)
|
||||
{
|
||||
default_random_engine random(0x1ee7c0de);
|
||||
uniform_real_distribution<float> zero_to_one(0.0f, 1.0f);
|
||||
float n = float(inNumBodies);
|
||||
Vec3 max_box_start = Vec3::sReplicate(inEnvironmentSize * (1.0f - pow(n, -1.0f / 3.0f)));
|
||||
Vec3 min_box_size = Vec3::sReplicate(1.0f / inEnvironmentSize);
|
||||
Vec3 max_box_size = Vec3::sReplicate(inEnvironmentSize * pow(n, -1.0f / 3.0f)) - min_box_size;
|
||||
for (int b = 0; b < inNumBodies; ++b)
|
||||
{
|
||||
AABox box;
|
||||
box.mMin = max_box_start * Vec3(zero_to_one(random), zero_to_one(random), zero_to_one(random)) - Vec3::sReplicate(0.5f * inEnvironmentSize);
|
||||
box.mMax = box.mMin + min_box_size + max_box_size * Vec3(zero_to_one(random), zero_to_one(random), zero_to_one(random));
|
||||
|
||||
BodyCreationSettings s;
|
||||
s.SetShape(new BoxShape(box.GetExtent(), 0.0f));
|
||||
s.mPosition = RVec3(box.GetCenter());
|
||||
s.mRotation = Quat::sIdentity();
|
||||
s.mObjectLayer = (random() % 10) == 0? Layers::MOVING : Layers::NON_MOVING;
|
||||
Body *body = inBodyManager->AllocateBody(s);
|
||||
inBodyManager->AddBody(body);
|
||||
}
|
||||
}
|
||||
|
||||
void BroadPhaseTest::Initialize()
|
||||
{
|
||||
// Create body manager
|
||||
mBodyManager = new BodyManager();
|
||||
mBodyManager->Init(NUM_BODIES, 0, mBroadPhaseLayerInterface);
|
||||
|
||||
// Create broadphase
|
||||
mBroadPhase = new BROAD_PHASE;
|
||||
mBroadPhase->Init(mBodyManager, mBroadPhaseLayerInterface);
|
||||
}
|
||||
|
||||
void BroadPhaseTest::PostPhysicsUpdate(float inDeltaTime)
|
||||
{
|
||||
#ifdef JPH_DEBUG_RENDERER
|
||||
mBodyManager->Draw(BodyManager::DrawSettings(), PhysicsSettings(), mDebugRenderer);
|
||||
#endif // JPH_DEBUG_RENDERER
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
|
||||
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Tests/Test.h>
|
||||
#include <Jolt/Physics/Body/BodyManager.h>
|
||||
#include <Jolt/Physics/Collision/BroadPhase/BroadPhase.h>
|
||||
#include <Layers.h>
|
||||
|
||||
// Base class for a test involving only the broad phase
|
||||
class BroadPhaseTest : public Test
|
||||
{
|
||||
public:
|
||||
JPH_DECLARE_RTTI_ABSTRACT(JPH_NO_EXPORT, BroadPhaseTest)
|
||||
|
||||
// Destructor
|
||||
virtual ~BroadPhaseTest() override;
|
||||
|
||||
// Initialize the test
|
||||
virtual void Initialize() override;
|
||||
|
||||
// Update the test, called after the physics update
|
||||
virtual void PostPhysicsUpdate(float inDeltaTime) override;
|
||||
|
||||
protected:
|
||||
// Create bodies according to method outlined in "FAST SOFTWARE FOR BOX INTERSECTIONS by AFRA ZOMORODIAN" section "The balanced distribution"
|
||||
// http://pub.ist.ac.at/~edels/Papers/2002-J-01-FastBoxIntersection.pdf
|
||||
void CreateBalancedDistribution(BodyManager *inBodyManager, int inNumBodies, float inEnvironmentSize = 512.0f);
|
||||
|
||||
BPLayerInterfaceImpl mBroadPhaseLayerInterface;
|
||||
BroadPhase * mBroadPhase = nullptr;
|
||||
BodyManager * mBodyManager = nullptr;
|
||||
};
|
||||
Reference in New Issue
Block a user