00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <common/SplinePath.h>
00022 #include <common/SplineCurve.h>
00023 #include <common/Defines.h>
00024 #include <math.h>
00025
00026 SplinePath::SplinePath() :
00027 pathTime_(0), pointsPerSecond_(0)
00028 {
00029 }
00030
00031 SplinePath::~SplinePath()
00032 {
00033 }
00034
00035 void SplinePath::generate(
00036 std::vector<FixedVector> &inPoints,
00037 int resolution,
00038 int polynomials,
00039 fixed pointsPerSecond)
00040 {
00041 pointsPerSecond_ = pointsPerSecond;
00042 controlPoints_ = inPoints;
00043 SplineCurve::generate(controlPoints_, pathPoints_,
00044 resolution, polynomials);
00045
00046 DIALOG_ASSERT(pointsPerSecond_ > 0);
00047 DIALOG_ASSERT(!controlPoints_.empty());
00048 DIALOG_ASSERT(!pathPoints_.empty());
00049 }
00050
00051 void SplinePath::simulate(fixed frameTime)
00052 {
00053 pathTime_ += frameTime;
00054 }
00055
00056 void SplinePath::getPathAttrs(FixedVector &position, FixedVector &direction)
00057 {
00058 fixed currentPointTime = pathTime_ * pointsPerSecond_;
00059
00060 unsigned int noPoints = pathPoints_.size();
00061 unsigned int currentPointId = (unsigned int) (currentPointTime).asInt();
00062 fixed currentPointDiff = currentPointTime - fixed(currentPointId);
00063 currentPointId = currentPointId % noPoints;
00064 unsigned int nextPointId = currentPointId + 1;
00065 nextPointId = nextPointId % noPoints;
00066 unsigned int nextNextPointId = currentPointId + 2;
00067 nextNextPointId = nextNextPointId % noPoints;
00068
00069 FixedVector ¤tPoint = pathPoints_[currentPointId];
00070 FixedVector &nextPoint = pathPoints_[nextPointId];
00071 FixedVector &nextNextPoint = pathPoints_[nextNextPointId];
00072
00073 FixedVector diff = nextPoint - currentPoint;
00074 FixedVector nextDiff = nextNextPoint - nextPoint;
00075 FixedVector diffDiff = nextDiff - diff;
00076
00077 diffDiff *= currentPointDiff;
00078 direction = diff;
00079 direction += diffDiff;
00080 direction.StoreNormalize();
00081
00082 diff *= currentPointDiff;
00083 position = currentPoint;
00084 position += diff;
00085 }
00086
00087 #ifndef S3D_SERVER
00088 #include <GLEXT/GLState.h>
00089 #endif
00090
00091 void SplinePath::draw()
00092 {
00093 #ifndef S3D_SERVER
00094 std::vector<FixedVector>::iterator itor;
00095 GLState state(GLState::TEXTURE_OFF | GLState::BLEND_OFF);
00096 glColor3f(1.0f, 0.0f, 0.0f);
00097 glBegin(GL_LINE_LOOP);
00098 for (itor = pathPoints_.begin();
00099 itor != pathPoints_.end();
00100 itor++)
00101 {
00102 Vector &pt = (*itor).asVector();
00103 glVertex3fv(pt);
00104 }
00105 glEnd();
00106
00107 glColor3f(1.0f, 1.0f, 0.0f);
00108 glPointSize(3.0f);
00109 glBegin(GL_POINTS);
00110 for (itor = controlPoints_.begin();
00111 itor != controlPoints_.end();
00112 itor++)
00113 {
00114 Vector &pt = (*itor).asVector();
00115 glVertex3fv(pt);
00116 }
00117 glColor3f(0.5f, 0.5f, 1.0f);
00118 FixedVector position, direction;
00119 getPathAttrs(position, direction);
00120 glVertex3fv(position.asVector());
00121 glEnd();
00122
00123 glPointSize(1.0f);
00124 #endif // #ifndef S3D_SERVER
00125 }