From f945e21bbd225d9a2beb0f8e623bf5b9a66fc846 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 26 Dec 2009 14:17:28 +0100 Subject: Optimized finding nearest plane --- src/Game.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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::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; } } } -- cgit v1.2.3