12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034 |
- // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
- // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
- // SPDX-License-Identifier: MIT
- #include <Jolt/Jolt.h>
- #include <Jolt/Physics/Body/BodyInterface.h>
- #include <Jolt/Physics/Collision/BroadPhase/BroadPhase.h>
- #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
- #include <Jolt/Physics/Body/Body.h>
- #include <Jolt/Physics/Body/BodyManager.h>
- #include <Jolt/Physics/Body/BodyCreationSettings.h>
- #include <Jolt/Physics/Body/BodyLock.h>
- #include <Jolt/Physics/Body/BodyLockMulti.h>
- #include <Jolt/Physics/Collision/PhysicsMaterial.h>
- #include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
- JPH_NAMESPACE_BEGIN
- void BodyInterface::ActivateBodyInternal(Body &ioBody) const
- {
- // Activate body or reset its sleep timer.
- // Note that BodyManager::ActivateBodies also resets the sleep timer internally, but we avoid a mutex lock if the body is already active by calling ResetSleepTimer directly.
- if (!ioBody.IsActive())
- mBodyManager->ActivateBodies(&ioBody.GetID(), 1);
- else
- ioBody.ResetSleepTimer();
- }
- Body *BodyInterface::CreateBody(const BodyCreationSettings &inSettings)
- {
- Body *body = mBodyManager->AllocateBody(inSettings);
- if (!mBodyManager->AddBody(body))
- {
- mBodyManager->FreeBody(body);
- return nullptr;
- }
- return body;
- }
- Body *BodyInterface::CreateSoftBody(const SoftBodyCreationSettings &inSettings)
- {
- Body *body = mBodyManager->AllocateSoftBody(inSettings);
- if (!mBodyManager->AddBody(body))
- {
- mBodyManager->FreeBody(body);
- return nullptr;
- }
- return body;
- }
- Body *BodyInterface::CreateBodyWithID(const BodyID &inBodyID, const BodyCreationSettings &inSettings)
- {
- Body *body = mBodyManager->AllocateBody(inSettings);
- if (!mBodyManager->AddBodyWithCustomID(body, inBodyID))
- {
- mBodyManager->FreeBody(body);
- return nullptr;
- }
- return body;
- }
- Body *BodyInterface::CreateSoftBodyWithID(const BodyID &inBodyID, const SoftBodyCreationSettings &inSettings)
- {
- Body *body = mBodyManager->AllocateSoftBody(inSettings);
- if (!mBodyManager->AddBodyWithCustomID(body, inBodyID))
- {
- mBodyManager->FreeBody(body);
- return nullptr;
- }
- return body;
- }
- Body *BodyInterface::CreateBodyWithoutID(const BodyCreationSettings &inSettings) const
- {
- return mBodyManager->AllocateBody(inSettings);
- }
- Body *BodyInterface::CreateSoftBodyWithoutID(const SoftBodyCreationSettings &inSettings) const
- {
- return mBodyManager->AllocateSoftBody(inSettings);
- }
- void BodyInterface::DestroyBodyWithoutID(Body *inBody) const
- {
- mBodyManager->FreeBody(inBody);
- }
- bool BodyInterface::AssignBodyID(Body *ioBody)
- {
- return mBodyManager->AddBody(ioBody);
- }
- bool BodyInterface::AssignBodyID(Body *ioBody, const BodyID &inBodyID)
- {
- return mBodyManager->AddBodyWithCustomID(ioBody, inBodyID);
- }
- Body *BodyInterface::UnassignBodyID(const BodyID &inBodyID)
- {
- Body *body = nullptr;
- mBodyManager->RemoveBodies(&inBodyID, 1, &body);
- return body;
- }
- void BodyInterface::UnassignBodyIDs(const BodyID *inBodyIDs, int inNumber, Body **outBodies)
- {
- mBodyManager->RemoveBodies(inBodyIDs, inNumber, outBodies);
- }
- void BodyInterface::DestroyBody(const BodyID &inBodyID)
- {
- mBodyManager->DestroyBodies(&inBodyID, 1);
- }
- void BodyInterface::DestroyBodies(const BodyID *inBodyIDs, int inNumber)
- {
- mBodyManager->DestroyBodies(inBodyIDs, inNumber);
- }
- void BodyInterface::AddBody(const BodyID &inBodyID, EActivation inActivationMode)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- const Body &body = lock.GetBody();
- // Add to broadphase
- BodyID id = inBodyID;
- BroadPhase::AddState add_state = mBroadPhase->AddBodiesPrepare(&id, 1);
- mBroadPhase->AddBodiesFinalize(&id, 1, add_state);
- // Optionally activate body
- if (inActivationMode == EActivation::Activate && !body.IsStatic())
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- void BodyInterface::RemoveBody(const BodyID &inBodyID)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- const Body &body = lock.GetBody();
- // Deactivate body
- if (body.IsActive())
- mBodyManager->DeactivateBodies(&inBodyID, 1);
- // Remove from broadphase
- BodyID id = inBodyID;
- mBroadPhase->RemoveBodies(&id, 1);
- }
- }
- bool BodyInterface::IsAdded(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- return lock.SucceededAndIsInBroadPhase();
- }
- BodyID BodyInterface::CreateAndAddBody(const BodyCreationSettings &inSettings, EActivation inActivationMode)
- {
- const Body *b = CreateBody(inSettings);
- if (b == nullptr)
- return BodyID(); // Out of bodies
- AddBody(b->GetID(), inActivationMode);
- return b->GetID();
- }
- BodyID BodyInterface::CreateAndAddSoftBody(const SoftBodyCreationSettings &inSettings, EActivation inActivationMode)
- {
- const Body *b = CreateSoftBody(inSettings);
- if (b == nullptr)
- return BodyID(); // Out of bodies
- AddBody(b->GetID(), inActivationMode);
- return b->GetID();
- }
- BodyInterface::AddState BodyInterface::AddBodiesPrepare(BodyID *ioBodies, int inNumber)
- {
- return mBroadPhase->AddBodiesPrepare(ioBodies, inNumber);
- }
- void BodyInterface::AddBodiesFinalize(BodyID *ioBodies, int inNumber, AddState inAddState, EActivation inActivationMode)
- {
- BodyLockMultiWrite lock(*mBodyLockInterface, ioBodies, inNumber);
- // Add to broadphase
- mBroadPhase->AddBodiesFinalize(ioBodies, inNumber, inAddState);
- // Optionally activate bodies
- if (inActivationMode == EActivation::Activate)
- mBodyManager->ActivateBodies(ioBodies, inNumber);
- }
- void BodyInterface::AddBodiesAbort(BodyID *ioBodies, int inNumber, AddState inAddState)
- {
- mBroadPhase->AddBodiesAbort(ioBodies, inNumber, inAddState);
- }
- void BodyInterface::RemoveBodies(BodyID *ioBodies, int inNumber)
- {
- BodyLockMultiWrite lock(*mBodyLockInterface, ioBodies, inNumber);
- // Deactivate bodies
- mBodyManager->DeactivateBodies(ioBodies, inNumber);
- // Remove from broadphase
- mBroadPhase->RemoveBodies(ioBodies, inNumber);
- }
- void BodyInterface::ActivateBody(const BodyID &inBodyID)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- ActivateBodyInternal(body);
- }
- }
- void BodyInterface::ActivateBodies(const BodyID *inBodyIDs, int inNumber)
- {
- BodyLockMultiWrite lock(*mBodyLockInterface, inBodyIDs, inNumber);
- mBodyManager->ActivateBodies(inBodyIDs, inNumber);
- }
- void BodyInterface::ActivateBodiesInAABox(const AABox &inBox, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter)
- {
- AllHitCollisionCollector<CollideShapeBodyCollector> collector;
- mBroadPhase->CollideAABox(inBox, collector, inBroadPhaseLayerFilter, inObjectLayerFilter);
- ActivateBodies(collector.mHits.data(), (int)collector.mHits.size());
- }
- void BodyInterface::DeactivateBody(const BodyID &inBodyID)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- const Body &body = lock.GetBody();
- if (body.IsActive())
- mBodyManager->DeactivateBodies(&inBodyID, 1);
- }
- }
- void BodyInterface::DeactivateBodies(const BodyID *inBodyIDs, int inNumber)
- {
- BodyLockMultiWrite lock(*mBodyLockInterface, inBodyIDs, inNumber);
- mBodyManager->DeactivateBodies(inBodyIDs, inNumber);
- }
- bool BodyInterface::IsActive(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- return lock.Succeeded() && lock.GetBody().IsActive();
- }
- void BodyInterface::ResetSleepTimer(const BodyID &inBodyID)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- lock.GetBody().ResetSleepTimer();
- }
- TwoBodyConstraint *BodyInterface::CreateConstraint(const TwoBodyConstraintSettings *inSettings, const BodyID &inBodyID1, const BodyID &inBodyID2)
- {
- BodyID constraint_bodies[] = { inBodyID1, inBodyID2 };
- BodyLockMultiWrite lock(*mBodyLockInterface, constraint_bodies, 2);
- Body *body1 = lock.GetBody(0);
- Body *body2 = lock.GetBody(1);
- JPH_ASSERT(body1 != body2);
- JPH_ASSERT(body1 != nullptr || body2 != nullptr);
- return inSettings->Create(body1 != nullptr? *body1 : Body::sFixedToWorld, body2 != nullptr? *body2 : Body::sFixedToWorld);
- }
- void BodyInterface::ActivateConstraint(const TwoBodyConstraint *inConstraint)
- {
- BodyID bodies[] = { inConstraint->GetBody1()->GetID(), inConstraint->GetBody2()->GetID() };
- ActivateBodies(bodies, 2);
- }
- RefConst<Shape> BodyInterface::GetShape(const BodyID &inBodyID) const
- {
- RefConst<Shape> shape;
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- shape = lock.GetBody().GetShape();
- return shape;
- }
- void BodyInterface::SetShape(const BodyID &inBodyID, const Shape *inShape, bool inUpdateMassProperties, EActivation inActivationMode) const
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- // Check if shape actually changed
- if (body.GetShape() != inShape)
- {
- // Update the shape
- body.SetShapeInternal(inShape, inUpdateMassProperties);
- // Flag collision cache invalid for this body
- mBodyManager->InvalidateContactCacheForBody(body);
- // Notify broadphase of change
- if (body.IsInBroadPhase())
- {
- BodyID id = body.GetID();
- mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
- }
- // Optionally activate body
- if (inActivationMode == EActivation::Activate && !body.IsStatic())
- ActivateBodyInternal(body);
- }
- }
- }
- void BodyInterface::NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inPreviousCenterOfMass, bool inUpdateMassProperties, EActivation inActivationMode) const
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- // Update center of mass, mass and inertia
- body.UpdateCenterOfMassInternal(inPreviousCenterOfMass, inUpdateMassProperties);
- // Recalculate bounding box
- body.CalculateWorldSpaceBoundsInternal();
- // Flag collision cache invalid for this body
- mBodyManager->InvalidateContactCacheForBody(body);
- // Notify broadphase of change
- if (body.IsInBroadPhase())
- {
- BodyID id = body.GetID();
- mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
- }
- // Optionally activate body
- if (inActivationMode == EActivation::Activate && !body.IsStatic())
- ActivateBodyInternal(body);
- }
- }
- void BodyInterface::SetObjectLayer(const BodyID &inBodyID, ObjectLayer inLayer)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- // Check if layer actually changed, updating the broadphase is rather expensive
- if (body.GetObjectLayer() != inLayer)
- {
- // Update the layer on the body
- mBodyManager->SetBodyObjectLayerInternal(body, inLayer);
- // Notify broadphase of change
- if (body.IsInBroadPhase())
- {
- BodyID id = body.GetID();
- mBroadPhase->NotifyBodiesLayerChanged(&id, 1);
- }
- }
- }
- }
- ObjectLayer BodyInterface::GetObjectLayer(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetObjectLayer();
- else
- return cObjectLayerInvalid;
- }
- void BodyInterface::SetPositionAndRotation(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- // Update the position
- body.SetPositionAndRotationInternal(inPosition, inRotation);
- // Notify broadphase of change
- if (body.IsInBroadPhase())
- {
- BodyID id = body.GetID();
- mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
- }
- // Optionally activate body
- if (inActivationMode == EActivation::Activate && !body.IsStatic())
- ActivateBodyInternal(body);
- }
- }
- void BodyInterface::SetPositionAndRotationWhenChanged(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- // Check if there is enough change
- if (!body.GetPosition().IsClose(inPosition)
- || !body.GetRotation().IsClose(inRotation))
- {
- // Update the position
- body.SetPositionAndRotationInternal(inPosition, inRotation);
- // Notify broadphase of change
- if (body.IsInBroadPhase())
- {
- BodyID id = body.GetID();
- mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
- }
- // Optionally activate body
- if (inActivationMode == EActivation::Activate && !body.IsStatic())
- ActivateBodyInternal(body);
- }
- }
- }
- void BodyInterface::GetPositionAndRotation(const BodyID &inBodyID, RVec3 &outPosition, Quat &outRotation) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- const Body &body = lock.GetBody();
- outPosition = body.GetPosition();
- outRotation = body.GetRotation();
- }
- else
- {
- outPosition = RVec3::sZero();
- outRotation = Quat::sIdentity();
- }
- }
- void BodyInterface::SetPosition(const BodyID &inBodyID, RVec3Arg inPosition, EActivation inActivationMode)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- // Update the position
- body.SetPositionAndRotationInternal(inPosition, body.GetRotation());
- // Notify broadphase of change
- if (body.IsInBroadPhase())
- {
- BodyID id = body.GetID();
- mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
- }
- // Optionally activate body
- if (inActivationMode == EActivation::Activate && !body.IsStatic())
- ActivateBodyInternal(body);
- }
- }
- RVec3 BodyInterface::GetPosition(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetPosition();
- else
- return RVec3::sZero();
- }
- RVec3 BodyInterface::GetCenterOfMassPosition(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetCenterOfMassPosition();
- else
- return RVec3::sZero();
- }
- void BodyInterface::SetRotation(const BodyID &inBodyID, QuatArg inRotation, EActivation inActivationMode)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- // Update the position
- body.SetPositionAndRotationInternal(body.GetPosition(), inRotation);
- // Notify broadphase of change
- if (body.IsInBroadPhase())
- {
- BodyID id = body.GetID();
- mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
- }
- // Optionally activate body
- if (inActivationMode == EActivation::Activate && !body.IsStatic())
- ActivateBodyInternal(body);
- }
- }
- Quat BodyInterface::GetRotation(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetRotation();
- else
- return Quat::sIdentity();
- }
- RMat44 BodyInterface::GetWorldTransform(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetWorldTransform();
- else
- return RMat44::sIdentity();
- }
- RMat44 BodyInterface::GetCenterOfMassTransform(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetCenterOfMassTransform();
- else
- return RMat44::sIdentity();
- }
- void BodyInterface::MoveKinematic(const BodyID &inBodyID, RVec3Arg inTargetPosition, QuatArg inTargetRotation, float inDeltaTime)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- body.MoveKinematic(inTargetPosition, inTargetRotation, inDeltaTime);
- if (!body.IsActive() && (!body.GetLinearVelocity().IsNearZero() || !body.GetAngularVelocity().IsNearZero()))
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- void BodyInterface::SetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (!body.IsStatic())
- {
- body.SetLinearVelocityClamped(inLinearVelocity);
- body.SetAngularVelocityClamped(inAngularVelocity);
- if (!body.IsActive() && (!inLinearVelocity.IsNearZero() || !inAngularVelocity.IsNearZero()))
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- }
- void BodyInterface::GetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3 &outLinearVelocity, Vec3 &outAngularVelocity) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- const Body &body = lock.GetBody();
- if (!body.IsStatic())
- {
- outLinearVelocity = body.GetLinearVelocity();
- outAngularVelocity = body.GetAngularVelocity();
- return;
- }
- }
- outLinearVelocity = outAngularVelocity = Vec3::sZero();
- }
- void BodyInterface::SetLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (!body.IsStatic())
- {
- body.SetLinearVelocityClamped(inLinearVelocity);
- if (!body.IsActive() && !inLinearVelocity.IsNearZero())
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- }
- Vec3 BodyInterface::GetLinearVelocity(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- const Body &body = lock.GetBody();
- if (!body.IsStatic())
- return body.GetLinearVelocity();
- }
- return Vec3::sZero();
- }
- void BodyInterface::AddLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (!body.IsStatic())
- {
- body.SetLinearVelocityClamped(body.GetLinearVelocity() + inLinearVelocity);
- if (!body.IsActive() && !body.GetLinearVelocity().IsNearZero())
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- }
- void BodyInterface::AddLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (!body.IsStatic())
- {
- body.SetLinearVelocityClamped(body.GetLinearVelocity() + inLinearVelocity);
- body.SetAngularVelocityClamped(body.GetAngularVelocity() + inAngularVelocity);
- if (!body.IsActive() && (!body.GetLinearVelocity().IsNearZero() || !body.GetAngularVelocity().IsNearZero()))
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- }
- void BodyInterface::SetAngularVelocity(const BodyID &inBodyID, Vec3Arg inAngularVelocity)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (!body.IsStatic())
- {
- body.SetAngularVelocityClamped(inAngularVelocity);
- if (!body.IsActive() && !inAngularVelocity.IsNearZero())
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- }
- Vec3 BodyInterface::GetAngularVelocity(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- const Body &body = lock.GetBody();
- if (!body.IsStatic())
- return body.GetAngularVelocity();
- }
- return Vec3::sZero();
- }
- Vec3 BodyInterface::GetPointVelocity(const BodyID &inBodyID, RVec3Arg inPoint) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- const Body &body = lock.GetBody();
- if (!body.IsStatic())
- return body.GetPointVelocity(inPoint);
- }
- return Vec3::sZero();
- }
- void BodyInterface::AddForce(const BodyID &inBodyID, Vec3Arg inForce, EActivation inActivationMode)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (body.IsDynamic() && (inActivationMode == EActivation::Activate || body.IsActive()))
- {
- body.AddForce(inForce);
- if (inActivationMode == EActivation::Activate)
- ActivateBodyInternal(body);
- }
- }
- }
- void BodyInterface::AddForce(const BodyID &inBodyID, Vec3Arg inForce, RVec3Arg inPoint, EActivation inActivationMode)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (body.IsDynamic() && (inActivationMode == EActivation::Activate || body.IsActive()))
- {
- body.AddForce(inForce, inPoint);
- if (inActivationMode == EActivation::Activate)
- ActivateBodyInternal(body);
- }
- }
- }
- void BodyInterface::AddTorque(const BodyID &inBodyID, Vec3Arg inTorque, EActivation inActivationMode)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (body.IsDynamic() && (inActivationMode == EActivation::Activate || body.IsActive()))
- {
- body.AddTorque(inTorque);
- if (inActivationMode == EActivation::Activate)
- ActivateBodyInternal(body);
- }
- }
- }
- void BodyInterface::AddForceAndTorque(const BodyID &inBodyID, Vec3Arg inForce, Vec3Arg inTorque, EActivation inActivationMode)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (body.IsDynamic() && (inActivationMode == EActivation::Activate || body.IsActive()))
- {
- body.AddForce(inForce);
- body.AddTorque(inTorque);
- if (inActivationMode == EActivation::Activate)
- ActivateBodyInternal(body);
- }
- }
- }
- void BodyInterface::AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (body.IsDynamic())
- {
- body.AddImpulse(inImpulse);
- if (!body.IsActive())
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- }
- void BodyInterface::AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse, RVec3Arg inPoint)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (body.IsDynamic())
- {
- body.AddImpulse(inImpulse, inPoint);
- if (!body.IsActive())
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- }
- void BodyInterface::AddAngularImpulse(const BodyID &inBodyID, Vec3Arg inAngularImpulse)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (body.IsDynamic())
- {
- body.AddAngularImpulse(inAngularImpulse);
- if (!body.IsActive())
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- }
- bool BodyInterface::ApplyBuoyancyImpulse(const BodyID &inBodyID, RVec3Arg inSurfacePosition, Vec3Arg inSurfaceNormal, float inBuoyancy, float inLinearDrag, float inAngularDrag, Vec3Arg inFluidVelocity, Vec3Arg inGravity, float inDeltaTime)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (body.IsDynamic()
- && body.ApplyBuoyancyImpulse(inSurfacePosition, inSurfaceNormal, inBuoyancy, inLinearDrag, inAngularDrag, inFluidVelocity, inGravity, inDeltaTime))
- {
- ActivateBodyInternal(body);
- return true;
- }
- }
- return false;
- }
- void BodyInterface::SetPositionRotationAndVelocity(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- // Update the position
- body.SetPositionAndRotationInternal(inPosition, inRotation);
- // Notify broadphase of change
- if (body.IsInBroadPhase())
- {
- BodyID id = body.GetID();
- mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
- }
- if (!body.IsStatic())
- {
- body.SetLinearVelocityClamped(inLinearVelocity);
- body.SetAngularVelocityClamped(inAngularVelocity);
- // Optionally activate body
- if (!body.IsActive() && (!inLinearVelocity.IsNearZero() || !inAngularVelocity.IsNearZero()))
- mBodyManager->ActivateBodies(&inBodyID, 1);
- }
- }
- }
- void BodyInterface::SetMotionType(const BodyID &inBodyID, EMotionType inMotionType, EActivation inActivationMode)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- // Deactivate if we're making the body static
- if (body.IsActive() && inMotionType == EMotionType::Static)
- mBodyManager->DeactivateBodies(&inBodyID, 1);
- body.SetMotionType(inMotionType);
- // Activate body if requested
- if (inMotionType != EMotionType::Static && inActivationMode == EActivation::Activate)
- ActivateBodyInternal(body);
- }
- }
- EBodyType BodyInterface::GetBodyType(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetBodyType();
- else
- return EBodyType::RigidBody;
- }
- EMotionType BodyInterface::GetMotionType(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetMotionType();
- else
- return EMotionType::Static;
- }
- void BodyInterface::SetMotionQuality(const BodyID &inBodyID, EMotionQuality inMotionQuality)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- mBodyManager->SetMotionQuality(lock.GetBody(), inMotionQuality);
- }
- EMotionQuality BodyInterface::GetMotionQuality(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded() && !lock.GetBody().IsStatic())
- return lock.GetBody().GetMotionProperties()->GetMotionQuality();
- else
- return EMotionQuality::Discrete;
- }
- Mat44 BodyInterface::GetInverseInertia(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetInverseInertia();
- else
- return Mat44::sIdentity();
- }
- void BodyInterface::SetRestitution(const BodyID &inBodyID, float inRestitution)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- lock.GetBody().SetRestitution(inRestitution);
- }
- float BodyInterface::GetRestitution(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetRestitution();
- else
- return 0.0f;
- }
- void BodyInterface::SetFriction(const BodyID &inBodyID, float inFriction)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- lock.GetBody().SetFriction(inFriction);
- }
- float BodyInterface::GetFriction(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetFriction();
- else
- return 0.0f;
- }
- void BodyInterface::SetGravityFactor(const BodyID &inBodyID, float inGravityFactor)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded() && lock.GetBody().GetMotionPropertiesUnchecked() != nullptr)
- lock.GetBody().GetMotionPropertiesUnchecked()->SetGravityFactor(inGravityFactor);
- }
- float BodyInterface::GetGravityFactor(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded() && lock.GetBody().GetMotionPropertiesUnchecked() != nullptr)
- return lock.GetBody().GetMotionPropertiesUnchecked()->GetGravityFactor();
- else
- return 1.0f;
- }
- void BodyInterface::SetUseManifoldReduction(const BodyID &inBodyID, bool inUseReduction)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- {
- Body &body = lock.GetBody();
- if (body.GetUseManifoldReduction() != inUseReduction)
- {
- body.SetUseManifoldReduction(inUseReduction);
- // Flag collision cache invalid for this body
- mBodyManager->InvalidateContactCacheForBody(body);
- }
- }
- }
- bool BodyInterface::GetUseManifoldReduction(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetUseManifoldReduction();
- else
- return true;
- }
- TransformedShape BodyInterface::GetTransformedShape(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetTransformedShape();
- else
- return TransformedShape();
- }
- uint64 BodyInterface::GetUserData(const BodyID &inBodyID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetUserData();
- else
- return 0;
- }
- void BodyInterface::SetUserData(const BodyID &inBodyID, uint64 inUserData) const
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- lock.GetBody().SetUserData(inUserData);
- }
- const PhysicsMaterial *BodyInterface::GetMaterial(const BodyID &inBodyID, const SubShapeID &inSubShapeID) const
- {
- BodyLockRead lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- return lock.GetBody().GetShape()->GetMaterial(inSubShapeID);
- else
- return PhysicsMaterial::sDefault;
- }
- void BodyInterface::InvalidateContactCache(const BodyID &inBodyID)
- {
- BodyLockWrite lock(*mBodyLockInterface, inBodyID);
- if (lock.Succeeded())
- mBodyManager->InvalidateContactCacheForBody(lock.GetBody());
- }
- JPH_NAMESPACE_END
|