Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Physics::Utils::ContactListenerImpl Class Referencefinal

ContactListener implementation. More...

#include <ContactListenerImpl.hpp>

Inheritance diagram for Physics::Utils::ContactListenerImpl:

Public Member Functions

 ContactListenerImpl ()=delete
 ContactListenerImpl (Engine::Core &core)
 ~ContactListenerImpl () override=default
JPH::ValidateResult OnContactValidate (const JPH::Body &inBody1, const JPH::Body &inBody2, JPH::RVec3Arg inBaseOffset, const JPH::CollideShapeResult &inCollisionResult) override
void OnContactAdded (const JPH::Body &inBody1, const JPH::Body &inBody2, const JPH::ContactManifold &inManifold, JPH::ContactSettings &ioSettings) override
 Called when two bodies start to make contact.
void OnContactPersisted (const JPH::Body &inBody1, const JPH::Body &inBody2, const JPH::ContactManifold &inManifold, JPH::ContactSettings &ioSettings) override
 Called when two bodies continue to make contact.
void OnContactRemoved (const JPH::SubShapeIDPair &inSubShapePair) override
 Called when two bodies cease to make contact.
void ProcessBufferedEvents (Engine::Core &core)
 Flush buffered events to the main thread EventManager (should be called from main thread).

Private Attributes

Engine::Core_core
std::mutex _bufferMutex
std::vector< Physics::Event::CollisionAddedEvent_bufferedAdded
std::vector< Physics::Event::CollisionPersistedEvent_bufferedPersisted
std::vector< Physics::Event::CollisionRemovedEvent_bufferedRemoved

Detailed Description

ContactListener implementation.

This is used to define callbacks for contact events between bodies. Callbacks will be called with the Core, as well as the two entities that collided.

Note
Jolt will call these callbacks from physics worker threads. To avoid concurrent access to engine resources (like the entt registry) and the EventManager, events are buffered in worker threads and flushed to the main thread by calling ProcessBufferedEvents(core) from the main physics update system. Do NOT call EventManager::PushEvent() directly in these callbacks.

Constructor & Destructor Documentation

◆ ContactListenerImpl() [1/2]

Physics::Utils::ContactListenerImpl::ContactListenerImpl ( )
delete

◆ ContactListenerImpl() [2/2]

Physics::Utils::ContactListenerImpl::ContactListenerImpl ( Engine::Core & core)
inlineexplicit

◆ ~ContactListenerImpl()

Physics::Utils::ContactListenerImpl::~ContactListenerImpl ( )
overridedefault

Member Function Documentation

◆ OnContactAdded()

void Physics::Utils::ContactListenerImpl::OnContactAdded ( const JPH::Body & inBody1,
const JPH::Body & inBody2,
const JPH::ContactManifold & inManifold,
JPH::ContactSettings & ioSettings )
override

Called when two bodies start to make contact.

Right now we use 32 bits for entities IDs with EnTT but Jolt stores user data as 64 bits so we have to mask the upper 32 bits auto entity1 = static_cast<Engine::Entity>(inBody1.GetUserData() & ENTITY_ID_MASK); auto entity2 = static_cast<Engine::Entity>(inBody2.GetUserData() & ENTITY_ID_MASK);

Validate entity ids and buffer the event on a worker thread; it will be flushed on the main thread by the PhysicsUpdate system.

Buffer the event. Can't safely access the registry from a physics worker thread, so defer any entity validity checks until ProcessBufferedEvents (main thread).

Parameters
inBody1The first body involved in the contact.
inBody2The second body involved in the contact.
inManifoldThe contact manifold containing contact points.
ioSettingsContact settings that can be modified.

◆ OnContactPersisted()

void Physics::Utils::ContactListenerImpl::OnContactPersisted ( const JPH::Body & inBody1,
const JPH::Body & inBody2,
const JPH::ContactManifold & inManifold,
JPH::ContactSettings & ioSettings )
override

Called when two bodies continue to make contact.

Same as OnContactAdded: delay entity validation until ProcessBufferedEvents (main thread)

Parameters
inBody1The first body involved in the contact.
inBody2The second body involved in the contact.
inManifoldThe contact manifold containing contact points.
ioSettingsContact settings that can be modified.

◆ OnContactRemoved()

void Physics::Utils::ContactListenerImpl::OnContactRemoved ( const JPH::SubShapeIDPair & inSubShapePair)
override

Called when two bodies cease to make contact.

Buffer the event on a worker thread; it will be flushed on the main thread by the PhysicsUpdate system.

Parameters
inSubShapePairThe pair of sub-shapes that were in contact.

◆ OnContactValidate()

JPH::ValidateResult Physics::Utils::ContactListenerImpl::OnContactValidate ( const JPH::Body & inBody1,
const JPH::Body & inBody2,
JPH::RVec3Arg inBaseOffset,
const JPH::CollideShapeResult & inCollisionResult )
inlineoverride

◆ ProcessBufferedEvents()

void Physics::Utils::ContactListenerImpl::ProcessBufferedEvents ( Engine::Core & core)

Flush buffered events to the main thread EventManager (should be called from main thread).

Move buffers into local copies under lock then push events to the EventManager from main thread.

Member Data Documentation

◆ _bufferedAdded

std::vector<Physics::Event::CollisionAddedEvent> Physics::Utils::ContactListenerImpl::_bufferedAdded
private

◆ _bufferedPersisted

std::vector<Physics::Event::CollisionPersistedEvent> Physics::Utils::ContactListenerImpl::_bufferedPersisted
private

◆ _bufferedRemoved

std::vector<Physics::Event::CollisionRemovedEvent> Physics::Utils::ContactListenerImpl::_bufferedRemoved
private

◆ _bufferMutex

std::mutex Physics::Utils::ContactListenerImpl::_bufferMutex
private

◆ _core

Engine::Core& Physics::Utils::ContactListenerImpl::_core
private

The documentation for this class was generated from the following files: