diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-12-26 14:17:28 +0100 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-12-26 14:17:28 +0100 |
commit | f945e21bbd225d9a2beb0f8e623bf5b9a66fc846 (patch) | |
tree | 78ec113701f971c6ccaa12189e794ec08c41b5e2 | |
parent | 5dc3e727e0fc470d344da8b18c3588104585496f (diff) | |
download | zoom++-f945e21bbd225d9a2beb0f8e623bf5b9a66fc846.tar zoom++-f945e21bbd225d9a2beb0f8e623bf5b9a66fc846.zip |
Optimized finding nearest plane
-rw-r--r-- | src/Game.cpp | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/Game.cpp b/src/Game.cpp index 5b61da7..e3aa72f 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -137,16 +137,24 @@ void Game::run(int delta) { ok = true; MathUtil::Plane nearestPlane; + float nearestDistance; 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(); + 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)) { - if(ok || p.distance(playerPos) < nearestPlane.distance(playerPos)) { + vmml::vec3f intersection = p.intersection(MathUtil::Ray(playerPos, playerMove)); + float distance = intersection.squared_distance(playerPos); + + if(ok || distance < nearestDistance) { ok = false; nearestPlane = p; + nearestDistance = distance; } } } |