The Plane class:
class Plane
{
public:
Plane(const Vec3 &normal, Real distance) : _normal(glm::normalize(normal)), _distance(distance) {}
Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3) {
_normal = glm::cross(p2-p1, p3-p1);
_normal = glm::normalize(_normal);
_distance = _normal.x*p1.x + _normal.y*p1.y + _normal.z*p1.z;
}
Plane(const Vec3 point, const Vec3 &normal) {
_normal = glm::normalize(normal);
_distance = _normal.x*point.x + _normal.y*point.y + _normal.z*point.z;
}
bool isFrontFacingTo(const Vec3 &direction) const {
Real dot = glm::dot(_normal, glm::normalize(direction));
return dot <= 0;
}
Real signedDistanceTo(const Vec3 &point) const {
return glm::dot(point, _normal) - _distance;
}
Real distanceTo(const Vec3 &point) const {
return std::fabs(this->signedDistanceTo(point));
}
const Vec3& normal() const { return _normal; }
Real distance() const { return _distance; }
friend ostream& operator<<(ostream& os, const Plane &plane);
bool operator==(const Plane &other) {
return (this->_normal == other._normal) && (this->_distance == other._distance);
}
private:
Vec3 _normal;
Real _distance;
};
What happens if you remove the collision response and just stop once you collide?
The collision detection routine sets colData->nearestDistance to something between [0,0.15] (0.15 is the initial velocity, set by the wasd keys) , and then my character stops at what seems to be a reasonable spot, but I'm sure that if I indeed resolve that collision, the next nearestDistance will be just a little be smaller than its predecessor, and not 0 as wanted.
Here's my "simulate" function which calls all the collision routined. It's the last piece of code I didn't share until now, and it's a bit hackish, sorry for that.
void Physics::simulate(const std::vector<glm::vec3> &vertices, const glm::vec3 &velocity)
{
std::cout << "simulating...\n";
CollisionPacket col;
col.nearestDistance = (Real)UINT_MAX;
col.velocity = velocity;
col.basePoint = cam.getPosition();
int i = 0;
do {
cout << "running for the " << ++i << " time" << endl;
col.foundCollision = false;
//find nearest collision
for(size_t i = 0; i < vertices.size(); i+=1000) //i+=100 so only the first triangle of the scene will be checked
{
_sphereTriangleCollision(&col, (Vec3)vertices[i], (Vec3)vertices[i+1], (Vec3)vertices[i+2]);
}
//solve that collision
if(col.foundCollision)
solveCollision(&col);
} while(col.foundCollision);
col.finalPosition = (Vec3)cam.getPosition() + col.velocity;
cam.setPosition((glm::vec3)col.finalPosition);
}