mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 07:37:20 +02:00
Merge pull request #4675 from sethalves/detect-ballistic
Detect ballistic motion + fix up simulation-owner code + fix up EntityItem constructor
This commit is contained in:
commit
0daaf20f01
9 changed files with 292 additions and 235 deletions
|
@ -114,12 +114,16 @@ function makeBalls(pos) {
|
||||||
for (var spot = 0; spot < row; spot++) {
|
for (var spot = 0; spot < row; spot++) {
|
||||||
balls.push(Entities.addEntity(
|
balls.push(Entities.addEntity(
|
||||||
{ type: "Model",
|
{ type: "Model",
|
||||||
modelURL: "https://s3.amazonaws.com/hifi-public/models/props/Pool/ball_" + whichBall[ballNumber].toString() + ".fbx",
|
modelURL: "https://s3.amazonaws.com/hifi-public/models/props/Pool/ball_" +
|
||||||
|
whichBall[ballNumber].toString() + ".fbx",
|
||||||
position: ballPosition,
|
position: ballPosition,
|
||||||
dimensions: { x: BALL_SIZE * SCALE, y: BALL_SIZE * SCALE, z: BALL_SIZE * SCALE },
|
dimensions: { x: BALL_SIZE * SCALE, y: BALL_SIZE * SCALE, z: BALL_SIZE * SCALE },
|
||||||
rotation: Quat.fromPitchYawRollDegrees((Math.random() - 0.5) * 20, (Math.random() - 0.5) * 20, (Math.random() - 0.5) * 20),
|
rotation: Quat.fromPitchYawRollDegrees((Math.random() - 0.5) * 20,
|
||||||
|
(Math.random() - 0.5) * 20,
|
||||||
|
(Math.random() - 0.5) * 20),
|
||||||
color: { red: 255, green: 255, blue: 255 },
|
color: { red: 255, green: 255, blue: 255 },
|
||||||
gravity: { x: 0, y: GRAVITY, z: 0 },
|
gravity: { x: 0, y: GRAVITY, z: 0 },
|
||||||
|
velocity: {x: 0, y: -0.2, z: 0 },
|
||||||
ignoreCollisions: false,
|
ignoreCollisions: false,
|
||||||
damping: 0.50,
|
damping: 0.50,
|
||||||
shapeType: "sphere",
|
shapeType: "sphere",
|
||||||
|
@ -140,7 +144,7 @@ function makeBalls(pos) {
|
||||||
color: { red: 255, green: 255, blue: 255 },
|
color: { red: 255, green: 255, blue: 255 },
|
||||||
gravity: { x: 0, y: GRAVITY, z: 0 },
|
gravity: { x: 0, y: GRAVITY, z: 0 },
|
||||||
angularVelocity: { x: 0, y: 0, z: 0 },
|
angularVelocity: { x: 0, y: 0, z: 0 },
|
||||||
velocity: {x: 0, y: 0, z: 0 },
|
velocity: {x: 0, y: -0.2, z: 0 },
|
||||||
ignoreCollisions: false,
|
ignoreCollisions: false,
|
||||||
damping: 0.50,
|
damping: 0.50,
|
||||||
shapeType: "sphere",
|
shapeType: "sphere",
|
||||||
|
|
|
@ -27,75 +27,53 @@
|
||||||
|
|
||||||
bool EntityItem::_sendPhysicsUpdates = true;
|
bool EntityItem::_sendPhysicsUpdates = true;
|
||||||
|
|
||||||
void EntityItem::initFromEntityItemID(const EntityItemID& entityItemID) {
|
EntityItem::EntityItem(const EntityItemID& entityItemID) :
|
||||||
_id = entityItemID.id;
|
_type(EntityTypes::Unknown),
|
||||||
_creatorTokenID = entityItemID.creatorTokenID;
|
_id(entityItemID.id),
|
||||||
|
_creatorTokenID(entityItemID.creatorTokenID),
|
||||||
// init values with defaults before calling setProperties
|
_newlyCreated(false),
|
||||||
|
_lastSimulated(0),
|
||||||
|
_lastUpdated(0),
|
||||||
|
_lastEdited(0),
|
||||||
|
_lastEditedFromRemote(0),
|
||||||
|
_lastEditedFromRemoteInRemoteTime(0),
|
||||||
|
_created(UNKNOWN_CREATED_TIME),
|
||||||
|
_changedOnServer(0),
|
||||||
|
_position(ENTITY_ITEM_ZERO_VEC3),
|
||||||
|
_dimensions(ENTITY_ITEM_DEFAULT_DIMENSIONS),
|
||||||
|
_rotation(ENTITY_ITEM_DEFAULT_ROTATION),
|
||||||
|
_glowLevel(ENTITY_ITEM_DEFAULT_GLOW_LEVEL),
|
||||||
|
_localRenderAlpha(ENTITY_ITEM_DEFAULT_LOCAL_RENDER_ALPHA),
|
||||||
|
_density(ENTITY_ITEM_DEFAULT_DENSITY),
|
||||||
|
_volumeMultiplier(1.0f),
|
||||||
|
_velocity(ENTITY_ITEM_DEFAULT_VELOCITY),
|
||||||
|
_gravity(ENTITY_ITEM_DEFAULT_GRAVITY),
|
||||||
|
_acceleration(ENTITY_ITEM_DEFAULT_ACCELERATION),
|
||||||
|
_damping(ENTITY_ITEM_DEFAULT_DAMPING),
|
||||||
|
_lifetime(ENTITY_ITEM_DEFAULT_LIFETIME),
|
||||||
|
_script(ENTITY_ITEM_DEFAULT_SCRIPT),
|
||||||
|
_registrationPoint(ENTITY_ITEM_DEFAULT_REGISTRATION_POINT),
|
||||||
|
_angularVelocity(ENTITY_ITEM_DEFAULT_ANGULAR_VELOCITY),
|
||||||
|
_angularDamping(ENTITY_ITEM_DEFAULT_ANGULAR_DAMPING),
|
||||||
|
_visible(ENTITY_ITEM_DEFAULT_VISIBLE),
|
||||||
|
_ignoreForCollisions(ENTITY_ITEM_DEFAULT_IGNORE_FOR_COLLISIONS),
|
||||||
|
_collisionsWillMove(ENTITY_ITEM_DEFAULT_COLLISIONS_WILL_MOVE),
|
||||||
|
_locked(ENTITY_ITEM_DEFAULT_LOCKED),
|
||||||
|
_userData(ENTITY_ITEM_DEFAULT_USER_DATA),
|
||||||
|
_simulatorID(ENTITY_ITEM_DEFAULT_SIMULATOR_ID),
|
||||||
|
_simulatorIDChangedTime(0),
|
||||||
|
_marketplaceID(ENTITY_ITEM_DEFAULT_MARKETPLACE_ID),
|
||||||
|
_physicsInfo(NULL),
|
||||||
|
_dirtyFlags(0),
|
||||||
|
_element(NULL)
|
||||||
|
{
|
||||||
quint64 now = usecTimestampNow();
|
quint64 now = usecTimestampNow();
|
||||||
_lastSimulated = now;
|
_lastSimulated = now;
|
||||||
_lastUpdated = now;
|
_lastUpdated = now;
|
||||||
_lastEdited = 0;
|
|
||||||
_lastEditedFromRemote = 0;
|
|
||||||
_lastEditedFromRemoteInRemoteTime = 0;
|
|
||||||
_created = UNKNOWN_CREATED_TIME;
|
|
||||||
_changedOnServer = 0;
|
|
||||||
|
|
||||||
_position = ENTITY_ITEM_ZERO_VEC3;
|
|
||||||
_dimensions = ENTITY_ITEM_DEFAULT_DIMENSIONS;
|
|
||||||
_density = ENTITY_ITEM_DEFAULT_DENSITY;
|
|
||||||
_rotation = ENTITY_ITEM_DEFAULT_ROTATION;
|
|
||||||
_glowLevel = ENTITY_ITEM_DEFAULT_GLOW_LEVEL;
|
|
||||||
_localRenderAlpha = ENTITY_ITEM_DEFAULT_LOCAL_RENDER_ALPHA;
|
|
||||||
_velocity = ENTITY_ITEM_DEFAULT_VELOCITY;
|
|
||||||
_gravity = ENTITY_ITEM_DEFAULT_GRAVITY;
|
|
||||||
_acceleration = ENTITY_ITEM_DEFAULT_ACCELERATION;
|
|
||||||
_damping = ENTITY_ITEM_DEFAULT_DAMPING;
|
|
||||||
_lifetime = ENTITY_ITEM_DEFAULT_LIFETIME;
|
|
||||||
_script = ENTITY_ITEM_DEFAULT_SCRIPT;
|
|
||||||
_registrationPoint = ENTITY_ITEM_DEFAULT_REGISTRATION_POINT;
|
|
||||||
_angularVelocity = ENTITY_ITEM_DEFAULT_ANGULAR_VELOCITY;
|
|
||||||
_angularDamping = ENTITY_ITEM_DEFAULT_ANGULAR_DAMPING;
|
|
||||||
_visible = ENTITY_ITEM_DEFAULT_VISIBLE;
|
|
||||||
_ignoreForCollisions = ENTITY_ITEM_DEFAULT_IGNORE_FOR_COLLISIONS;
|
|
||||||
_collisionsWillMove = ENTITY_ITEM_DEFAULT_COLLISIONS_WILL_MOVE;
|
|
||||||
_locked = ENTITY_ITEM_DEFAULT_LOCKED;
|
|
||||||
_userData = ENTITY_ITEM_DEFAULT_USER_DATA;
|
|
||||||
_simulatorID = ENTITY_ITEM_DEFAULT_SIMULATOR_ID;
|
|
||||||
_marketplaceID = ENTITY_ITEM_DEFAULT_MARKETPLACE_ID;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityItem::EntityItem(const EntityItemID& entityItemID) {
|
EntityItem::EntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) : EntityItem(entityItemID)
|
||||||
_type = EntityTypes::Unknown;
|
{
|
||||||
quint64 now = usecTimestampNow();
|
|
||||||
_lastSimulated = now;
|
|
||||||
_lastUpdated = now;
|
|
||||||
_lastEdited = 0;
|
|
||||||
_lastEditedFromRemote = 0;
|
|
||||||
_lastEditedFromRemoteInRemoteTime = 0;
|
|
||||||
_created = UNKNOWN_CREATED_TIME;
|
|
||||||
_dirtyFlags = 0;
|
|
||||||
_changedOnServer = 0;
|
|
||||||
_element = NULL;
|
|
||||||
_simulatorIDChangedTime = 0;
|
|
||||||
_shouldClaimSimulationOwnership = false;
|
|
||||||
initFromEntityItemID(entityItemID);
|
|
||||||
}
|
|
||||||
|
|
||||||
EntityItem::EntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) {
|
|
||||||
_type = EntityTypes::Unknown;
|
|
||||||
quint64 now = usecTimestampNow();
|
|
||||||
_lastSimulated = now;
|
|
||||||
_lastUpdated = now;
|
|
||||||
_lastEdited = 0;
|
|
||||||
_lastEditedFromRemote = 0;
|
|
||||||
_lastEditedFromRemoteInRemoteTime = 0;
|
|
||||||
_created = UNKNOWN_CREATED_TIME;
|
|
||||||
_dirtyFlags = 0;
|
|
||||||
_changedOnServer = 0;
|
|
||||||
_element = NULL;
|
|
||||||
_simulatorIDChangedTime = 0;
|
|
||||||
initFromEntityItemID(entityItemID);
|
|
||||||
setProperties(properties);
|
setProperties(properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -244,8 +244,6 @@ public:
|
||||||
QUuid getSimulatorID() const { return _simulatorID; }
|
QUuid getSimulatorID() const { return _simulatorID; }
|
||||||
void setSimulatorID(const QUuid& value);
|
void setSimulatorID(const QUuid& value);
|
||||||
quint64 getSimulatorIDChangedTime() const { return _simulatorIDChangedTime; }
|
quint64 getSimulatorIDChangedTime() const { return _simulatorIDChangedTime; }
|
||||||
void setShouldClaimSimulationOwnership(bool value) { _shouldClaimSimulationOwnership = value; }
|
|
||||||
bool getShouldClaimSimulationOwnership() { return _shouldClaimSimulationOwnership; }
|
|
||||||
|
|
||||||
const QString& getMarketplaceID() const { return _marketplaceID; }
|
const QString& getMarketplaceID() const { return _marketplaceID; }
|
||||||
void setMarketplaceID(const QString& value) { _marketplaceID = value; }
|
void setMarketplaceID(const QString& value) { _marketplaceID = value; }
|
||||||
|
@ -305,9 +303,6 @@ public:
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
static bool _sendPhysicsUpdates;
|
static bool _sendPhysicsUpdates;
|
||||||
|
|
||||||
virtual void initFromEntityItemID(const EntityItemID& entityItemID); // maybe useful to allow subclasses to init
|
|
||||||
|
|
||||||
EntityTypes::EntityType _type;
|
EntityTypes::EntityType _type;
|
||||||
QUuid _id;
|
QUuid _id;
|
||||||
uint32_t _creatorTokenID;
|
uint32_t _creatorTokenID;
|
||||||
|
@ -347,7 +342,6 @@ protected:
|
||||||
QString _userData;
|
QString _userData;
|
||||||
QUuid _simulatorID; // id of Node which is currently responsible for simulating this Entity
|
QUuid _simulatorID; // id of Node which is currently responsible for simulating this Entity
|
||||||
quint64 _simulatorIDChangedTime; // when was _simulatorID last updated?
|
quint64 _simulatorIDChangedTime; // when was _simulatorID last updated?
|
||||||
bool _shouldClaimSimulationOwnership;
|
|
||||||
QString _marketplaceID;
|
QString _marketplaceID;
|
||||||
|
|
||||||
// NOTE: Damping is applied like this: v *= pow(1 - damping, dt)
|
// NOTE: Damping is applied like this: v *= pow(1 - damping, dt)
|
||||||
|
|
|
@ -65,17 +65,8 @@ void EntityScriptingInterface::setEntityTree(EntityTree* modelTree) {
|
||||||
void setSimId(EntityItemProperties& propertiesWithSimID, EntityItem* entity) {
|
void setSimId(EntityItemProperties& propertiesWithSimID, EntityItem* entity) {
|
||||||
auto nodeList = DependencyManager::get<NodeList>();
|
auto nodeList = DependencyManager::get<NodeList>();
|
||||||
const QUuid myNodeID = nodeList->getSessionUUID();
|
const QUuid myNodeID = nodeList->getSessionUUID();
|
||||||
|
|
||||||
// if this entity has non-zero physics/simulation related values, claim simulation ownership
|
|
||||||
if (propertiesWithSimID.velocityChanged() ||
|
|
||||||
propertiesWithSimID.rotationChanged() ||
|
|
||||||
propertiesWithSimID.containsPositionChange()) {
|
|
||||||
propertiesWithSimID.setSimulatorID(myNodeID);
|
propertiesWithSimID.setSimulatorID(myNodeID);
|
||||||
entity->setSimulatorID(myNodeID);
|
entity->setSimulatorID(myNodeID);
|
||||||
} else if (entity->getSimulatorID() == myNodeID) {
|
|
||||||
propertiesWithSimID.setSimulatorID(QUuid()); // give up simulation ownership
|
|
||||||
entity->setSimulatorID(QUuid());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,8 @@
|
||||||
#include "PhysicsHelpers.h"
|
#include "PhysicsHelpers.h"
|
||||||
#include "PhysicsLogging.h"
|
#include "PhysicsLogging.h"
|
||||||
|
|
||||||
static const float MEASURED_ACCELERATION_CLOSE_TO_ZERO = 0.05f;
|
static const float ACCELERATION_EQUIVALENT_EPSILON_RATIO = 0.1f;
|
||||||
|
static const quint8 STEPS_TO_DECIDE_BALLISTIC = 4;
|
||||||
|
|
||||||
QSet<EntityItem*>* _outgoingEntityList;
|
QSet<EntityItem*>* _outgoingEntityList;
|
||||||
|
|
||||||
|
@ -34,8 +35,11 @@ void EntityMotionState::enqueueOutgoingEntity(EntityItem* entity) {
|
||||||
_outgoingEntityList->insert(entity);
|
_outgoingEntityList->insert(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityMotionState::EntityMotionState(EntityItem* entity)
|
EntityMotionState::EntityMotionState(EntityItem* entity) :
|
||||||
: _entity(entity) {
|
_entity(entity),
|
||||||
|
_accelerationNearlyGravityCount(0),
|
||||||
|
_shouldClaimSimulationOwnership(false)
|
||||||
|
{
|
||||||
_type = MOTION_STATE_TYPE_ENTITY;
|
_type = MOTION_STATE_TYPE_ENTITY;
|
||||||
assert(entity != NULL);
|
assert(entity != NULL);
|
||||||
}
|
}
|
||||||
|
@ -191,7 +195,7 @@ bool EntityMotionState::shouldSendUpdate(uint32_t simulationFrame) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_entity->getShouldClaimSimulationOwnership()) {
|
if (getShouldClaimSimulationOwnership()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +203,7 @@ bool EntityMotionState::shouldSendUpdate(uint32_t simulationFrame) {
|
||||||
const QUuid& myNodeID = nodeList->getSessionUUID();
|
const QUuid& myNodeID = nodeList->getSessionUUID();
|
||||||
const QUuid& simulatorID = _entity->getSimulatorID();
|
const QUuid& simulatorID = _entity->getSimulatorID();
|
||||||
|
|
||||||
if (!simulatorID.isNull() && simulatorID != myNodeID) {
|
if (simulatorID != myNodeID) {
|
||||||
// some other Node owns the simulating of this, so don't broadcast the results of local simulation.
|
// some other Node owns the simulating of this, so don't broadcast the results of local simulation.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -214,10 +218,26 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, uint32_
|
||||||
if (_outgoingPacketFlags) {
|
if (_outgoingPacketFlags) {
|
||||||
EntityItemProperties properties = _entity->getProperties();
|
EntityItemProperties properties = _entity->getProperties();
|
||||||
|
|
||||||
if (glm::length(_measuredAcceleration) < MEASURED_ACCELERATION_CLOSE_TO_ZERO) {
|
float gravityLength = glm::length(_entity->getGravity());
|
||||||
_entity->setAcceleration(glm::vec3(0.0f));
|
float accVsGravity = glm::abs(glm::length(_measuredAcceleration) - gravityLength);
|
||||||
|
if (accVsGravity < ACCELERATION_EQUIVALENT_EPSILON_RATIO * gravityLength) {
|
||||||
|
// acceleration measured during the most recent simulation step was close to gravity.
|
||||||
|
if (getAccelerationNearlyGravityCount() < STEPS_TO_DECIDE_BALLISTIC) {
|
||||||
|
// only increment this if we haven't reached the threshold yet. this is to avoid
|
||||||
|
// overflowing the counter.
|
||||||
|
incrementAccelerationNearlyGravityCount();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// acceleration wasn't similar to this entities gravity, so reset the went-ballistic counter
|
||||||
|
resetAccelerationNearlyGravityCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
// if this entity has been accelerated at close to gravity for a certain number of simulation-steps, let
|
||||||
|
// the entity server's estimates include gravity.
|
||||||
|
if (getAccelerationNearlyGravityCount() >= STEPS_TO_DECIDE_BALLISTIC) {
|
||||||
_entity->setAcceleration(_entity->getGravity());
|
_entity->setAcceleration(_entity->getGravity());
|
||||||
|
} else {
|
||||||
|
_entity->setAcceleration(glm::vec3(0.0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_outgoingPacketFlags & EntityItem::DIRTY_POSITION) {
|
if (_outgoingPacketFlags & EntityItem::DIRTY_POSITION) {
|
||||||
|
@ -266,16 +286,13 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, uint32_
|
||||||
QUuid myNodeID = nodeList->getSessionUUID();
|
QUuid myNodeID = nodeList->getSessionUUID();
|
||||||
QUuid simulatorID = _entity->getSimulatorID();
|
QUuid simulatorID = _entity->getSimulatorID();
|
||||||
|
|
||||||
if (_entity->getShouldClaimSimulationOwnership()) {
|
if (getShouldClaimSimulationOwnership()) {
|
||||||
_entity->setSimulatorID(myNodeID);
|
_entity->setSimulatorID(myNodeID);
|
||||||
properties.setSimulatorID(myNodeID);
|
properties.setSimulatorID(myNodeID);
|
||||||
_entity->setShouldClaimSimulationOwnership(false);
|
setShouldClaimSimulationOwnership(false);
|
||||||
}
|
}
|
||||||
else if (simulatorID.isNull() && !(zeroSpeed && zeroSpin)) {
|
|
||||||
// The object is moving and nobody thinks they own the motion. set this Node as the simulator
|
if (simulatorID == myNodeID && zeroSpeed && zeroSpin) {
|
||||||
_entity->setSimulatorID(myNodeID);
|
|
||||||
properties.setSimulatorID(myNodeID);
|
|
||||||
} else if (simulatorID == myNodeID && zeroSpeed && zeroSpin) {
|
|
||||||
// we are the simulator and the object has stopped. give up "simulator" status
|
// we are the simulator and the object has stopped. give up "simulator" status
|
||||||
_entity->setSimulatorID(QUuid());
|
_entity->setSimulatorID(QUuid());
|
||||||
properties.setSimulatorID(QUuid());
|
properties.setSimulatorID(QUuid());
|
||||||
|
|
|
@ -62,10 +62,18 @@ public:
|
||||||
virtual uint32_t getIncomingDirtyFlags() const;
|
virtual uint32_t getIncomingDirtyFlags() const;
|
||||||
virtual void clearIncomingDirtyFlags(uint32_t flags) { _entity->clearDirtyFlags(flags); }
|
virtual void clearIncomingDirtyFlags(uint32_t flags) { _entity->clearDirtyFlags(flags); }
|
||||||
|
|
||||||
EntityItem* getEntity() const { return _entity; }
|
void incrementAccelerationNearlyGravityCount() { _accelerationNearlyGravityCount++; }
|
||||||
|
void resetAccelerationNearlyGravityCount() { _accelerationNearlyGravityCount = 0; }
|
||||||
|
quint8 getAccelerationNearlyGravityCount() { return _accelerationNearlyGravityCount; }
|
||||||
|
|
||||||
|
virtual EntityItem* getEntity() const { return _entity; }
|
||||||
|
virtual void setShouldClaimSimulationOwnership(bool value) { _shouldClaimSimulationOwnership = value; }
|
||||||
|
virtual bool getShouldClaimSimulationOwnership() { return _shouldClaimSimulationOwnership; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
EntityItem* _entity;
|
EntityItem* _entity;
|
||||||
|
quint8 _accelerationNearlyGravityCount;
|
||||||
|
bool _shouldClaimSimulationOwnership;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_EntityMotionState_h
|
#endif // hifi_EntityMotionState_h
|
||||||
|
|
|
@ -111,6 +111,12 @@ public:
|
||||||
virtual bool isMoving() const = 0;
|
virtual bool isMoving() const = 0;
|
||||||
|
|
||||||
friend class PhysicsEngine;
|
friend class PhysicsEngine;
|
||||||
|
|
||||||
|
// these are here so we can call into EntityMotionObject with a base-class pointer
|
||||||
|
virtual EntityItem* getEntity() const { return NULL; }
|
||||||
|
virtual void setShouldClaimSimulationOwnership(bool value) { }
|
||||||
|
virtual bool getShouldClaimSimulationOwnership() { return false; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void setRigidBody(btRigidBody* body);
|
void setRigidBody(btRigidBody* body);
|
||||||
|
|
||||||
|
|
|
@ -348,10 +348,10 @@ void PhysicsEngine::stepSimulation() {
|
||||||
_characterController->postSimulation();
|
_characterController->postSimulation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
computeCollisionEvents();
|
||||||
|
|
||||||
unlock();
|
unlock();
|
||||||
_entityTree->unlock();
|
_entityTree->unlock();
|
||||||
|
|
||||||
computeCollisionEvents();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,6 +369,9 @@ void PhysicsEngine::stepNonPhysicalKinematics(const quint64& now) {
|
||||||
void PhysicsEngine::computeCollisionEvents() {
|
void PhysicsEngine::computeCollisionEvents() {
|
||||||
BT_PROFILE("computeCollisionEvents");
|
BT_PROFILE("computeCollisionEvents");
|
||||||
|
|
||||||
|
auto nodeList = DependencyManager::get<NodeList>();
|
||||||
|
QUuid myNodeID = nodeList->getSessionUUID();
|
||||||
|
|
||||||
const btCollisionObject* characterCollisionObject =
|
const btCollisionObject* characterCollisionObject =
|
||||||
_characterController ? _characterController->getCollisionObject() : NULL;
|
_characterController ? _characterController->getCollisionObject() : NULL;
|
||||||
|
|
||||||
|
@ -388,21 +391,31 @@ void PhysicsEngine::computeCollisionEvents() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* a = objectA->getUserPointer();
|
ObjectMotionState* a = static_cast<ObjectMotionState*>(objectA->getUserPointer());
|
||||||
void* b = objectB->getUserPointer();
|
ObjectMotionState* b = static_cast<ObjectMotionState*>(objectB->getUserPointer());
|
||||||
|
EntityItem* entityA = a ? a->getEntity() : NULL;
|
||||||
|
EntityItem* entityB = b ? b->getEntity() : NULL;
|
||||||
|
bool aIsDynamic = entityA && !objectA->isStaticOrKinematicObject();
|
||||||
|
bool bIsDynamic = entityB && !objectB->isStaticOrKinematicObject();
|
||||||
|
|
||||||
if (a || b) {
|
if (a || b) {
|
||||||
// the manifold has up to 4 distinct points, but only extract info from the first
|
// the manifold has up to 4 distinct points, but only extract info from the first
|
||||||
_contactMap[ContactKey(a, b)].update(_numContactFrames, contactManifold->getContactPoint(0), _originOffset);
|
_contactMap[ContactKey(a, b)].update(_numContactFrames, contactManifold->getContactPoint(0), _originOffset);
|
||||||
|
|
||||||
// if our character capsule is colliding with something dynamic, claim simulation ownership.
|
|
||||||
// see EntityMotionState::sendUpdate
|
|
||||||
if (objectA == characterCollisionObject && !objectB->isStaticOrKinematicObject() && b) {
|
|
||||||
EntityItem* entityB = static_cast<EntityMotionState*>(b)->getEntity();
|
|
||||||
entityB->setShouldClaimSimulationOwnership(true);
|
|
||||||
}
|
}
|
||||||
if (objectB == characterCollisionObject && !objectA->isStaticOrKinematicObject() && a) {
|
// collisions cause infectious spread of simulation-ownership. we also attempt to take
|
||||||
EntityItem* entityA = static_cast<EntityMotionState*>(a)->getEntity();
|
// ownership of anything that collides with our avatar.
|
||||||
entityA->setShouldClaimSimulationOwnership(true);
|
if ((aIsDynamic && entityA->getSimulatorID() == myNodeID) ||
|
||||||
|
(a && a->getShouldClaimSimulationOwnership()) ||
|
||||||
|
(objectA == characterCollisionObject)) {
|
||||||
|
if (bIsDynamic) {
|
||||||
|
b->setShouldClaimSimulationOwnership(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((bIsDynamic && entityB->getSimulatorID() == myNodeID) ||
|
||||||
|
(b && b->getShouldClaimSimulationOwnership()) ||
|
||||||
|
(objectB == characterCollisionObject)) {
|
||||||
|
if (aIsDynamic) {
|
||||||
|
a->setShouldClaimSimulationOwnership(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -413,7 +426,6 @@ void PhysicsEngine::computeCollisionEvents() {
|
||||||
// scan known contacts and trigger events
|
// scan known contacts and trigger events
|
||||||
ContactMap::iterator contactItr = _contactMap.begin();
|
ContactMap::iterator contactItr = _contactMap.begin();
|
||||||
|
|
||||||
|
|
||||||
while (contactItr != _contactMap.end()) {
|
while (contactItr != _contactMap.end()) {
|
||||||
ObjectMotionState* A = static_cast<ObjectMotionState*>(contactItr->first._a);
|
ObjectMotionState* A = static_cast<ObjectMotionState*>(contactItr->first._a);
|
||||||
ObjectMotionState* B = static_cast<ObjectMotionState*>(contactItr->first._b);
|
ObjectMotionState* B = static_cast<ObjectMotionState*>(contactItr->first._b);
|
||||||
|
@ -521,9 +533,55 @@ void PhysicsEngine::addObject(const ShapeInfo& shapeInfo, btCollisionShape* shap
|
||||||
motionState->resetMeasuredAcceleration();
|
motionState->resetMeasuredAcceleration();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PhysicsEngine::bump(EntityItem* bumpEntity) {
|
||||||
|
// If this node is doing something like deleting an entity, scan for contacts involving the
|
||||||
|
// entity. For each found, flag the other entity involved as being simulated by this node.
|
||||||
|
lock();
|
||||||
|
int numManifolds = _collisionDispatcher->getNumManifolds();
|
||||||
|
for (int i = 0; i < numManifolds; ++i) {
|
||||||
|
btPersistentManifold* contactManifold = _collisionDispatcher->getManifoldByIndexInternal(i);
|
||||||
|
if (contactManifold->getNumContacts() > 0) {
|
||||||
|
const btCollisionObject* objectA = static_cast<const btCollisionObject*>(contactManifold->getBody0());
|
||||||
|
const btCollisionObject* objectB = static_cast<const btCollisionObject*>(contactManifold->getBody1());
|
||||||
|
if (objectA && objectB) {
|
||||||
|
void* a = objectA->getUserPointer();
|
||||||
|
void* b = objectB->getUserPointer();
|
||||||
|
if (a && b) {
|
||||||
|
EntityMotionState* entityMotionStateA = static_cast<EntityMotionState*>(a);
|
||||||
|
EntityMotionState* entityMotionStateB = static_cast<EntityMotionState*>(b);
|
||||||
|
EntityItem* entityA = entityMotionStateA ? entityMotionStateA->getEntity() : NULL;
|
||||||
|
EntityItem* entityB = entityMotionStateB ? entityMotionStateB->getEntity() : NULL;
|
||||||
|
if (entityA && entityB) {
|
||||||
|
if (entityA == bumpEntity) {
|
||||||
|
entityMotionStateB->setShouldClaimSimulationOwnership(true);
|
||||||
|
if (!objectB->isActive()) {
|
||||||
|
objectB->setActivationState(ACTIVE_TAG);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (entityB == bumpEntity) {
|
||||||
|
entityMotionStateA->setShouldClaimSimulationOwnership(true);
|
||||||
|
if (!objectA->isActive()) {
|
||||||
|
objectA->setActivationState(ACTIVE_TAG);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unlock();
|
||||||
|
}
|
||||||
|
|
||||||
void PhysicsEngine::removeObjectFromBullet(ObjectMotionState* motionState) {
|
void PhysicsEngine::removeObjectFromBullet(ObjectMotionState* motionState) {
|
||||||
assert(motionState);
|
assert(motionState);
|
||||||
btRigidBody* body = motionState->getRigidBody();
|
btRigidBody* body = motionState->getRigidBody();
|
||||||
|
|
||||||
|
// set the about-to-be-deleted entity active in order to wake up the island it's part of. this is done
|
||||||
|
// so that anything resting on top of it will fall.
|
||||||
|
// body->setActivationState(ACTIVE_TAG);
|
||||||
|
EntityItem* entity = static_cast<EntityMotionState*>(motionState)->getEntity();
|
||||||
|
bump(entity);
|
||||||
|
|
||||||
if (body) {
|
if (body) {
|
||||||
const btCollisionShape* shape = body->getCollisionShape();
|
const btCollisionShape* shape = body->getCollisionShape();
|
||||||
_dynamicsWorld->removeRigidBody(body);
|
_dynamicsWorld->removeRigidBody(body);
|
||||||
|
|
|
@ -98,6 +98,7 @@ private:
|
||||||
// return 'true' of update was successful
|
// return 'true' of update was successful
|
||||||
bool updateObjectHard(btRigidBody* body, ObjectMotionState* motionState, uint32_t flags);
|
bool updateObjectHard(btRigidBody* body, ObjectMotionState* motionState, uint32_t flags);
|
||||||
void updateObjectEasy(btRigidBody* body, ObjectMotionState* motionState, uint32_t flags);
|
void updateObjectEasy(btRigidBody* body, ObjectMotionState* motionState, uint32_t flags);
|
||||||
|
void bump(EntityItem* bumpEntity);
|
||||||
|
|
||||||
btClock _clock;
|
btClock _clock;
|
||||||
btDefaultCollisionConfiguration* _collisionConfig = NULL;
|
btDefaultCollisionConfiguration* _collisionConfig = NULL;
|
||||||
|
|
Loading…
Reference in a new issue