-
-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented a basic collision listener class
Made sure API import also defines default visibility and added pool setting Tweaked Jolt build settings and warning options for the thrive lib Started on a few various files, tweaked some stuff, setup clang tidy rules more sensibly
- Loading branch information
1 parent
324a6fa
commit 4edd032
Showing
11 changed files
with
194 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
--- | ||
Checks: '*,-llvmlibc-implementation-in-namespace,-llvm-header-guard,-modernize-use-trailing-return-type' | ||
Checks: '-*,boost-*,bugprone-*,cert-*,clang-analyzer-*,concurrency-*,cppcoreguidelines-*,hicpp-*, | ||
llvm-*,-llvm-header-guard,misc-*,-misc-no-recursion,modernize-*,-modernize-use-trailing-return-type, | ||
performance-*,portability-*,readability-*' | ||
ShortStatementLines: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
/// \file Forward definitions for various important classes in this library for reducing header include amounts | ||
|
||
namespace Thrive | ||
{ | ||
class TaskSystem; | ||
|
||
namespace Physics | ||
{ | ||
class ContactListener; | ||
} // namespace Physics | ||
} // namespace Thrive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <mutex> | ||
#include <shared_mutex> | ||
|
||
// Jolt says the mutex is not fast for all platforms so here's some flexibility to allow redefining it | ||
|
||
namespace Thrive | ||
{ | ||
using Mutex = std::mutex; | ||
using SharedMutex = std::shared_mutex; | ||
using Lock = std::lock_guard<Mutex>; | ||
using SharedLock = std::lock_guard<SharedMutex>; | ||
} // namespace Thrive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// ------------------------------------ // | ||
#include "ContactListener.hpp" | ||
|
||
#include <Jolt/Physics/Body/Body.h> | ||
// ------------------------------------ // | ||
using namespace Thrive::Physics; | ||
|
||
JPH::ValidateResult ContactListener::OnContactValidate(const JPH::Body& body1, const JPH::Body& body2, | ||
JPH::RVec3Arg baseOffset, const JPH::CollideShapeResult& collisionResult) | ||
{ | ||
JPH::ValidateResult result; | ||
if (chainedListener != nullptr) | ||
{ | ||
result = chainedListener->OnContactValidate(body1, body2, baseOffset, collisionResult); | ||
} | ||
else | ||
{ | ||
result = JPH::ContactListener::OnContactValidate(body1, body2, baseOffset, collisionResult); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
void ContactListener::OnContactAdded(const JPH::Body& body1, const JPH::Body& body2, | ||
const JPH::ContactManifold& manifold, JPH::ContactSettings& settings) | ||
{ | ||
// Note the bodies are sorted (at least the sample Jolt code has asserts to verify it, so we can probably safely | ||
// always assume that) `body1.GetID() < body2.GetID()` | ||
|
||
// Add the new collision | ||
{ | ||
Lock lock(currentCollisionsMutex); | ||
JPH::SubShapeIDPair key(body1.GetID(), manifold.mSubShapeID1, body2.GetID(), manifold.mSubShapeID2); | ||
currentCollisions[key] = CollisionPair(manifold.mBaseOffset, manifold.mRelativeContactPointsOn1); | ||
} | ||
|
||
if (chainedListener != nullptr) | ||
chainedListener->OnContactAdded(body1, body2, manifold, settings); | ||
} | ||
|
||
void ContactListener::OnContactPersisted(const JPH::Body& body1, const JPH::Body& body2, | ||
const JPH::ContactManifold& manifold, JPH::ContactSettings& settings) | ||
{ | ||
// Update existing collision info | ||
{ | ||
Lock lock(currentCollisionsMutex); | ||
|
||
JPH::SubShapeIDPair key(body1.GetID(), manifold.mSubShapeID1, body2.GetID(), manifold.mSubShapeID2); | ||
|
||
const auto iter = currentCollisions.find(key); | ||
if (iter != currentCollisions.end()) | ||
{ | ||
iter->second = CollisionPair(manifold.mBaseOffset, manifold.mRelativeContactPointsOn1); | ||
} | ||
} | ||
|
||
if (chainedListener != nullptr) | ||
chainedListener->OnContactPersisted(body1, body2, manifold, settings); | ||
} | ||
|
||
void ContactListener::OnContactRemoved(const JPH::SubShapeIDPair& subShapePair) | ||
{ | ||
// Remove the contact | ||
{ | ||
Lock lock(currentCollisionsMutex); | ||
|
||
const auto iter = currentCollisions.find(subShapePair); | ||
if (iter != currentCollisions.end()) | ||
currentCollisions.erase(iter); | ||
} | ||
|
||
if (chainedListener != nullptr) | ||
chainedListener->OnContactRemoved(subShapePair); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <Jolt/Physics/Collision/ContactListener.h> | ||
|
||
#include "core/Mutex.hpp" | ||
|
||
namespace Thrive::Physics | ||
{ | ||
/// \brief Contact listener implementation | ||
class ContactListener : public JPH::ContactListener | ||
{ | ||
using CollisionPair = std::pair<JPH::RVec3, JPH::ContactPoints>; | ||
|
||
public: | ||
JPH::ValidateResult OnContactValidate(const JPH::Body& body1, const JPH::Body& body2, JPH::RVec3Arg baseOffset, | ||
const JPH::CollideShapeResult& collisionResult) override; | ||
|
||
void OnContactAdded(const JPH::Body& body1, const JPH::Body& body2, const JPH::ContactManifold& manifold, | ||
JPH::ContactSettings& settings) override; | ||
|
||
void OnContactPersisted(const JPH::Body& body1, const JPH::Body& body2, const JPH::ContactManifold& manifold, | ||
JPH::ContactSettings& settings) override; | ||
|
||
void OnContactRemoved(const JPH::SubShapeIDPair& subShapePair) override; | ||
|
||
inline void SetNextListener(JPH::ContactListener* listener) | ||
{ | ||
chainedListener = listener; | ||
} | ||
|
||
private: | ||
Mutex currentCollisionsMutex; | ||
|
||
// TODO: JPH seems to use a custom allocator here so we might need to do so as well (for performance) | ||
std::unordered_map<JPH::SubShapeIDPair, CollisionPair> currentCollisions; | ||
|
||
JPH::ContactListener* chainedListener = nullptr; | ||
}; | ||
|
||
} // namespace Thrive::Physics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
# Native third party module references | ||
message(STATUS "TODO: do these native third party modules") | ||
|
||
# JoltPhysics | ||
|
||
# Might as well get used to double precision impact now as we'll want that eventually anyway | ||
set(DOUBLE_PRECISION ON) | ||
|
||
# TODO: do we need to turn this off (requiring too new AMD CPUs)? | ||
set(USE_AVX2 ON) | ||
|
||
# This is way too unsupported to enable in a general build | ||
set(USE_AVX512 OFF) | ||
|
||
add_subdirectory(JoltPhysics/Build) |