improved velocity behavior, handling lastUpdated better, changing state when velocity reaches zero

This commit is contained in:
ZappoMan 2014-08-11 11:56:05 -07:00
parent 7c8163c475
commit b89c740e17
3 changed files with 50 additions and 6 deletions

View file

@ -35,6 +35,7 @@ const float EntityItem::DEFAULT_MASS = 1.0f;
const float EntityItem::DEFAULT_LIFETIME = EntityItem::IMMORTAL;
const float EntityItem::DEFAULT_DAMPING = 0.99f;
const glm::vec3 EntityItem::NO_VELOCITY = glm::vec3(0, 0, 0);
const float EntityItem::EPSILON_VELOCITY_LENGTH = (1.0f / 10000.0f) / (float)TREE_SCALE; // really small
const glm::vec3 EntityItem::DEFAULT_VELOCITY = EntityItem::NO_VELOCITY;
const glm::vec3 EntityItem::NO_GRAVITY = glm::vec3(0, 0, 0);
const glm::vec3 EntityItem::REGULAR_GRAVITY = glm::vec3(0, (-9.8f / TREE_SCALE), 0);
@ -493,6 +494,7 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
quint64 updateDelta = updateDeltaCoder;
if (overwriteLocalData) {
_lastUpdated = _lastEdited + updateDelta; // don't adjust for clock skew since we already did that for _lastEdited
qDebug() << "%%%%%%%%%%%%%%%% EntityItem::readEntityDataFromBuffer() .... SETTING _lastUpdated=" << _lastUpdated;
}
encodedUpdateDelta = updateDeltaCoder; // determine true length
dataAt += encodedUpdateDelta.size();
@ -663,6 +665,8 @@ void EntityItem::update(const quint64& updateTime) {
float timeElapsed = (float)(updateTime - _lastUpdated) / (float)(USECS_PER_SECOND);
_lastUpdated = updateTime;
qDebug() << "********** EntityItem::update() .... SETTING _lastUpdated=" << _lastUpdated;
if (isMortal()) {
if (getAge() > getLifetime()) {
qDebug() << "Lifetime has expired... WHAT TO DO??? getAge()=" << getAge() << "getLifetime()=" << getLifetime();
@ -673,25 +677,45 @@ void EntityItem::update(const quint64& updateTime) {
if (hasVelocity() || hasGravity()) {
glm::vec3 position = getPosition();
glm::vec3 velocity = getVelocity();
qDebug() << "EntityItem::update()....";
qDebug() << " timeElapsed:" << timeElapsed;
qDebug() << " old AACube:" << getAACube();
qDebug() << " old position:" << position;
qDebug() << " old velocity:" << velocity;
position += velocity * timeElapsed;
// handle bounces off the ground...
if (position.y <= 0) {
velocity = velocity * glm::vec3(1,-1,1);
position.y = 0;
qDebug() << "################### handle bounces off the ground...... velocity=" << velocity;
}
// handle gravity....
velocity += getGravity() * timeElapsed;
if (hasGravity()) {
velocity += getGravity() * timeElapsed;
qDebug() << "################### apply gravity......";
qDebug() << " getGravity()=" << getGravity();
qDebug() << " velocity=" << velocity;
}
// handle damping
glm::vec3 dampingResistance = velocity * getDamping();
qDebug() << " getDamping():" << getDamping();
qDebug() << " dampingResistance:" << dampingResistance;
qDebug() << " dampingResistance * timeElapsed:" << dampingResistance * timeElapsed;
velocity -= dampingResistance * timeElapsed;
qDebug() << " velocity AFTER dampingResistance:" << velocity;
qDebug() << "EntityItem::update()....";
qDebug() << " old AACube:" << getAACube();
qDebug() << " position:" << position;
qDebug() << " velocity:" << velocity;
// round velocity to zero if it's close enough...
if (glm::length(velocity) <= EPSILON_VELOCITY_LENGTH) {
velocity = NO_VELOCITY;
}
qDebug() << " new position:" << position;
qDebug() << " new velocity:" << velocity;
setPosition(position);
setVelocity(velocity);
qDebug() << " new AACube:" << getAACube();

View file

@ -52,7 +52,7 @@ public:
quint64 getLastUpdated() const { return _lastUpdated; } /// Last simulated time of this entity universal usecs
quint64 getLastEdited() const { return _lastEdited; } /// Last edited time of this entity universal usecs
void setLastEdited(quint64 lastEdited) { _lastEdited = lastEdited; }
void setLastEdited(quint64 lastEdited) { _lastEdited = lastEdited; _lastUpdated = lastEdited; }
float getEditedAgo() const /// Elapsed seconds since this entity was last edited
{ return (float)(usecTimestampNow() - _lastEdited) / (float)USECS_PER_SECOND; }
@ -124,6 +124,7 @@ public:
static const glm::vec3 DEFAULT_VELOCITY;
static const glm::vec3 NO_VELOCITY;
static const float EPSILON_VELOCITY_LENGTH;
const glm::vec3& getVelocity() const { return _velocity; } /// velocity in domain scale units (0.0-1.0) per second
void setVelocity(const glm::vec3& value) { _velocity = value; } /// velocity in domain scale units (0.0-1.0) per second
bool hasVelocity() const { return _velocity != NO_VELOCITY; }

View file

@ -1079,6 +1079,9 @@ void EntityTree::update() {
}
MovingEntitiesOperator moveOperator(this);
QSet<EntityItem*> entitiesBecomingStatic;
QSet<EntityItem*> entitiesBecomingChanging;
for (int i = 0; i < _movingEntities.size(); i++) {
EntityItem* thisEntity = _movingEntities[i];
@ -1091,9 +1094,25 @@ void EntityTree::update() {
qDebug() << " newCube=" << newCube;
moveOperator.addEntityToMoveList(thisEntity, oldCube, newCube);
// check to see if this entity is no longer moving
EntityItem::SimuationState newState = thisEntity->getSimulationState();
if (newState == EntityItem::Changing) {
entitiesBecomingChanging << thisEntity;
} else if (newState == EntityItem::Static) {
entitiesBecomingStatic << thisEntity;
}
}
recurseTreeWithOperator(&moveOperator);
// for any and all entities that were moving but are now either static or changing
// change their state accordingly
foreach(EntityItem* entity, entitiesBecomingStatic) {
changeEntityState(entity, EntityItem::Moving, EntityItem::Static);
}
foreach(EntityItem* entity, entitiesBecomingChanging) {
changeEntityState(entity, EntityItem::Moving, EntityItem::Changing);
}
unlock();