|
@@ -29,12 +29,26 @@ void SensorTest::Initialize()
|
|
|
// Floor
|
|
|
CreateFloor();
|
|
|
|
|
|
- // Sensor
|
|
|
- BodyCreationSettings sensor_settings(new SphereShape(10.0f), Vec3(0, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
|
|
|
- sensor_settings.mIsSensor = true;
|
|
|
- Body &sensor = *mBodyInterface->CreateBody(sensor_settings);
|
|
|
- mSensorID = sensor.GetID();
|
|
|
- mBodyInterface->AddBody(mSensorID, EActivation::DontActivate);
|
|
|
+ {
|
|
|
+ // A static sensor that attrects dynamic bodies that enter its area
|
|
|
+ BodyCreationSettings sensor_settings(new SphereShape(10.0f), Vec3(0, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::SENSOR);
|
|
|
+ sensor_settings.mIsSensor = true;
|
|
|
+ mSensorID[StaticAttractor] = mBodyInterface->CreateAndAddBody(sensor_settings, EActivation::DontActivate);
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ // A static sensor that only detects active bodies
|
|
|
+ BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(5.0f)), Vec3(-10, 5, 0), Quat::sIdentity(), EMotionType::Static, Layers::SENSOR);
|
|
|
+ sensor_settings.mIsSensor = true;
|
|
|
+ mSensorID[StaticSensor] = mBodyInterface->CreateAndAddBody(sensor_settings, EActivation::DontActivate);
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ // A kinematic sensor that also detects sleeping bodies
|
|
|
+ BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(5.0f)), Vec3(10, 5, 0), Quat::sIdentity(), EMotionType::Kinematic, Layers::SENSOR);
|
|
|
+ sensor_settings.mIsSensor = true;
|
|
|
+ mSensorID[KinematicSensor] = mBodyInterface->CreateAndAddBody(sensor_settings, EActivation::Activate);
|
|
|
+ }
|
|
|
|
|
|
// Dynamic bodies
|
|
|
for (int i = 0; i < 10; ++i)
|
|
@@ -64,7 +78,7 @@ void SensorTest::Initialize()
|
|
|
mRagdoll->AddToPhysicsSystem(EActivation::Activate);
|
|
|
|
|
|
// Create kinematic body
|
|
|
- BodyCreationSettings kinematic_settings(new BoxShape(Vec3(0.25f, 0.5f, 1.0f)), Vec3(-15, 10, 0), Quat::sIdentity(), EMotionType::Kinematic, Layers::MOVING);
|
|
|
+ BodyCreationSettings kinematic_settings(new BoxShape(Vec3(0.25f, 0.5f, 1.0f)), Vec3(-20, 10, 0), Quat::sIdentity(), EMotionType::Kinematic, Layers::MOVING);
|
|
|
Body &kinematic = *mBodyInterface->CreateBody(kinematic_settings);
|
|
|
mKinematicBodyID = kinematic.GetID();
|
|
|
mBodyInterface->AddBody(kinematic.GetID(), EActivation::Activate);
|
|
@@ -76,12 +90,18 @@ void SensorTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
|
|
|
mTime += inParams.mDeltaTime;
|
|
|
|
|
|
// Move kinematic body
|
|
|
- Vec3 kinematic_pos = Vec3(-15.0f * cos(mTime), 10, 0);
|
|
|
+ Vec3 kinematic_pos = Vec3(-20.0f * cos(mTime), 10, 0);
|
|
|
mBodyInterface->MoveKinematic(mKinematicBodyID, kinematic_pos, Quat::sIdentity(), inParams.mDeltaTime);
|
|
|
|
|
|
- // Draw if the kinematic body is in the sensor
|
|
|
- if (mKinematicBodyInSensor)
|
|
|
- mDebugRenderer->DrawWireBox(mBodyInterface->GetTransformedShape(mKinematicBodyID).GetWorldSpaceBounds(), Color::sRed);
|
|
|
+ // Draw if body is in sensor
|
|
|
+ Color sensor_color[] = { Color::sRed, Color::sGreen, Color::sBlue };
|
|
|
+ for (int sensor = 0; sensor < NumSensors; ++sensor)
|
|
|
+ for (const BodyAndCount &body_and_count : mBodiesInSensor[sensor])
|
|
|
+ {
|
|
|
+ AABox bounds = mBodyInterface->GetTransformedShape(body_and_count.mBodyID).GetWorldSpaceBounds();
|
|
|
+ bounds.ExpandBy(Vec3::sReplicate(0.01f * sensor));
|
|
|
+ mDebugRenderer->DrawWireBox(bounds, sensor_color[sensor]);
|
|
|
+ }
|
|
|
|
|
|
// Apply forces to dynamic bodies in sensor
|
|
|
lock_guard lock(mMutex);
|
|
@@ -90,12 +110,14 @@ void SensorTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
|
|
|
float centrifugal_force = 10.0f;
|
|
|
Vec3 gravity = mPhysicsSystem->GetGravity();
|
|
|
|
|
|
- for (const BodyAndCount &body_and_count : mBodiesInSensor)
|
|
|
+ for (const BodyAndCount &body_and_count : mBodiesInSensor[StaticAttractor])
|
|
|
{
|
|
|
BodyLockWrite body_lock(mPhysicsSystem->GetBodyLockInterface(), body_and_count.mBodyID);
|
|
|
if (body_lock.Succeeded())
|
|
|
{
|
|
|
Body &body = body_lock.GetBody();
|
|
|
+ if (body.IsKinematic())
|
|
|
+ continue;
|
|
|
|
|
|
// Calculate centrifugal acceleration
|
|
|
Vec3 acceleration = center - body.GetPosition();
|
|
@@ -119,61 +141,57 @@ void SensorTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
|
|
|
|
|
|
void SensorTest::OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
|
|
|
{
|
|
|
- // Check which body is the sensor
|
|
|
- BodyID body_id;
|
|
|
- if (inBody1.GetID() == mSensorID)
|
|
|
- body_id = inBody2.GetID();
|
|
|
- else if (inBody2.GetID() == mSensorID)
|
|
|
- body_id = inBody1.GetID();
|
|
|
- else
|
|
|
- return;
|
|
|
+ for (int sensor = 0; sensor < NumSensors; ++sensor)
|
|
|
+ {
|
|
|
+ BodyID sensor_id = mSensorID[sensor];
|
|
|
|
|
|
- lock_guard lock(mMutex);
|
|
|
+ // Check which body is the sensor
|
|
|
+ BodyID body_id;
|
|
|
+ if (inBody1.GetID() == sensor_id)
|
|
|
+ body_id = inBody2.GetID();
|
|
|
+ else if (inBody2.GetID() == sensor_id)
|
|
|
+ body_id = inBody1.GetID();
|
|
|
+ else
|
|
|
+ continue;
|
|
|
+
|
|
|
+ lock_guard lock(mMutex);
|
|
|
|
|
|
- if (body_id == mKinematicBodyID)
|
|
|
- {
|
|
|
- JPH_ASSERT(!mKinematicBodyInSensor);
|
|
|
- mKinematicBodyInSensor = true;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
// Add to list and make sure that the list remains sorted for determinism (contacts can be added from multiple threads)
|
|
|
BodyAndCount body_and_count { body_id, 1 };
|
|
|
- BodiesInSensor::iterator b = lower_bound(mBodiesInSensor.begin(), mBodiesInSensor.end(), body_and_count);
|
|
|
- if (b != mBodiesInSensor.end() && b->mBodyID == body_id)
|
|
|
+ BodiesInSensor &bodies_in_sensor = mBodiesInSensor[sensor];
|
|
|
+ BodiesInSensor::iterator b = lower_bound(bodies_in_sensor.begin(), bodies_in_sensor.end(), body_and_count);
|
|
|
+ if (b != bodies_in_sensor.end() && b->mBodyID == body_id)
|
|
|
{
|
|
|
// This is the right body, increment reference
|
|
|
b->mCount++;
|
|
|
return;
|
|
|
}
|
|
|
- mBodiesInSensor.insert(b, body_and_count);
|
|
|
+ bodies_in_sensor.insert(b, body_and_count);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void SensorTest::OnContactRemoved(const SubShapeIDPair &inSubShapePair)
|
|
|
{
|
|
|
- // Check which body is the sensor
|
|
|
- BodyID body_id;
|
|
|
- if (inSubShapePair.GetBody1ID() == mSensorID)
|
|
|
- body_id = inSubShapePair.GetBody2ID();
|
|
|
- else if (inSubShapePair.GetBody2ID() == mSensorID)
|
|
|
- body_id = inSubShapePair.GetBody1ID();
|
|
|
- else
|
|
|
- return;
|
|
|
+ for (int sensor = 0; sensor < NumSensors; ++sensor)
|
|
|
+ {
|
|
|
+ BodyID sensor_id = mSensorID[sensor];
|
|
|
|
|
|
- lock_guard lock(mMutex);
|
|
|
+ // Check which body is the sensor
|
|
|
+ BodyID body_id;
|
|
|
+ if (inSubShapePair.GetBody1ID() == sensor_id)
|
|
|
+ body_id = inSubShapePair.GetBody2ID();
|
|
|
+ else if (inSubShapePair.GetBody2ID() == sensor_id)
|
|
|
+ body_id = inSubShapePair.GetBody1ID();
|
|
|
+ else
|
|
|
+ continue;
|
|
|
+
|
|
|
+ lock_guard lock(mMutex);
|
|
|
|
|
|
- if (body_id == mKinematicBodyID)
|
|
|
- {
|
|
|
- JPH_ASSERT(mKinematicBodyInSensor);
|
|
|
- mKinematicBodyInSensor = false;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
// Remove from list
|
|
|
BodyAndCount body_and_count { body_id, 1 };
|
|
|
- BodiesInSensor::iterator b = lower_bound(mBodiesInSensor.begin(), mBodiesInSensor.end(), body_and_count);
|
|
|
- if (b != mBodiesInSensor.end() && b->mBodyID == body_id)
|
|
|
+ BodiesInSensor &bodies_in_sensor = mBodiesInSensor[sensor];
|
|
|
+ BodiesInSensor::iterator b = lower_bound(bodies_in_sensor.begin(), bodies_in_sensor.end(), body_and_count);
|
|
|
+ if (b != bodies_in_sensor.end() && b->mBodyID == body_id)
|
|
|
{
|
|
|
// This is the right body, increment reference
|
|
|
JPH_ASSERT(b->mCount > 0);
|
|
@@ -181,7 +199,7 @@ void SensorTest::OnContactRemoved(const SubShapeIDPair &inSubShapePair)
|
|
|
|
|
|
// When last reference remove from the list
|
|
|
if (b->mCount == 0)
|
|
|
- mBodiesInSensor.erase(b);
|
|
|
+ bodies_in_sensor.erase(b);
|
|
|
return;
|
|
|
}
|
|
|
JPH_ASSERT(false, "Body pair not found");
|
|
@@ -191,13 +209,13 @@ void SensorTest::OnContactRemoved(const SubShapeIDPair &inSubShapePair)
|
|
|
void SensorTest::SaveState(StateRecorder &inStream) const
|
|
|
{
|
|
|
inStream.Write(mTime);
|
|
|
- inStream.Write(mBodiesInSensor);
|
|
|
- inStream.Write(mKinematicBodyInSensor);
|
|
|
+ for (const BodiesInSensor &b : mBodiesInSensor)
|
|
|
+ inStream.Write(b);
|
|
|
}
|
|
|
|
|
|
void SensorTest::RestoreState(StateRecorder &inStream)
|
|
|
{
|
|
|
inStream.Read(mTime);
|
|
|
- inStream.Read(mBodiesInSensor);
|
|
|
- inStream.Read(mKinematicBodyInSensor);
|
|
|
+ for (BodiesInSensor &b : mBodiesInSensor)
|
|
|
+ inStream.Read(b);
|
|
|
}
|