From 740d2b978352b16943b24594572586d95d476466 Mon Sep 17 00:00:00 2001 From: Tigran Gasparian Date: Tue, 26 Oct 2021 18:31:33 +0200 Subject: Adds a request body info command for the physics direct command. This is useful to incrementally sync the body state in the physics clients of plugins (e.g. in response to a Body Added notification). --- examples/SharedMemory/PhysicsClientC_API.cpp | 13 ++++++++++ examples/SharedMemory/PhysicsClientC_API.h | 3 +++ examples/SharedMemory/PhysicsDirect.cpp | 38 ++++++++++++++++------------ examples/SharedMemory/PhysicsDirect.h | 2 ++ 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/examples/SharedMemory/PhysicsClientC_API.cpp b/examples/SharedMemory/PhysicsClientC_API.cpp index 5b070713a..75f48e39d 100644 --- a/examples/SharedMemory/PhysicsClientC_API.cpp +++ b/examples/SharedMemory/PhysicsClientC_API.cpp @@ -3997,6 +3997,19 @@ B3_SHARED_API b3SharedMemoryCommandHandle b3InitSyncBodyInfoCommand(b3PhysicsCli return (b3SharedMemoryCommandHandle)command; } +B3_SHARED_API b3SharedMemoryCommandHandle b3InitRequestBodyInfoCommand(b3PhysicsClientHandle physClient, int bodyUniqueId) +{ + PhysicsClient* cl = (PhysicsClient*)physClient; + b3Assert(cl); + b3Assert(cl->canSubmitCommand()); + struct SharedMemoryCommand* command = cl->getAvailableSharedMemoryCommand(); + b3Assert(command); + + command->m_type = CMD_REQUEST_BODY_INFO; + command->m_sdfRequestInfoArgs.m_bodyUniqueId = bodyUniqueId; + return (b3SharedMemoryCommandHandle)command; +} + B3_SHARED_API b3SharedMemoryCommandHandle b3InitSyncUserDataCommand(b3PhysicsClientHandle physClient) { PhysicsClient* cl = (PhysicsClient*)physClient; diff --git a/examples/SharedMemory/PhysicsClientC_API.h b/examples/SharedMemory/PhysicsClientC_API.h index b6ec6d63b..c14feb82f 100644 --- a/examples/SharedMemory/PhysicsClientC_API.h +++ b/examples/SharedMemory/PhysicsClientC_API.h @@ -106,6 +106,9 @@ extern "C" ///If you re-connected to an existing server, or server changed otherwise, sync the body info and user constraints etc. B3_SHARED_API b3SharedMemoryCommandHandle b3InitSyncBodyInfoCommand(b3PhysicsClientHandle physClient); + // Sync the body info of a single body. Useful when a new body has been added by a different client (e,g, when detecting through a body added notification). + B3_SHARED_API b3SharedMemoryCommandHandle b3InitRequestBodyInfoCommand(b3PhysicsClientHandle physClient, int bodyUniqueId); + B3_SHARED_API b3SharedMemoryCommandHandle b3InitRemoveBodyCommand(b3PhysicsClientHandle physClient, int bodyUniqueId); ///return the total number of bodies in the simulation diff --git a/examples/SharedMemory/PhysicsDirect.cpp b/examples/SharedMemory/PhysicsDirect.cpp index ef72e7e19..a8aa72a60 100644 --- a/examples/SharedMemory/PhysicsDirect.cpp +++ b/examples/SharedMemory/PhysicsDirect.cpp @@ -1008,21 +1008,7 @@ void PhysicsDirect::postProcessStatus(const struct SharedMemoryStatus& serverCmd m_data->m_tmpInfoRequestCommand.m_type = CMD_REQUEST_BODY_INFO; m_data->m_tmpInfoRequestCommand.m_sdfRequestInfoArgs.m_bodyUniqueId = bodyUniqueId; - - bool hasStatus = m_data->m_commandProcessor->processCommand(m_data->m_tmpInfoRequestCommand, m_data->m_tmpInfoStatus, &m_data->m_bulletStreamDataServerToClient[0], SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE); - - b3Clock clock; - double startTime = clock.getTimeInSeconds(); - double timeOutInSeconds = m_data->m_timeOutInSeconds; - while ((!hasStatus) && (clock.getTimeInSeconds() - startTime < timeOutInSeconds)) - { - hasStatus = m_data->m_commandProcessor->receiveStatus(m_data->m_tmpInfoStatus, &m_data->m_bulletStreamDataServerToClient[0], SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE); - } - - if (hasStatus) - { - processBodyJointInfo(bodyUniqueId, m_data->m_tmpInfoStatus); - } + processRequestBodyInfo(m_data->m_tmpInfoRequestCommand, m_data->m_tmpInfoStatus); } break; } @@ -1405,6 +1391,22 @@ bool PhysicsDirect::processCustomCommand(const struct SharedMemoryCommand& orgCo return m_data->m_hasStatus; } +bool PhysicsDirect::processRequestBodyInfo(const struct SharedMemoryCommand& command, SharedMemoryStatus& status) { + bool hasStatus = m_data->m_commandProcessor->processCommand(command, status, &m_data->m_bulletStreamDataServerToClient[0], SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE); + b3Clock clock; + double startTime = clock.getTimeInSeconds(); + double timeOutInSeconds = m_data->m_timeOutInSeconds; + while ((!hasStatus) && (clock.getTimeInSeconds() - startTime < timeOutInSeconds)) + { + hasStatus = m_data->m_commandProcessor->receiveStatus(status, &m_data->m_bulletStreamDataServerToClient[0], SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE); + } + if (hasStatus) { + processBodyJointInfo(command.m_sdfRequestInfoArgs.m_bodyUniqueId, status); + } + m_data->m_hasStatus = hasStatus; + return m_data->m_hasStatus; +} + bool PhysicsDirect::submitClientCommand(const struct SharedMemoryCommand& command) { if (command.m_type == CMD_CUSTOM_COMMAND) @@ -1434,10 +1436,14 @@ bool PhysicsDirect::submitClientCommand(const struct SharedMemoryCommand& comman return processOverlappingObjects(command); } - if (command.m_type == CMD_REQUEST_MESH_DATA) + if (command.m_type == CMD_REQUEST_MESH_DATA) { return processMeshData(command); } + if (command.m_type == CMD_REQUEST_BODY_INFO) + { + return processRequestBodyInfo(command, m_data->m_serverStatus); + } bool hasStatus = m_data->m_commandProcessor->processCommand(command, m_data->m_serverStatus, &m_data->m_bulletStreamDataServerToClient[0], SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE); m_data->m_hasStatus = hasStatus; diff --git a/examples/SharedMemory/PhysicsDirect.h b/examples/SharedMemory/PhysicsDirect.h index 2e09a95d2..0d33f8119 100644 --- a/examples/SharedMemory/PhysicsDirect.h +++ b/examples/SharedMemory/PhysicsDirect.h @@ -28,6 +28,8 @@ protected: void processAddUserData(const struct SharedMemoryStatus& serverCmd); + bool processRequestBodyInfo(const struct SharedMemoryCommand& command, SharedMemoryStatus& status); + bool processCustomCommand(const struct SharedMemoryCommand& orgCommand); void postProcessStatus(const struct SharedMemoryStatus& serverCmd); -- cgit v1.2.1