From 9bbd5f0e727c2e2e4ab66642d28ef3a5752f2a7e Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Thu, 11 Jun 2009 20:03:05 +0100 Subject: [PATCH] Turn onto curved bit of points --- include/Maths.hpp | 5 +++++ maps/points.xml | 2 +- src/Maths.cpp | 14 ++++++++++++++ src/Points.cpp | 47 +++++++++++++++++++++++++++++++++++------------ 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/include/Maths.hpp b/include/Maths.hpp index 3637e1d..64c9a25 100644 --- a/include/Maths.hpp +++ b/include/Maths.hpp @@ -18,6 +18,8 @@ #ifndef INC_MATHS_HPP #define INC_MATHS_HPP +#include "Platform.hpp" + #include #include @@ -190,6 +192,9 @@ struct Frustum { Frustum getViewFrustum(); +// A rough guess at the gradient at a point on a curve +float approxGradient(function aFunc, float x); + // Useful functions for converting to/from radians template diff --git a/maps/points.xml b/maps/points.xml index 8c316e5..6c85879 100644 --- a/maps/points.xml +++ b/maps/points.xml @@ -2,7 +2,7 @@ No Name - + maps/points.bin diff --git a/src/Maths.cpp b/src/Maths.cpp index 38b1e4f..f57521c 100644 --- a/src/Maths.cpp +++ b/src/Maths.cpp @@ -44,3 +44,17 @@ void drawNormal(const Vector& aPosition, glPopAttrib(); glPopAttrib(); } + +// A rough guess at the gradient at a point on a curve +float approxGradient(function aFunc, float x) +{ + const float delta = 0.01f; + + const float x1 = x - delta; + const float x2 = x + delta; + + const float y1 = aFunc(x1); + const float y2 = aFunc(x2); + + return (y2 - y1) / (x2 - x1); +} diff --git a/src/Points.cpp b/src/Points.cpp index dd047e0..ba557fb 100644 --- a/src/Points.cpp +++ b/src/Points.cpp @@ -146,6 +146,8 @@ void Points::transform(const track::TravelToken& aToken, double aDelta) const if (myAxis == axis::Y || myAxis == -axis::Y) glRotated(-90.0, 0.0, 1.0, 0.0); + + glTranslated(-0.5, 0.0, 0.0); } else if (aToken.position == straightEndpoint()) { debug() << "Section 2"; @@ -169,44 +171,63 @@ void Points::transform(const track::TravelToken& aToken, double aDelta) const if (myAxis == axis::Y || myAxis == -axis::Y) glRotated(-90.0, 0.0, 1.0, 0.0); + + glTranslated(-0.5, 0.0, 0.0); } else if (aToken.position == displacedEndpoint()) { // Curving onto the straight section debug() << "Section 3"; - float xTrans, yTrans; + float xTrans, yTrans, rotate; + + const float fValue = 3.0f - aDelta; + const float curveValue = hypTanCurveFunc(fValue); + + // Calculate the angle that the tangent to the curve at this + // point makes to (one of) the axis at this point + const float grad = approxGradient(hypTanCurveFunc, fValue); + const float angle = radToDeg(atanf(grad)); + debug() << grad << " angle=" << angle; if (myAxis == -axis::X && aToken.direction == axis::X) { xTrans = aDelta - 2.0f; if (amReflected) - yTrans = hypTanCurveFunc(3.0f - aDelta); + yTrans = curveValue; else - yTrans = -hypTanCurveFunc(3.0f - aDelta); + yTrans = -curveValue; + + rotate = amReflected ? angle : -angle; } else if (myAxis == axis::X && aToken.direction == -axis::X) { xTrans = 3.0f - aDelta; if (amReflected) - yTrans = -hypTanCurveFunc(3.0f - aDelta); + yTrans = -curveValue; else - yTrans = hypTanCurveFunc(3.0f - aDelta); + yTrans = curveValue; + + rotate = amReflected ? -angle : angle; } - if (myAxis == -axis::Y && aToken.direction == axis::Y) { + else if (myAxis == -axis::Y && aToken.direction == axis::Y) { if (amReflected) - xTrans = -hypTanCurveFunc(3.0f - aDelta); + xTrans = -curveValue; else - xTrans = hypTanCurveFunc(3.0f - aDelta); + xTrans = curveValue; yTrans = aDelta - 2.0f; + + rotate = amReflected ? angle : -angle; } else if (myAxis == axis::Y && aToken.direction == -axis::Y) { if (amReflected) - xTrans = hypTanCurveFunc(3.0f - aDelta); + xTrans = curveValue; else - xTrans = -hypTanCurveFunc(3.0f - aDelta); + xTrans = -curveValue; yTrans = 3.0f - aDelta; + + rotate = amReflected ? angle : -angle; } else assert(false); @@ -215,11 +236,13 @@ void Points::transform(const track::TravelToken& aToken, double aDelta) const if (myAxis == axis::Y || myAxis == -axis::Y) glRotated(-90.0, 0.0, 1.0, 0.0); + + glTranslated(-0.5, 0.0, 0.0); + + glRotatef(rotate, 0.0f, 1.0f, 0.0f); } else assert(false); - - glTranslated(-0.5, 0.0, 0.0); if (aToken.direction == -axis::X || aToken.direction == -axis::Y) glRotated(-180.0, 0.0, 1.0, 0.0); -- 2.39.2