Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Body/BodyID.h
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Core/HashCombine.h>78JPH_NAMESPACE_BEGIN910/// ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding race conditions.11class BodyID12{13public:14JPH_OVERRIDE_NEW_DELETE1516static constexpr uint32 cInvalidBodyID = 0xffffffff; ///< The value for an invalid body ID17static constexpr uint32 cBroadPhaseBit = 0x80000000; ///< This bit is used by the broadphase18static constexpr uint32 cMaxBodyIndex = 0x7fffff; ///< Maximum value for body index (also the maximum amount of bodies supported - 1)19static constexpr uint8 cMaxSequenceNumber = 0xff; ///< Maximum value for the sequence number20static constexpr uint cSequenceNumberShift = 23; ///< Number of bits to shift to get the sequence number2122/// Construct invalid body ID23BodyID() :24mID(cInvalidBodyID)25{26}2728/// Construct from index and sequence number combined in a single uint32 (use with care!)29explicit BodyID(uint32 inID) :30mID(inID)31{32JPH_ASSERT((inID & cBroadPhaseBit) == 0 || inID == cInvalidBodyID); // Check bit used by broadphase33}3435/// Construct from index and sequence number36explicit BodyID(uint32 inID, uint8 inSequenceNumber) :37mID((uint32(inSequenceNumber) << cSequenceNumberShift) | inID)38{39JPH_ASSERT(inID <= cMaxBodyIndex); // Should not overlap with broadphase bit or sequence number40}4142/// Get index in body array43inline uint32 GetIndex() const44{45return mID & cMaxBodyIndex;46}4748/// Get sequence number of body.49/// The sequence number can be used to check if a body ID with the same body index has been reused by another body.50/// It is mainly used in multi threaded situations where a body is removed and its body index is immediately reused by a body created from another thread.51/// Functions querying the broadphase can (after acquiring a body lock) detect that the body has been removed (we assume that this won't happen more than 128 times in a row).52inline uint8 GetSequenceNumber() const53{54return uint8(mID >> cSequenceNumberShift);55}5657/// Returns the index and sequence number combined in an uint3258inline uint32 GetIndexAndSequenceNumber() const59{60return mID;61}6263/// Check if the ID is valid64inline bool IsInvalid() const65{66return mID == cInvalidBodyID;67}6869/// Equals check70inline bool operator == (const BodyID &inRHS) const71{72return mID == inRHS.mID;73}7475/// Not equals check76inline bool operator != (const BodyID &inRHS) const77{78return mID != inRHS.mID;79}8081/// Smaller than operator, can be used for sorting bodies82inline bool operator < (const BodyID &inRHS) const83{84return mID < inRHS.mID;85}8687/// Greater than operator, can be used for sorting bodies88inline bool operator > (const BodyID &inRHS) const89{90return mID > inRHS.mID;91}9293private:94uint32 mID;95};9697JPH_NAMESPACE_END9899// Create a std::hash/JPH::Hash for BodyID100JPH_MAKE_HASHABLE(JPH::BodyID, t.GetIndexAndSequenceNumber())101102103