add EntityItem::updateFoo() methods that set flags

when properties are actually changed
This commit is contained in:
Andrew Meadows 2014-11-19 14:01:55 -08:00
parent 3864adf82d
commit 3b85805048
2 changed files with 134 additions and 9 deletions

View file

@ -94,6 +94,7 @@ EntityItem::EntityItem(const EntityItemID& entityItemID) {
#ifdef USE_BULLET_PHYSICS
_motionState = NULL;
#endif // USE_BULLET_PHYSICS
_updateFlags = 0;
_changedOnServer = 0;
initFromEntityItemID(entityItemID);
_simulationState = EntityItem::Static;
@ -109,6 +110,7 @@ EntityItem::EntityItem(const EntityItemID& entityItemID, const EntityItemPropert
#ifdef USE_BULLET_PHYSICS
_motionState = NULL;
#endif // USE_BULLET_PHYSICS
_updateFlags = 0;
_changedOnServer = 0;
initFromEntityItemID(entityItemID);
setProperties(properties, true); // force copy
@ -523,7 +525,7 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
READ_ENTITY_PROPERTY(PROP_ANGULAR_DAMPING, float, _angularDamping);
READ_ENTITY_PROPERTY(PROP_VISIBLE, bool, _visible);
READ_ENTITY_PROPERTY(PROP_IGNORE_FOR_COLLISIONS, bool, _ignoreForCollisions);
READ_ENTITY_PROPERTY(PROP_COLLISIONS_WILL_MOVE, bool, _collisionsWillMove);
READ_ENTITY_PROPERTY_SETTER(PROP_COLLISIONS_WILL_MOVE, bool, setCollisionsWillMove);
READ_ENTITY_PROPERTY(PROP_LOCKED, bool, _locked);
READ_ENTITY_PROPERTY_STRING(PROP_USER_DATA,setUserData);
@ -750,11 +752,6 @@ bool EntityItem::lifetimeHasExpired() const {
return isMortal() && (getAge() > getLifetime());
}
void EntityItem::copyChangedProperties(const EntityItem& other) {
*this = other;
}
EntityItemProperties EntityItem::getProperties() const {
EntityItemProperties properties;
properties._id = getID();
@ -967,6 +964,106 @@ void EntityItem::recalculateCollisionShape() {
// TODO: use motionState to update physics object
}
void EntityItem::updatePosition(const glm::vec3& value) {
if (_position != value) {
_position = value;
recalculateCollisionShape();
_updateFlags |= UPDATE_POSITION;
}
}
void EntityItem::updatePositionInMeters(const glm::vec3& value) {
glm::vec3 position = glm::clamp(value / (float) TREE_SCALE, 0.0f, 1.0f);
if (_position != position) {
_position = position;
recalculateCollisionShape();
_updateFlags |= UPDATE_POSITION;
}
}
void EntityItem::updateDimensions(const glm::vec3& value) {
if (_dimensions != value) {
_dimensions = value;
recalculateCollisionShape();
_updateFlags |= UPDATE_SHAPE;
}
}
void EntityItem::updateDimensionsInMeters(const glm::vec3& value) {
glm::vec3 dimensions = value / (float) TREE_SCALE;
if (_dimensions != dimensions) {
_dimensions = dimensions;
recalculateCollisionShape();
_updateFlags |= UPDATE_SHAPE;
}
}
void EntityItem::updateRotation(const glm::quat& rotation) {
if (_rotation != rotation) {
_rotation = rotation;
recalculateCollisionShape();
_updateFlags |= UPDATE_POSITION;
}
}
void EntityItem::updateMass(float value) {
if (_mass != value) {
_mass = value;
_updateFlags |= UPDATE_MASS;
}
}
void EntityItem::updateVelocity(const glm::vec3& value) {
if (_velocity != value) {
_velocity = value;
_updateFlags |= UPDATE_VELOCITY;
}
}
void EntityItem::updateVelocityInMeters(const glm::vec3& value) {
glm::vec3 velocity = value / (float) TREE_SCALE;
if (_velocity != velocity) {
_velocity = velocity;
_updateFlags |= UPDATE_VELOCITY;
}
}
void EntityItem::updateGravity(const glm::vec3& value) {
if (_gravity != value) {
_gravity = value;
_updateFlags |= UPDATE_VELOCITY;
}
}
void EntityItem::updateGravityInMeters(const glm::vec3& value) {
glm::vec3 gravity = value / (float) TREE_SCALE;
if (_gravity != gravity) {
_gravity = gravity;
_updateFlags |= UPDATE_VELOCITY;
}
}
void EntityItem::updateAngularVelocity(const glm::vec3& value) {
if (_angularVelocity != value) {
_angularVelocity = value;
_updateFlags |= UPDATE_VELOCITY;
}
}
void EntityItem::updateIgnoreForCollisions(bool value) {
if (_ignoreForCollisions != value) {
_ignoreForCollisions = value;
_updateFlags |= UPDATE_COLLISION;
}
}
void EntityItem::updateCollisionsWillMove(bool value) {
if (_collisionsWillMove != value) {
_collisionsWillMove = value;
_updateFlags |= UPDATE_COLLISION;
}
}
#ifdef USE_BULLET_PHYSICS
void EntityItem::destroyMotionState() {
if (_motionState) {

View file

@ -39,6 +39,15 @@ class EntityTreeElementExtraEncodeData;
class EntityMotionState;
#endif // USE_BULLET_PHYSICS
enum EntityUpdateFlags {
UPDATE_POSITION = 0x0001,
UPDATE_VELOCITY = 0x0002,
UPDATE_MASS = 0x0004,
UPDATE_COLLISION = 0x0008,
UPDATE_SHAPE = 0x0010,
//UPDATE_APPEARANCE = 0x8000,
};
/// EntityItem class this is the base class for all entity types. It handles the basic properties and functionality available
/// to all other entity types. In particular: postion, size, rotation, age, lifetime, velocity, gravity. You can not instantiate
@ -129,9 +138,6 @@ public:
virtual void debugDump() const;
// similar to assignment/copy, but it handles keeping lifetime accurate
void copyChangedProperties(const EntityItem& other);
// attributes applicable to all entity types
EntityTypes::EntityType getType() const { return _type; }
const glm::vec3& getPosition() const { return _position; } /// get position in domain scale units (0.0 - 1.0)
@ -270,6 +276,24 @@ public:
virtual const Shape& getCollisionShapeInMeters() const { return _collisionShape; }
virtual bool contains(const glm::vec3& point) const { return getAABox().contains(point); }
// updateFoo() methods to be used when changes need to be accumulated in the _updateFlags
void updatePosition(const glm::vec3& value);
void updatePositionInMeters(const glm::vec3& value);
void updateDimensions(const glm::vec3& value);
void updateDimensionsInMeters(const glm::vec3& value);
void updateRotation(const glm::quat& rotation);
void updateMass(float value);
void updateVelocity(const glm::vec3& value);
void updateVelocityInMeters(const glm::vec3& value);
void updateGravity(const glm::vec3& value);
void updateGravityInMeters(const glm::vec3& value);
void updateAngularVelocity(const glm::vec3& value);
void updateIgnoreForCollisions(bool value);
void updateCollisionsWillMove(bool value);
void setUpdateFlags(uint32_t mask) { _updateFlags |= mask; }
void clearUpdateFlags(uint32_t mask) { _updateFlags &= ~mask; }
#ifdef USE_BULLET_PHYSICS
EntityMotionState* getMotionState() const { return _motionState; }
virtual EntityMotionState* createMotionState() { return NULL; }
@ -326,6 +350,10 @@ protected:
EntityMotionState* _motionState;
#endif // USE_BULLET_PHYSICS
SimulationState _simulationState; // only set by EntityTree
// UpdateFlags are set whenever a property changes that requires the change to be communicated to other
// data structures. It is the responsibility of the EntityTree to relay changes entity and clear flags.
uint32_t _updateFlags;
};