simpler EntityItem::simulate() bypass for bullet

This commit is contained in:
Andrew Meadows 2014-12-18 13:56:18 -08:00
parent 85fd1fc14f
commit dab1f026d7

View file

@ -587,6 +587,11 @@ bool EntityItem::isRestingOnSurface() const {
} }
void EntityItem::simulate(const quint64& now) { void EntityItem::simulate(const quint64& now) {
if (_physicsInfo) {
// we rely on bullet for simulation, so bail
return;
}
bool wantDebug = false; bool wantDebug = false;
if (_lastSimulated == 0) { if (_lastSimulated == 0) {
@ -636,110 +641,108 @@ void EntityItem::simulate(const quint64& now) {
qDebug() << " ********** EntityItem::simulate() .... SETTING _lastSimulated=" << _lastSimulated; qDebug() << " ********** EntityItem::simulate() .... SETTING _lastSimulated=" << _lastSimulated;
} }
if (!_physicsInfo) { if (hasAngularVelocity()) {
if (hasAngularVelocity()) { glm::quat rotation = getRotation();
glm::quat rotation = getRotation(); glm::vec3 angularVelocity = glm::radians(getAngularVelocity());
glm::vec3 angularVelocity = glm::radians(getAngularVelocity()); float angularSpeed = glm::length(angularVelocity);
float angularSpeed = glm::length(angularVelocity);
if (angularSpeed < EPSILON_VELOCITY_LENGTH) {
if (angularSpeed < EPSILON_VELOCITY_LENGTH) { setAngularVelocity(NO_ANGULAR_VELOCITY);
setAngularVelocity(NO_ANGULAR_VELOCITY); } else {
} else { float angle = timeElapsed * angularSpeed;
float angle = timeElapsed * angularSpeed; glm::quat dQ = glm::angleAxis(angle, glm::normalize(angularVelocity));
glm::quat dQ = glm::angleAxis(angle, glm::normalize(angularVelocity)); rotation = dQ * rotation;
rotation = dQ * rotation; setRotation(rotation);
setRotation(rotation);
// handle damping for angular velocity
// handle damping for angular velocity float dampingTimescale = getAngularDamping();
float dampingTimescale = getAngularDamping(); if (dampingTimescale > 0.0f) {
if (dampingTimescale > 0.0f) { float dampingFactor = glm::clamp(timeElapsed / dampingTimescale, 0.0f, 1.0f);
float dampingFactor = glm::clamp(timeElapsed / dampingTimescale, 0.0f, 1.0f); glm::vec3 newAngularVelocity = (1.0f - dampingFactor) * getAngularVelocity();
glm::vec3 newAngularVelocity = (1.0f - dampingFactor) * getAngularVelocity(); setAngularVelocity(newAngularVelocity);
setAngularVelocity(newAngularVelocity); if (wantDebug) {
if (wantDebug) { qDebug() << " dampingTimescale :" << dampingTimescale;
qDebug() << " dampingTimescale :" << dampingTimescale; qDebug() << " newAngularVelocity:" << newAngularVelocity;
qDebug() << " newAngularVelocity:" << newAngularVelocity;
}
} }
} }
} }
}
if (hasVelocity() || hasGravity()) {
glm::vec3 position = getPosition();
glm::vec3 velocity = getVelocity();
glm::vec3 newPosition = position + (velocity * timeElapsed);
if (wantDebug) {
qDebug() << " EntityItem::simulate()....";
qDebug() << " timeElapsed:" << timeElapsed;
qDebug() << " old AACube:" << getMaximumAACube();
qDebug() << " old position:" << position;
qDebug() << " old velocity:" << velocity;
qDebug() << " old getAABox:" << getAABox();
qDebug() << " getDistanceToBottomOfEntity():" << getDistanceToBottomOfEntity() * (float)TREE_SCALE << " in meters";
qDebug() << " newPosition:" << newPosition;
qDebug() << " glm::distance(newPosition, position):" << glm::distance(newPosition, position);
}
position = newPosition;
// handle bounces off the ground... We bounce at the distance to the bottom of our entity
if (position.y <= getDistanceToBottomOfEntity()) {
velocity = velocity * glm::vec3(1,-1,1);
// if we've slowed considerably, then just stop moving
if (glm::length(velocity) <= EPSILON_VELOCITY_LENGTH) {
velocity = NO_VELOCITY;
}
position.y = getDistanceToBottomOfEntity();
}
// handle gravity....
if (hasGravity()) {
// handle resting on surface case, this is definitely a bit of a hack, and it only works on the
// "ground" plane of the domain, but for now it what we've got
if (isRestingOnSurface()) {
velocity.y = 0.0f;
position.y = getDistanceToBottomOfEntity();
} else {
velocity += getGravity() * timeElapsed;
}
}
// handle damping for velocity if (hasVelocity() || hasGravity()) {
float dampingTimescale = getDamping(); glm::vec3 position = getPosition();
if (dampingTimescale > 0.0f) { glm::vec3 velocity = getVelocity();
float dampingFactor = glm::clamp(timeElapsed / dampingTimescale, 0.0f, 1.0f); glm::vec3 newPosition = position + (velocity * timeElapsed);
velocity *= (1.0f - dampingFactor);
if (wantDebug) { if (wantDebug) {
qDebug() << " dampingTimescale:" << dampingTimescale; qDebug() << " EntityItem::simulate()....";
qDebug() << " newVelocity:" << velocity; qDebug() << " timeElapsed:" << timeElapsed;
} qDebug() << " old AACube:" << getMaximumAACube();
} qDebug() << " old position:" << position;
qDebug() << " old velocity:" << velocity;
qDebug() << " old getAABox:" << getAABox();
qDebug() << " getDistanceToBottomOfEntity():" << getDistanceToBottomOfEntity() * (float)TREE_SCALE << " in meters";
qDebug() << " newPosition:" << newPosition;
qDebug() << " glm::distance(newPosition, position):" << glm::distance(newPosition, position);
}
position = newPosition;
// handle bounces off the ground... We bounce at the distance to the bottom of our entity
if (position.y <= getDistanceToBottomOfEntity()) {
velocity = velocity * glm::vec3(1,-1,1);
if (wantDebug) {
qDebug() << " velocity AFTER dampingResistance:" << velocity;
qDebug() << " glm::length(velocity):" << glm::length(velocity);
qDebug() << " EPSILON_VELOCITY_LENGTH:" << EPSILON_VELOCITY_LENGTH;
}
// if we've slowed considerably, then just stop moving // if we've slowed considerably, then just stop moving
if (glm::length(velocity) <= EPSILON_VELOCITY_LENGTH) { if (glm::length(velocity) <= EPSILON_VELOCITY_LENGTH) {
velocity = NO_VELOCITY; velocity = NO_VELOCITY;
} }
// NOTE: the simulation should NOT set any DirtyFlags on this entity
setPosition(position); // this will automatically recalculate our collision shape
setVelocity(velocity);
if (wantDebug) { position.y = getDistanceToBottomOfEntity();
qDebug() << " new position:" << position; }
qDebug() << " new velocity:" << velocity;
qDebug() << " new AACube:" << getMaximumAACube(); // handle gravity....
qDebug() << " old getAABox:" << getAABox(); if (hasGravity()) {
// handle resting on surface case, this is definitely a bit of a hack, and it only works on the
// "ground" plane of the domain, but for now it what we've got
if (isRestingOnSurface()) {
velocity.y = 0.0f;
position.y = getDistanceToBottomOfEntity();
} else {
velocity += getGravity() * timeElapsed;
} }
} }
// handle damping for velocity
float dampingTimescale = getDamping();
if (dampingTimescale > 0.0f) {
float dampingFactor = glm::clamp(timeElapsed / dampingTimescale, 0.0f, 1.0f);
velocity *= (1.0f - dampingFactor);
if (wantDebug) {
qDebug() << " dampingTimescale:" << dampingTimescale;
qDebug() << " newVelocity:" << velocity;
}
}
if (wantDebug) {
qDebug() << " velocity AFTER dampingResistance:" << velocity;
qDebug() << " glm::length(velocity):" << glm::length(velocity);
qDebug() << " EPSILON_VELOCITY_LENGTH:" << EPSILON_VELOCITY_LENGTH;
}
// round velocity to zero if it's close enough...
if (glm::length(velocity) <= EPSILON_VELOCITY_LENGTH) {
velocity = NO_VELOCITY;
}
// NOTE: the simulation should NOT set any DirtyFlags on this entity
setPosition(position); // this will automatically recalculate our collision shape
setVelocity(velocity);
if (wantDebug) {
qDebug() << " new position:" << position;
qDebug() << " new velocity:" << velocity;
qDebug() << " new AACube:" << getMaximumAACube();
qDebug() << " old getAABox:" << getAABox();
}
} }
_lastSimulated = now; _lastSimulated = now;