summaryrefslogtreecommitdiffstats
path: root/src/Game.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Game.cpp')
-rw-r--r--src/Game.cpp40
1 files changed, 15 insertions, 25 deletions
diff --git a/src/Game.cpp b/src/Game.cpp
index aea829f..d13a262 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -136,43 +136,32 @@ void Game::run(int delta) {
while(collision) {
collision = false;
- MathUtil::Plane nearestPlane;
float nearestDistance;
+ vmml::vec3f nearestNormal;
for(std::vector<TriangleRecord>::iterator t = triangles.begin(); t != triangles.end(); ++t) {
- if(Collision::test(t->getTriangle(), playerPos, PLAYER_RADIUS, playerMove)) {
- vmml::vec3f normal = t->getTriangle().computeNormal();
+ float distance;
+ vmml::vec3f normal;
+
+ if(Collision::test(t->getTriangle(), playerPos, PLAYER_RADIUS, playerMove, &distance, &normal)) {
+ normal.y() = 0;
+
if(normal.dot(playerMove) >= 0)
continue;
- MathUtil::Plane p(normal, vmml::dot(normal, t->getTriangle().getVertex(0)+PLAYER_RADIUS*normal));
- if(p.isInFront(playerPos) || p.contains(playerPos)) {
- vmml::vec3f intersection = p.intersection(MathUtil::Ray(playerPos, playerMove));
- float distance = intersection.squared_distance(playerPos);
-
- if(!collision || distance < nearestDistance) {
- collision = true;
-
- nearestPlane = p;
- nearestDistance = distance;
- }
- }
- else {
- // TODO Edge collision
+ if(!collision || distance < nearestDistance) {
+ collision = true;
+ nearestDistance = distance;
+ nearestNormal = normal;
}
}
}
if(collision) {
- vmml::vec3f move;
-
- if(playerMove.dot(nearestPlane.getNormal()) == 0)
- move = playerMove;
- else
- move = nearestPlane.intersection(MathUtil::Ray(playerPos, playerMove)) - playerPos;
+ vmml::vec3f move = playerMove*nearestDistance;
- if(move.dot(origMove) <= 0 && move.squared_length() > MathUtil::EPSILON) {
+ if(move.dot(origMove) <= 0 && move.squared_length() > 0) {
return;
}
@@ -182,7 +171,8 @@ void Game::run(int delta) {
playerPos += move;
vmml::vec3f restMove = playerMove - move;
- playerMove = restMove - nearestPlane.getNormal() * (nearestPlane.getNormal().dot(restMove));
+
+ playerMove = restMove - nearestNormal * (nearestNormal.dot(restMove));
}
}