Consolidate collider transforms into Collider

This commit is contained in:
Mishura
2024-05-06 18:27:52 -04:00
parent 64151c71e6
commit 977f8f6e66
5 changed files with 36 additions and 41 deletions

View File

@@ -18,6 +18,11 @@ public:
ColliderType type;
void draw();
void translate(const Vector3& translation);
// Pitch-yaw-roll
void rotate(const Vector3& rotation);
void scale(const Vector3& factor);
bool contains(const Position &position) const;
bool collides(const Collider& other) const;
bool collides(const Triangle& triangle) const;

View File

@@ -40,21 +40,11 @@ void Cube::update(float elapsed) {
}
Collider Cube::getCollider() {
Collider collider = Collider::calculateAABB(getGeometry()->getBoundingVolume(angle));
Collider collider = Collider::calculateAABB(getGeometry()->getBoundingVolume(angle));
//Scale.
for (auto& vertex : collider.shape.vertices) {
vertex.x *= getScale();
vertex.y *= getScale();
vertex.z *= getScale();
}
//To world space.
for (auto& vertex : collider.shape.vertices) {
vertex.x = vertex.x + position.x;
vertex.y = vertex.y + position.y;
vertex.z = vertex.z + position.z;
}
collider.rotate(angle);
collider.scale(Vector3(getScale(), getScale(), getScale()));
collider.translate(position);
return collider;
}

View File

@@ -27,19 +27,9 @@ Collider Sphere2::getCollider() {
collider.shape.indices = getGeometry()->indices;
collider.type = ColliderType::CollisionMap;
//Scale.
for (auto& vertex : collider.shape.vertices) {
vertex.x *= getScale();
vertex.y *= getScale();
vertex.z *= getScale();
}
//To world space.
for (auto& vertex : collider.shape.vertices) {
vertex.x = vertex.x + position.x;
vertex.y = vertex.y + position.y;
vertex.z = vertex.z + position.z;
}
collider.rotate(angle);
collider.scale(Vector3(getScale(), getScale(), getScale()));
collider.translate(position);
return collider;
}

View File

@@ -173,6 +173,27 @@ bool Collider::collides(const Collider& other) const {
return false;
}
void Collider::translate(const Vector3& translation) {
for (auto& vertex : shape.vertices) {
vertex += translation;
}
}
void Collider::rotate(const Vector3& rotation) {
if (type == ColliderType::AxisAlignedBoundingBox)
return; // Axis-aligned bounding box does not rotate, it is always axis-aligned
shape.rotate(rotation);
angle = rotation;
}
void Collider::scale(const Vector3& scale) {
for (auto& vertex : shape.vertices) {
vertex.x *= scale.x;
vertex.y *= scale.y;
vertex.z *= scale.z;
}
}
bool Collider::collides(const Triangle& triangle) const {
return triangleVSAxisAlignedBoundingBoxCollides(triangle, *this);
}

View File

@@ -59,20 +59,9 @@ GLfloat Entity::getScale() const {
Collider Entity::getCollider() {
//Default Collider is AABB.
Collider collider = Collider::calculateAABB(getGeometry()->getBoundingVolume(angle));
collider.angle = angle;
//Scale.
for (auto& vertex : collider.shape.vertices) {
vertex.x *= getScale();
vertex.y *= getScale();
vertex.z *= getScale();
}
//To world space.
for (auto& vertex : collider.shape.vertices) {
vertex.x = vertex.x + position.x;
vertex.y = vertex.y + position.y;
vertex.z = vertex.z + position.z;
}
collider.rotate(angle);
collider.scale(Vector3(getScale(), getScale(), getScale()));
collider.translate(position);
return collider;
}