Optimized finding nearest plane

This commit is contained in:
Matthias Schiffer 2009-12-26 14:17:28 +01:00
parent 5dc3e727e0
commit f945e21bbd

View file

@ -137,16 +137,24 @@ void Game::run(int delta) {
ok = true; ok = true;
MathUtil::Plane nearestPlane; MathUtil::Plane nearestPlane;
float nearestDistance;
for(std::vector<TriangleRecord>::iterator t = triangles.begin(); t != triangles.end(); ++t) { for(std::vector<TriangleRecord>::iterator t = triangles.begin(); t != triangles.end(); ++t) {
if(Collision::test(t->getTriangle(), playerPos, PLAYER_RADIUS, playerMove)) { if(Collision::test(t->getTriangle(), playerPos, PLAYER_RADIUS, playerMove)) {
vmml::vec3f normal = t->getTriangle().computeNormal(); 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)); MathUtil::Plane p(normal, vmml::dot(normal, t->getTriangle().getVertex(0)+PLAYER_RADIUS*normal));
if(p.isInFront(playerPos) || p.contains(playerPos)) { 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; ok = false;
nearestPlane = p; nearestPlane = p;
nearestDistance = distance;
} }
} }
} }