Shape::getCenter() -> Shape::getTranslation()

This commit is contained in:
Andrew Meadows 2014-06-18 08:55:48 -07:00
parent b9d4545aef
commit 9e839f0980
15 changed files with 99 additions and 101 deletions

View file

@ -1245,7 +1245,7 @@ void MyAvatar::updateCollisionWithVoxels(float deltaTime, float radius) {
float capsuleHalfHeight = boundingShape.getHalfHeight();
const float MAX_STEP_HEIGHT = capsuleRadius + capsuleHalfHeight;
const float MIN_STEP_HEIGHT = 0.0f;
glm::vec3 footBase = boundingShape.getCenter() - (capsuleRadius + capsuleHalfHeight) * _worldUpDirection;
glm::vec3 footBase = boundingShape.getTranslation() - (capsuleRadius + capsuleHalfHeight) * _worldUpDirection;
float highestStep = 0.0f;
float lowestStep = MAX_STEP_HEIGHT;
glm::vec3 floorPoint;
@ -1262,7 +1262,7 @@ void MyAvatar::updateCollisionWithVoxels(float deltaTime, float radius) {
if (horizontalDepth > capsuleRadius || fabsf(verticalDepth) > MAX_STEP_HEIGHT) {
isTrapped = true;
if (_trapDuration > MAX_TRAP_PERIOD) {
float distance = glm::dot(boundingShape.getCenter() - cubeCenter, _worldUpDirection);
float distance = glm::dot(boundingShape.getTranslation() - cubeCenter, _worldUpDirection);
if (distance < 0.0f) {
distance = fabsf(distance) + 0.5f * cubeSide;
}

View file

@ -596,7 +596,7 @@ void SkeletonModel::updateShapePositionsLegacy() {
glm::vec3 worldPosition = _translation + _rotation * (state.getPosition() + shapeOffset);
Shape* shape = _shapes[i];
if (shape) {
shape->setCenter(worldPosition);
shape->setTranslation(worldPosition);
shape->setRotation(_rotation * stateRotation * joint.shapeRotation);
float distance = glm::distance(worldPosition, _translation) + shape->getBoundingRadius();
if (distance > _boundingRadius) {
@ -608,7 +608,7 @@ void SkeletonModel::updateShapePositionsLegacy() {
}
}
_shapesAreDirty = false;
_boundingShape.setCenter(rootPosition + _rotation * _boundingShapeLocalOffset);
_boundingShape.setTranslation(rootPosition + _rotation * _boundingShapeLocalOffset);
_boundingShape.setRotation(_rotation);
}
}
@ -665,7 +665,7 @@ void SkeletonModel::computeBoundingShape(const FBXGeometry& geometry) {
const FBXJoint& joint = geometry.joints[i];
glm::vec3 jointToShapeOffset = uniformScale * (finalRotations[i] * joint.shapePosition);
glm::vec3 localPosition = extractTranslation(transforms[i]) + jointToShapeOffset;
shape->setCenter(localPosition);
shape->setTranslation(localPosition);
shape->setRotation(finalRotations[i] * joint.shapeRotation);
float distance = glm::length(localPosition) + shape->getBoundingRadius();
if (distance > _boundingRadius) {
@ -684,7 +684,7 @@ void SkeletonModel::computeBoundingShape(const FBXGeometry& geometry) {
}
Extents shapeExtents;
shapeExtents.reset();
glm::vec3 localPosition = shape->getCenter();
glm::vec3 localPosition = shape->getTranslation();
int type = shape->getType();
if (type == Shape::CAPSULE_SHAPE) {
// add the two furthest surface points of the capsule
@ -739,11 +739,11 @@ void SkeletonModel::resetShapePositions() {
for (int i = 0; i < _shapes.size(); i++) {
Shape* shape = _shapes[i];
if (shape) {
shape->setCenter(_translation + _rotation * shape->getCenter());
shape->setTranslation(_translation + _rotation * shape->getTranslation());
shape->setRotation(_rotation * shape->getRotation());
}
}
_boundingShape.setCenter(_translation + _rotation * _boundingShapeLocalOffset);
_boundingShape.setTranslation(_translation + _rotation * _boundingShapeLocalOffset);
_boundingShape.setRotation(_rotation);
}

View file

@ -1102,7 +1102,7 @@ void Model::renderJointCollisionShapes(float alpha) {
glPushMatrix();
if (shape->getType() == Shape::SPHERE_SHAPE) {
// shapes are stored in world-frame, so we have to transform into model frame
glm::vec3 position = shape->getCenter() - _translation;
glm::vec3 position = shape->getTranslation() - _translation;
glTranslatef(position.x, position.y, position.z);
const glm::quat& rotation = shape->getRotation();
glm::vec3 axis = glm::axis(rotation);

View file

@ -757,7 +757,7 @@ bool findShapeCollisionsOp(OctreeElement* element, void* extraData) {
// coarse check against bounds
AACube cube = element->getAACube();
cube.scale(TREE_SCALE);
if (!cube.expandedContains(args->shape->getCenter(), args->shape->getBoundingRadius())) {
if (!cube.expandedContains(args->shape->getTranslation(), args->shape->getBoundingRadius())) {
return false;
}
if (!element->isLeaf()) {

View file

@ -40,12 +40,12 @@ CapsuleShape::CapsuleShape(float radius, const glm::vec3& startPoint, const glm:
/// \param[out] startPoint is the center of start cap
void CapsuleShape::getStartPoint(glm::vec3& startPoint) const {
startPoint = _center - _rotation * glm::vec3(0.0f, _halfHeight, 0.0f);
startPoint = _translation - _rotation * glm::vec3(0.0f, _halfHeight, 0.0f);
}
/// \param[out] endPoint is the center of the end cap
void CapsuleShape::getEndPoint(glm::vec3& endPoint) const {
endPoint = _center + _rotation * glm::vec3(0.0f, _halfHeight, 0.0f);
endPoint = _translation + _rotation * glm::vec3(0.0f, _halfHeight, 0.0f);
}
void CapsuleShape::computeNormalizedAxis(glm::vec3& axis) const {
@ -71,7 +71,7 @@ void CapsuleShape::setRadiusAndHalfHeight(float radius, float halfHeight) {
void CapsuleShape::setEndPoints(const glm::vec3& startPoint, const glm::vec3& endPoint) {
glm::vec3 axis = endPoint - startPoint;
_center = 0.5f * (endPoint + startPoint);
_translation = 0.5f * (endPoint + startPoint);
float height = glm::length(axis);
if (height > EPSILON) {
_halfHeight = 0.5f * height;

View file

@ -14,7 +14,7 @@
// ListShapeEntry
void ListShapeEntry::updateTransform(const glm::vec3& rootPosition, const glm::quat& rootRotation) {
_shape->setCenter(rootPosition + rootRotation * _localPosition);
_shape->setTranslation(rootPosition + rootRotation * _localPosition);
_shape->setRotation(_localRotation * rootRotation);
}
@ -24,9 +24,9 @@ ListShape::~ListShape() {
clear();
}
void ListShape::setCenter(const glm::vec3& center) {
void ListShape::setTranslation(const glm::vec3& position) {
_subShapeTransformsAreDirty = true;
Shape::setCenter(center);
Shape::setTranslation(position);
}
void ListShape::setRotation(const glm::quat& rotation) {
@ -44,7 +44,7 @@ const Shape* ListShape::getSubShape(int index) const {
void ListShape::updateSubTransforms() {
if (_subShapeTransformsAreDirty) {
for (int i = 0; i < _subShapeEntries.size(); ++i) {
_subShapeEntries[i].updateTransform(_center, _rotation);
_subShapeEntries[i].updateTransform(_translation, _rotation);
}
_subShapeTransformsAreDirty = false;
}

View file

@ -42,9 +42,7 @@ public:
~ListShape();
void setCenter(const glm::vec3& center);
// void setPosition(const glm::vec3& position);
// glm::vec3 getPosition() const { return _position; }
void setTranslation(const glm::vec3& position);
void setRotation(const glm::quat& rotation);
const Shape* getSubShape(int index) const;

View file

@ -18,7 +18,7 @@ PlaneShape::PlaneShape(const glm::vec4& coefficients) :
Shape(Shape::PLANE_SHAPE) {
glm::vec3 normal = glm::vec3(coefficients);
_center = -normal * coefficients.w;
_translation = -normal * coefficients.w;
float angle = acosf(glm::dot(normal, UNROTATED_NORMAL));
if (angle > EPSILON) {
@ -36,7 +36,7 @@ glm::vec3 PlaneShape::getNormal() const {
glm::vec4 PlaneShape::getCoefficients() const {
glm::vec3 normal = _rotation * UNROTATED_NORMAL;
return glm::vec4(normal.x, normal.y, normal.z, -glm::dot(normal, _center));
return glm::vec4(normal.x, normal.y, normal.z, -glm::dot(normal, _translation));
}
bool PlaneShape::findRayIntersection(const glm::vec3& rayStart, const glm::vec3& rayDirection, float& distance) const {
@ -44,9 +44,9 @@ bool PlaneShape::findRayIntersection(const glm::vec3& rayStart, const glm::vec3&
float denominator = glm::dot(n, rayDirection);
if (fabsf(denominator) < EPSILON) {
// line is parallel to plane
return glm::dot(_center - rayStart, n) < EPSILON;
return glm::dot(_translation - rayStart, n) < EPSILON;
} else {
float d = glm::dot(_center - rayStart, n) / denominator;
float d = glm::dot(_translation - rayStart, n) / denominator;
if (d > 0.0f) {
// ray points toward plane
distance = d;

View file

@ -76,7 +76,7 @@ void DistanceConstraint::updateProxyShape(Shape* shape, const glm::quat& rotatio
case Shape::SPHERE_SHAPE: {
// sphere collides at endPoint
SphereShape* sphere = static_cast<SphereShape*>(shape);
sphere->setCenter(translation + rotation * (*_points[1]));
sphere->setTranslation(translation + rotation * (*_points[1]));
}
break;
case Shape::CAPSULE_SHAPE: {

View file

@ -27,7 +27,7 @@ public:
LIST_SHAPE
};
Shape() : _type(UNKNOWN_SHAPE), _owningEntity(NULL), _boundingRadius(0.f), _center(0.f), _rotation() { }
Shape() : _type(UNKNOWN_SHAPE), _owningEntity(NULL), _boundingRadius(0.f), _translation(0.f), _rotation() { }
virtual ~Shape() {}
int getType() const { return _type; }
@ -35,33 +35,33 @@ public:
// const glm::vec3& getPosition() const { return _position; }
const glm::quat& getRotation() const { return _rotation; }
// virtual void setPosition(const glm::vec3& position) { _center = position; }
// virtual void setPosition(const glm::vec3& position) { _translation = position; }
virtual void setRotation(const glm::quat& rotation) { _rotation = rotation; }
void setEntity(PhysicalEntity* entity) { _owningEntity = entity; }
PhysicalEntity* getEntity() const { return _owningEntity; }
virtual void setCenter(const glm::vec3& center) { _center = center; }
virtual glm::vec3 getCenter() const { return _center; }
virtual void setTranslation(const glm::vec3& center) { _translation = center; }
virtual glm::vec3 getTranslation() const { return _translation; }
virtual bool findRayIntersection(const glm::vec3& rayStart, const glm::vec3& rayDirection, float& distance) const = 0;
protected:
// these ctors are protected (used by derived classes only)
Shape(Type type) : _type(type), _owningEntity(NULL), _boundingRadius(0.f), _center(0.f), _rotation() {}
Shape(Type type) : _type(type), _owningEntity(NULL), _boundingRadius(0.f), _translation(0.f), _rotation() {}
Shape(Type type, const glm::vec3& position)
: _type(type), _owningEntity(NULL), _boundingRadius(0.f), _center(position), _rotation() {}
: _type(type), _owningEntity(NULL), _boundingRadius(0.f), _translation(position), _rotation() {}
Shape(Type type, const glm::vec3& position, const glm::quat& rotation)
: _type(type), _owningEntity(NULL), _boundingRadius(0.f), _center(position), _rotation(rotation) {}
: _type(type), _owningEntity(NULL), _boundingRadius(0.f), _translation(position), _rotation(rotation) {}
void setBoundingRadius(float radius) { _boundingRadius = radius; }
int _type;
PhysicalEntity* _owningEntity;
float _boundingRadius;
glm::vec3 _center;
glm::vec3 _translation;
glm::quat _rotation;
};

View file

@ -157,7 +157,7 @@ bool collideShapeWithAACube(const Shape* shapeA, const glm::vec3& cubeCenter, fl
}
bool sphereSphere(const SphereShape* sphereA, const SphereShape* sphereB, CollisionList& collisions) {
glm::vec3 BA = sphereB->getCenter() - sphereA->getCenter();
glm::vec3 BA = sphereB->getTranslation() - sphereA->getTranslation();
float distanceSquared = glm::dot(BA, BA);
float totalRadius = sphereA->getRadius() + sphereB->getRadius();
if (distanceSquared < totalRadius * totalRadius) {
@ -175,7 +175,7 @@ bool sphereSphere(const SphereShape* sphereA, const SphereShape* sphereB, Collis
if (collision) {
collision->_penetration = BA * (totalRadius - distance);
// contactPoint is on surface of A
collision->_contactPoint = sphereA->getCenter() + sphereA->getRadius() * BA;
collision->_contactPoint = sphereA->getTranslation() + sphereA->getRadius() * BA;
collision->_shapeA = sphereA;
collision->_shapeB = sphereB;
return true;
@ -186,7 +186,7 @@ bool sphereSphere(const SphereShape* sphereA, const SphereShape* sphereB, Collis
bool sphereCapsule(const SphereShape* sphereA, const CapsuleShape* capsuleB, CollisionList& collisions) {
// find sphereA's closest approach to axis of capsuleB
glm::vec3 BA = capsuleB->getCenter() - sphereA->getCenter();
glm::vec3 BA = capsuleB->getTranslation() - sphereA->getTranslation();
glm::vec3 capsuleAxis;
capsuleB->computeNormalizedAxis(capsuleAxis);
float axialDistance = - glm::dot(BA, capsuleAxis);
@ -221,7 +221,7 @@ bool sphereCapsule(const SphereShape* sphereA, const CapsuleShape* capsuleB, Col
// penetration points from A into B
collision->_penetration = (totalRadius - radialDistance) * radialAxis; // points from A into B
// contactPoint is on surface of sphereA
collision->_contactPoint = sphereA->getCenter() + sphereA->getRadius() * radialAxis;
collision->_contactPoint = sphereA->getTranslation() + sphereA->getRadius() * radialAxis;
collision->_shapeA = sphereA;
collision->_shapeB = capsuleB;
} else {
@ -244,7 +244,7 @@ bool sphereCapsule(const SphereShape* sphereA, const CapsuleShape* capsuleB, Col
float sign = (axialDistance > 0.0f) ? -1.0f : 1.0f;
collision->_penetration = (sign * (totalRadius + capsuleB->getHalfHeight() - absAxialDistance)) * capsuleAxis;
// contactPoint is on surface of sphereA
collision->_contactPoint = sphereA->getCenter() + (sign * sphereA->getRadius()) * capsuleAxis;
collision->_contactPoint = sphereA->getTranslation() + (sign * sphereA->getRadius()) * capsuleAxis;
collision->_shapeA = sphereA;
collision->_shapeB = capsuleB;
}
@ -255,13 +255,13 @@ bool sphereCapsule(const SphereShape* sphereA, const CapsuleShape* capsuleB, Col
bool spherePlane(const SphereShape* sphereA, const PlaneShape* planeB, CollisionList& collisions) {
glm::vec3 penetration;
if (findSpherePlanePenetration(sphereA->getCenter(), sphereA->getRadius(), planeB->getCoefficients(), penetration)) {
if (findSpherePlanePenetration(sphereA->getTranslation(), sphereA->getRadius(), planeB->getCoefficients(), penetration)) {
CollisionInfo* collision = collisions.getNewCollision();
if (!collision) {
return false; // collision list is full
}
collision->_penetration = penetration;
collision->_contactPoint = sphereA->getCenter() + sphereA->getRadius() * glm::normalize(penetration);
collision->_contactPoint = sphereA->getTranslation() + sphereA->getRadius() * glm::normalize(penetration);
collision->_shapeA = sphereA;
collision->_shapeB = planeB;
return true;
@ -271,7 +271,7 @@ bool spherePlane(const SphereShape* sphereA, const PlaneShape* planeB, Collision
bool capsuleSphere(const CapsuleShape* capsuleA, const SphereShape* sphereB, CollisionList& collisions) {
// find sphereB's closest approach to axis of capsuleA
glm::vec3 AB = capsuleA->getCenter() - sphereB->getCenter();
glm::vec3 AB = capsuleA->getTranslation() - sphereB->getTranslation();
glm::vec3 capsuleAxis;
capsuleA->computeNormalizedAxis(capsuleAxis);
float axialDistance = - glm::dot(AB, capsuleAxis);
@ -287,14 +287,14 @@ bool capsuleSphere(const CapsuleShape* capsuleA, const SphereShape* sphereB, Col
}
// closestApproach = point on capsuleA's axis that is closest to sphereB's center
glm::vec3 closestApproach = capsuleA->getCenter() + axialDistance * capsuleAxis;
glm::vec3 closestApproach = capsuleA->getTranslation() + axialDistance * capsuleAxis;
if (absAxialDistance > capsuleA->getHalfHeight()) {
// sphere hits capsule on a cap
// --> recompute radialAxis and closestApproach
float sign = (axialDistance > 0.0f) ? 1.0f : -1.0f;
closestApproach = capsuleA->getCenter() + (sign * capsuleA->getHalfHeight()) * capsuleAxis;
radialAxis = closestApproach - sphereB->getCenter();
closestApproach = capsuleA->getTranslation() + (sign * capsuleA->getHalfHeight()) * capsuleAxis;
radialAxis = closestApproach - sphereB->getTranslation();
radialDistance2 = glm::length2(radialAxis);
if (radialDistance2 > totalRadius2) {
return false;
@ -349,8 +349,8 @@ bool capsuleCapsule(const CapsuleShape* capsuleA, const CapsuleShape* capsuleB,
capsuleA->computeNormalizedAxis(axisA);
glm::vec3 axisB;
capsuleB->computeNormalizedAxis(axisB);
glm::vec3 centerA = capsuleA->getCenter();
glm::vec3 centerB = capsuleB->getCenter();
glm::vec3 centerA = capsuleA->getTranslation();
glm::vec3 centerB = capsuleB->getTranslation();
// NOTE: The formula for closest approach between two lines is:
// d = [(B - A) . (a - (a.b)b)] / (1 - (a.b)^2)
@ -505,13 +505,13 @@ bool capsulePlane(const CapsuleShape* capsuleA, const PlaneShape* planeB, Collis
bool planeSphere(const PlaneShape* planeA, const SphereShape* sphereB, CollisionList& collisions) {
glm::vec3 penetration;
if (findSpherePlanePenetration(sphereB->getCenter(), sphereB->getRadius(), planeA->getCoefficients(), penetration)) {
if (findSpherePlanePenetration(sphereB->getTranslation(), sphereB->getRadius(), planeA->getCoefficients(), penetration)) {
CollisionInfo* collision = collisions.getNewCollision();
if (!collision) {
return false; // collision list is full
}
collision->_penetration = -penetration;
collision->_contactPoint = sphereB->getCenter() +
collision->_contactPoint = sphereB->getTranslation() +
(sphereB->getRadius() / glm::length(penetration) - 1.0f) * penetration;
collision->_shapeA = planeA;
collision->_shapeB = sphereB;
@ -803,21 +803,21 @@ bool sphereAACube_StarkAngles(const glm::vec3& sphereCenter, float sphereRadius,
*/
bool sphereAACube(const SphereShape* sphereA, const glm::vec3& cubeCenter, float cubeSide, CollisionList& collisions) {
return sphereAACube(sphereA->getCenter(), sphereA->getRadius(), cubeCenter, cubeSide, collisions);
return sphereAACube(sphereA->getTranslation(), sphereA->getRadius(), cubeCenter, cubeSide, collisions);
}
bool capsuleAACube(const CapsuleShape* capsuleA, const glm::vec3& cubeCenter, float cubeSide, CollisionList& collisions) {
// find nerest approach of capsule line segment to cube
glm::vec3 capsuleAxis;
capsuleA->computeNormalizedAxis(capsuleAxis);
float offset = glm::dot(cubeCenter - capsuleA->getCenter(), capsuleAxis);
float offset = glm::dot(cubeCenter - capsuleA->getTranslation(), capsuleAxis);
float halfHeight = capsuleA->getHalfHeight();
if (offset > halfHeight) {
offset = halfHeight;
} else if (offset < -halfHeight) {
offset = -halfHeight;
}
glm::vec3 nearestApproach = capsuleA->getCenter() + offset * capsuleAxis;
glm::vec3 nearestApproach = capsuleA->getTranslation() + offset * capsuleAxis;
// collide nearest approach like a sphere at that point
return sphereAACube(nearestApproach, capsuleA->getRadius(), cubeCenter, cubeSide, collisions);
}

View file

@ -17,14 +17,14 @@ bool SphereShape::findRayIntersection(const glm::vec3& rayStart, const glm::vec3
float r2 = _boundingRadius * _boundingRadius;
// compute closest approach (CA)
float a = glm::dot(_center - rayStart, rayDirection); // a = distance from ray-start to CA
float b2 = glm::distance2(_center, rayStart + a * rayDirection); // b2 = squared distance from sphere-center to CA
float a = glm::dot(_translation - rayStart, rayDirection); // a = distance from ray-start to CA
float b2 = glm::distance2(_translation, rayStart + a * rayDirection); // b2 = squared distance from sphere-center to CA
if (b2 > r2) {
// ray does not hit sphere
return false;
}
float c = sqrtf(r2 - b2); // c = distance from CA to sphere surface along rayDirection
float d2 = glm::distance2(rayStart, _center); // d2 = squared distance from sphere-center to ray-start
float d2 = glm::distance2(rayStart, _translation); // d2 = squared distance from sphere-center to ray-start
if (a < 0.0f) {
// ray points away from sphere-center
if (d2 > r2) {

View file

@ -22,11 +22,11 @@ VerletSphereShape::VerletSphereShape(float radius, glm::vec3* centerPoint) : Sph
}
// virtual from Shape class
void VerletSphereShape::setCenter(const glm::vec3& center) {
*_point = center;
void VerletSphereShape::setTranslation(const glm::vec3& position) {
*_point = position;
}
// virtual from Shape class
glm::vec3 VerletSphereShape::getCenter() {
glm::vec3 VerletSphereShape::getTranslation() {
return *_point;
}

View file

@ -23,8 +23,8 @@ public:
VerletSphereShape(float radius, glm::vec3* centerPoint);
void setCenter(const glm::vec3& center);
glm::vec3 getCenter();
void setTranslation(const glm::vec3& position);
glm::vec3 getTranslation();
protected:
// NOTE: VerletSphereShape does NOT own its _point

View file

@ -123,8 +123,8 @@ void ShapeColliderTests::sphereTouchesSphere() {
}
// contactPoint is on surface of sphereA
glm::vec3 AtoB = sphereB.getCenter() - sphereA.getCenter();
glm::vec3 expectedContactPoint = sphereA.getCenter() + radiusA * glm::normalize(AtoB);
glm::vec3 AtoB = sphereB.getTranslation() - sphereA.getTranslation();
glm::vec3 expectedContactPoint = sphereA.getTranslation() + radiusA * glm::normalize(AtoB);
inaccuracy = glm::length(collision->_contactPoint - expectedContactPoint);
if (fabs(inaccuracy) > EPSILON) {
std::cout << __FILE__ << ":" << __LINE__
@ -153,8 +153,8 @@ void ShapeColliderTests::sphereTouchesSphere() {
}
// contactPoint is on surface of sphereA
glm::vec3 BtoA = sphereA.getCenter() - sphereB.getCenter();
glm::vec3 expectedContactPoint = sphereB.getCenter() + radiusB * glm::normalize(BtoA);
glm::vec3 BtoA = sphereA.getTranslation() - sphereB.getTranslation();
glm::vec3 expectedContactPoint = sphereB.getTranslation() + radiusB * glm::normalize(BtoA);
inaccuracy = glm::length(collision->_contactPoint - expectedContactPoint);
if (fabs(inaccuracy) > EPSILON) {
std::cout << __FILE__ << ":" << __LINE__
@ -182,7 +182,7 @@ void ShapeColliderTests::sphereMissesCapsule() {
glm::quat rotation = glm::angleAxis(angle, axis);
glm::vec3 translation(15.1f, -27.1f, -38.6f);
capsuleB.setRotation(rotation);
capsuleB.setCenter(translation);
capsuleB.setTranslation(translation);
CollisionList collisions(16);
@ -193,7 +193,7 @@ void ShapeColliderTests::sphereMissesCapsule() {
for (int i = 0; i < numberOfSteps; ++i) {
// translate sphereA into world-frame
glm::vec3 localPosition = localStartPosition + ((float)i * delta) * yAxis;
sphereA.setCenter(rotation * localPosition + translation);
sphereA.setTranslation(rotation * localPosition + translation);
// sphereA agains capsuleB
if (ShapeCollider::collideShapes(&sphereA, &capsuleB, collisions))
@ -236,7 +236,7 @@ void ShapeColliderTests::sphereTouchesCapsule() {
int numCollisions = 0;
{ // sphereA collides with capsuleB's cylindrical wall
sphereA.setCenter(radialOffset * xAxis);
sphereA.setTranslation(radialOffset * xAxis);
if (!ShapeCollider::collideShapes(&sphereA, &capsuleB, collisions))
{
@ -258,7 +258,7 @@ void ShapeColliderTests::sphereTouchesCapsule() {
}
// contactPoint is on surface of sphereA
glm::vec3 expectedContactPoint = sphereA.getCenter() - radiusA * xAxis;
glm::vec3 expectedContactPoint = sphereA.getTranslation() - radiusA * xAxis;
inaccuracy = glm::length(collision->_contactPoint - expectedContactPoint);
if (fabs(inaccuracy) > EPSILON) {
std::cout << __FILE__ << ":" << __LINE__
@ -287,8 +287,8 @@ void ShapeColliderTests::sphereTouchesCapsule() {
}
// contactPoint is on surface of capsuleB
glm::vec3 BtoA = sphereA.getCenter() - capsuleB.getCenter();
glm::vec3 closestApproach = capsuleB.getCenter() + glm::dot(BtoA, yAxis) * yAxis;
glm::vec3 BtoA = sphereA.getTranslation() - capsuleB.getTranslation();
glm::vec3 closestApproach = capsuleB.getTranslation() + glm::dot(BtoA, yAxis) * yAxis;
expectedContactPoint = closestApproach + radiusB * glm::normalize(BtoA - closestApproach);
inaccuracy = glm::length(collision->_contactPoint - expectedContactPoint);
if (fabs(inaccuracy) > EPSILON) {
@ -299,7 +299,7 @@ void ShapeColliderTests::sphereTouchesCapsule() {
}
{ // sphereA hits end cap at axis
glm::vec3 axialOffset = (halfHeightB + alpha * radiusA + beta * radiusB) * yAxis;
sphereA.setCenter(axialOffset * yAxis);
sphereA.setTranslation(axialOffset * yAxis);
if (!ShapeCollider::collideShapes(&sphereA, &capsuleB, collisions))
{
@ -321,7 +321,7 @@ void ShapeColliderTests::sphereTouchesCapsule() {
}
// contactPoint is on surface of sphereA
glm::vec3 expectedContactPoint = sphereA.getCenter() - radiusA * yAxis;
glm::vec3 expectedContactPoint = sphereA.getTranslation() - radiusA * yAxis;
inaccuracy = glm::length(collision->_contactPoint - expectedContactPoint);
if (fabs(inaccuracy) > EPSILON) {
std::cout << __FILE__ << ":" << __LINE__
@ -362,7 +362,7 @@ void ShapeColliderTests::sphereTouchesCapsule() {
}
{ // sphereA hits start cap at axis
glm::vec3 axialOffset = - (halfHeightB + alpha * radiusA + beta * radiusB) * yAxis;
sphereA.setCenter(axialOffset * yAxis);
sphereA.setTranslation(axialOffset * yAxis);
if (!ShapeCollider::collideShapes(&sphereA, &capsuleB, collisions))
{
@ -384,7 +384,7 @@ void ShapeColliderTests::sphereTouchesCapsule() {
}
// contactPoint is on surface of sphereA
glm::vec3 expectedContactPoint = sphereA.getCenter() + radiusA * yAxis;
glm::vec3 expectedContactPoint = sphereA.getTranslation() + radiusA * yAxis;
inaccuracy = glm::length(collision->_contactPoint - expectedContactPoint);
if (fabs(inaccuracy) > EPSILON) {
std::cout << __FILE__ << ":" << __LINE__
@ -446,7 +446,7 @@ void ShapeColliderTests::capsuleMissesCapsule() {
CollisionList collisions(16);
// side by side
capsuleB.setCenter((1.01f * totalRadius) * xAxis);
capsuleB.setTranslation((1.01f * totalRadius) * xAxis);
if (ShapeCollider::collideShapes(&capsuleA, &capsuleB, collisions))
{
std::cout << __FILE__ << ":" << __LINE__
@ -461,7 +461,7 @@ void ShapeColliderTests::capsuleMissesCapsule() {
}
// end to end
capsuleB.setCenter((1.01f * totalHalfLength) * xAxis);
capsuleB.setTranslation((1.01f * totalHalfLength) * xAxis);
if (ShapeCollider::collideShapes(&capsuleA, &capsuleB, collisions))
{
std::cout << __FILE__ << ":" << __LINE__
@ -478,7 +478,7 @@ void ShapeColliderTests::capsuleMissesCapsule() {
// rotate B and move it to the side
glm::quat rotation = glm::angleAxis(PI_OVER_TWO, zAxis);
capsuleB.setRotation(rotation);
capsuleB.setCenter((1.01f * (totalRadius + capsuleB.getHalfHeight())) * xAxis);
capsuleB.setTranslation((1.01f * (totalRadius + capsuleB.getHalfHeight())) * xAxis);
if (ShapeCollider::collideShapes(&capsuleA, &capsuleB, collisions))
{
std::cout << __FILE__ << ":" << __LINE__
@ -516,7 +516,7 @@ void ShapeColliderTests::capsuleTouchesCapsule() {
int numCollisions = 0;
{ // side by side
capsuleB.setCenter((0.99f * totalRadius) * xAxis);
capsuleB.setTranslation((0.99f * totalRadius) * xAxis);
if (!ShapeCollider::collideShapes(&capsuleA, &capsuleB, collisions))
{
std::cout << __FILE__ << ":" << __LINE__
@ -536,7 +536,7 @@ void ShapeColliderTests::capsuleTouchesCapsule() {
}
{ // end to end
capsuleB.setCenter((0.99f * totalHalfLength) * yAxis);
capsuleB.setTranslation((0.99f * totalHalfLength) * yAxis);
if (!ShapeCollider::collideShapes(&capsuleA, &capsuleB, collisions))
{
@ -559,7 +559,7 @@ void ShapeColliderTests::capsuleTouchesCapsule() {
{ // rotate B and move it to the side
glm::quat rotation = glm::angleAxis(PI_OVER_TWO, zAxis);
capsuleB.setRotation(rotation);
capsuleB.setCenter((0.99f * (totalRadius + capsuleB.getHalfHeight())) * xAxis);
capsuleB.setTranslation((0.99f * (totalRadius + capsuleB.getHalfHeight())) * xAxis);
if (!ShapeCollider::collideShapes(&capsuleA, &capsuleB, collisions))
{
@ -584,7 +584,7 @@ void ShapeColliderTests::capsuleTouchesCapsule() {
glm::quat rotation = glm::angleAxis(PI_OVER_TWO, zAxis);
capsuleB.setRotation(rotation);
glm::vec3 positionB = ((totalRadius + capsuleB.getHalfHeight()) - overlap) * xAxis;
capsuleB.setCenter(positionB);
capsuleB.setTranslation(positionB);
// capsuleA vs capsuleB
if (!ShapeCollider::collideShapes(&capsuleA, &capsuleB, collisions))
@ -605,7 +605,7 @@ void ShapeColliderTests::capsuleTouchesCapsule() {
<< " actual = " << collision->_penetration;
}
glm::vec3 expectedContactPoint = capsuleA.getCenter() + radiusA * xAxis;
glm::vec3 expectedContactPoint = capsuleA.getTranslation() + radiusA * xAxis;
inaccuracy = glm::length(collision->_contactPoint - expectedContactPoint);
if (fabs(inaccuracy) > EPSILON) {
std::cout << __FILE__ << ":" << __LINE__
@ -633,7 +633,7 @@ void ShapeColliderTests::capsuleTouchesCapsule() {
<< std::endl;
}
expectedContactPoint = capsuleB.getCenter() - (radiusB + halfHeightB) * xAxis;
expectedContactPoint = capsuleB.getTranslation() - (radiusB + halfHeightB) * xAxis;
inaccuracy = glm::length(collision->_contactPoint - expectedContactPoint);
if (fabs(inaccuracy) > EPSILON) {
std::cout << __FILE__ << ":" << __LINE__
@ -649,7 +649,7 @@ void ShapeColliderTests::capsuleTouchesCapsule() {
glm::quat rotation = glm::angleAxis(PI_OVER_TWO, zAxis);
capsuleB.setRotation(rotation);
glm::vec3 positionB = (totalRadius - overlap) * zAxis + shift * yAxis;
capsuleB.setCenter(positionB);
capsuleB.setTranslation(positionB);
// capsuleA vs capsuleB
if (!ShapeCollider::collideShapes(&capsuleA, &capsuleB, collisions))
@ -671,7 +671,7 @@ void ShapeColliderTests::capsuleTouchesCapsule() {
<< std::endl;
}
glm::vec3 expectedContactPoint = capsuleA.getCenter() + radiusA * zAxis + shift * yAxis;
glm::vec3 expectedContactPoint = capsuleA.getTranslation() + radiusA * zAxis + shift * yAxis;
inaccuracy = glm::length(collision->_contactPoint - expectedContactPoint);
if (fabs(inaccuracy) > EPSILON) {
std::cout << __FILE__ << ":" << __LINE__
@ -708,7 +708,7 @@ void ShapeColliderTests::sphereTouchesAACubeFaces() {
float overlap = 0.25f;
float sphereOffset = 0.5f * cubeSide + sphereRadius - overlap;
sphereCenter = cubeCenter + sphereOffset * axis;
sphere.setCenter(sphereCenter);
sphere.setTranslation(sphereCenter);
if (!ShapeCollider::sphereAACube(&sphere, cubeCenter, cubeSide, collisions)){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR: sphere should collide with cube. axis = " << axis << std::endl;
@ -741,7 +741,7 @@ void ShapeColliderTests::sphereTouchesAACubeFaces() {
float overlap = 1.25f * sphereRadius;
float sphereOffset = 0.5f * cubeSide + sphereRadius - overlap;
sphereCenter = cubeCenter + sphereOffset * axis;
sphere.setCenter(sphereCenter);
sphere.setTranslation(sphereCenter);
if (!ShapeCollider::sphereAACube(&sphere, cubeCenter, cubeSide, collisions)){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR: sphere should collide with cube."
@ -815,7 +815,7 @@ void ShapeColliderTests::sphereTouchesAACubeEdges() {
float overlap = 0.25f;
sphereCenter = cubeCenter + (lengthAxis * 0.5f * cubeSide + sphereRadius - overlap) * axis;
sphere.setCenter(sphereCenter);
sphere.setTranslation(sphereCenter);
if (!ShapeCollider::sphereAACube(&sphere, cubeCenter, cubeSide, collisions)){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR: sphere should collide with cube. axis = " << axis << std::endl;
@ -857,42 +857,42 @@ void ShapeColliderTests::sphereMissesAACube() {
// top
sphereCenter = cubeCenter + sphereOffset * yAxis;
sphere.setCenter(sphereCenter);
sphere.setTranslation(sphereCenter);
if (ShapeCollider::sphereAACube(&sphere, cubeCenter, cubeSide, collisions)){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR: sphere should NOT collide with cube" << std::endl;
}
// bottom
sphereCenter = cubeCenter - sphereOffset * yAxis;
sphere.setCenter(sphereCenter);
sphere.setTranslation(sphereCenter);
if (ShapeCollider::sphereAACube(&sphere, cubeCenter, cubeSide, collisions)){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR: sphere should NOT collide with cube" << std::endl;
}
// left
sphereCenter = cubeCenter + sphereOffset * xAxis;
sphere.setCenter(sphereCenter);
sphere.setTranslation(sphereCenter);
if (ShapeCollider::sphereAACube(&sphere, cubeCenter, cubeSide, collisions)){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR: sphere should NOT collide with cube" << std::endl;
}
// right
sphereCenter = cubeCenter - sphereOffset * xAxis;
sphere.setCenter(sphereCenter);
sphere.setTranslation(sphereCenter);
if (ShapeCollider::sphereAACube(&sphere, cubeCenter, cubeSide, collisions)){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR: sphere should NOT collide with cube" << std::endl;
}
// forward
sphereCenter = cubeCenter + sphereOffset * zAxis;
sphere.setCenter(sphereCenter);
sphere.setTranslation(sphereCenter);
if (ShapeCollider::sphereAACube(&sphere, cubeCenter, cubeSide, collisions)){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR: sphere should NOT collide with cube" << std::endl;
}
// back
sphereCenter = cubeCenter - sphereOffset * zAxis;
sphere.setCenter(sphereCenter);
sphere.setTranslation(sphereCenter);
if (ShapeCollider::sphereAACube(&sphere, cubeCenter, cubeSide, collisions)){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR: sphere should NOT collide with cube" << std::endl;
}
@ -955,7 +955,7 @@ void ShapeColliderTests::rayHitsSphere() {
rayDirection = rotation * unrotatedRayDirection;
sphere.setRadius(radius);
sphere.setCenter(rotation * translation);
sphere.setTranslation(rotation * translation);
float distance = FLT_MAX;
if (!sphere.findRayIntersection(rayStart, rayDirection, distance)) {
@ -994,7 +994,7 @@ void ShapeColliderTests::rayBarelyHitsSphere() {
rayStart = rotation * (rayStart + translation);
rayDirection = rotation * rayDirection;
sphere.setCenter(rotation * translation);
sphere.setTranslation(rotation * translation);
// ...and test again
distance = FLT_MAX;
@ -1032,7 +1032,7 @@ void ShapeColliderTests::rayBarelyMissesSphere() {
rayStart = rotation * (rayStart + translation);
rayDirection = rotation * rayDirection;
sphere.setCenter(rotation * translation);
sphere.setTranslation(rotation * translation);
// ...and test again
distance = FLT_MAX;
@ -1186,7 +1186,7 @@ void ShapeColliderTests::rayHitsPlane() {
float planeDistanceFromOrigin = 3.579;
glm::vec3 planePosition(0.0f, planeDistanceFromOrigin, 0.0f);
PlaneShape plane;
plane.setCenter(planePosition);
plane.setTranslation(planePosition);
// make a simple ray
float startDistance = 1.234f;
@ -1209,7 +1209,7 @@ void ShapeColliderTests::rayHitsPlane() {
glm::vec3 axis = glm::normalize( glm::vec3(-7.0f, 2.8f, 9.3f) );
glm::quat rotation = glm::angleAxis(angle, axis);
plane.setCenter(rotation * planePosition);
plane.setTranslation(rotation * planePosition);
plane.setRotation(rotation);
rayStart = rotation * rayStart;
rayDirection = rotation * rayDirection;
@ -1231,7 +1231,7 @@ void ShapeColliderTests::rayMissesPlane() {
float planeDistanceFromOrigin = 3.579;
glm::vec3 planePosition(0.0f, planeDistanceFromOrigin, 0.0f);
PlaneShape plane;
plane.setCenter(planePosition);
plane.setTranslation(planePosition);
{ // parallel rays should miss
float startDistance = 1.234f;
@ -1251,7 +1251,7 @@ void ShapeColliderTests::rayMissesPlane() {
glm::vec3 axis = glm::normalize( glm::vec3(-7.0f, 2.8f, 9.3f) );
glm::quat rotation = glm::angleAxis(angle, axis);
plane.setCenter(rotation * planePosition);
plane.setTranslation(rotation * planePosition);
plane.setRotation(rotation);
rayStart = rotation * rayStart;
rayDirection = rotation * rayDirection;
@ -1283,7 +1283,7 @@ void ShapeColliderTests::rayMissesPlane() {
glm::vec3 axis = glm::normalize( glm::vec3(-7.0f, 2.8f, 9.3f) );
glm::quat rotation = glm::angleAxis(angle, axis);
plane.setCenter(rotation * planePosition);
plane.setTranslation(rotation * planePosition);
plane.setRotation(rotation);
rayStart = rotation * rayStart;
rayDirection = rotation * rayDirection;