[3018] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Benjamin Grauer |
---|
| 13 | co-programmer: ... |
---|
| 14 | |
---|
| 15 | Quaternion code borrowed from an Gamasutra article by Nick Bobick and Ken Shoemake |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | #include "curve.h" |
---|
| 19 | |
---|
[3019] | 20 | |
---|
| 21 | |
---|
[3018] | 22 | /** |
---|
[3019] | 23 | \brief adds a new Node to the bezier Curve |
---|
| 24 | \param newNode a Vector to the position of the new node |
---|
| 25 | */ |
---|
| 26 | void Curve::addNode(const Vector& newNode) |
---|
| 27 | { |
---|
| 28 | if (nodeCount != 0 ) |
---|
| 29 | { |
---|
| 30 | currentNode = currentNode->next = new PathNode; |
---|
| 31 | } |
---|
| 32 | currentNode->position = newNode; |
---|
| 33 | currentNode->next = 0; // not sure if this really points to NULL!! |
---|
| 34 | currentNode->number = (++nodeCount); |
---|
| 35 | return; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | /** |
---|
[3018] | 40 | \brief Creates a new BezierCurve |
---|
| 41 | */ |
---|
| 42 | BezierCurve::BezierCurve (void) |
---|
| 43 | { |
---|
| 44 | nodeCount = 0; |
---|
| 45 | firstNode = new PathNode; |
---|
| 46 | currentNode = firstNode; |
---|
| 47 | |
---|
| 48 | firstNode->position = Vector (.0, .0, .0); |
---|
| 49 | firstNode->number = 0; |
---|
| 50 | firstNode->next = 0; // not sure if this really points to NULL!! |
---|
| 51 | |
---|
| 52 | return; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | \brief Deletes a BezierCurve. |
---|
| 57 | It does this by freeing all the space taken over from the nodes |
---|
| 58 | */ |
---|
| 59 | BezierCurve::~BezierCurve (void) |
---|
| 60 | { |
---|
| 61 | PathNode* tmpNode; |
---|
| 62 | currentNode = firstNode; |
---|
| 63 | while (tmpNode != 0) |
---|
| 64 | { |
---|
| 65 | tmpNode = currentNode; |
---|
| 66 | currentNode = currentNode->next; |
---|
| 67 | delete tmpNode; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | \brief calculates the Position on the curve |
---|
| 73 | \param t The position on the Curve (0<=t<=1) |
---|
| 74 | \return the Position on the Path |
---|
| 75 | */ |
---|
| 76 | Vector BezierCurve::calcPos(float t) |
---|
| 77 | { |
---|
| 78 | if (nodeCount <=4) |
---|
| 79 | { |
---|
| 80 | // if (verbose >= 1) |
---|
| 81 | // printf ("Please define at least 4 nodes, until now you have only defined %i.\n", nodeCount); |
---|
| 82 | return Vector(0,0,0); |
---|
| 83 | } |
---|
| 84 | PathNode* tmpNode = firstNode; |
---|
| 85 | |
---|
| 86 | Vector ret = Vector(0.0,0.0,0.0); |
---|
| 87 | double factor; |
---|
| 88 | int k=0; |
---|
| 89 | while(tmpNode!=0) |
---|
| 90 | { |
---|
| 91 | k++; |
---|
| 92 | factor = ncr (nodeCount, k); |
---|
| 93 | |
---|
| 94 | for (int j=0; j<nodeCount-k; j++) |
---|
| 95 | factor*=(1-t); |
---|
| 96 | for (int j=0; j<k; j++) |
---|
| 97 | factor*=t; |
---|
| 98 | ret.x += factor * tmpNode->position.x; |
---|
| 99 | ret.y += factor * tmpNode->position.y; |
---|
| 100 | ret.z += factor * tmpNode->position.z; |
---|
| 101 | |
---|
| 102 | tmpNode = tmpNode->next; |
---|
| 103 | |
---|
| 104 | } |
---|
| 105 | return ret; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | Vector BezierCurve::calcDir (float t) |
---|
| 109 | { |
---|
| 110 | PathNode* tmpNode = firstNode; |
---|
| 111 | BezierCurve* tmpCurve = new BezierCurve(); |
---|
| 112 | Vector ret; |
---|
| 113 | Vector tmpVector; |
---|
| 114 | |
---|
| 115 | while (tmpNode->next != 0) |
---|
| 116 | { |
---|
| 117 | tmpVector = (tmpNode->next->position)- (tmpNode->position); |
---|
| 118 | tmpVector.x*=(float)nodeCount; |
---|
| 119 | tmpVector.y*=(float)nodeCount; |
---|
| 120 | tmpVector.z*=(float)nodeCount; |
---|
| 121 | |
---|
| 122 | tmpCurve->addNode(tmpVector); |
---|
| 123 | tmpNode = tmpNode->next; |
---|
| 124 | } |
---|
| 125 | ret = tmpCurve->calcPos(t); |
---|
| 126 | ret.normalize(); |
---|
| 127 | |
---|
| 128 | return Vector (0,0,0);//ret; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | \brief returns the Position of the point calculated on the Curve |
---|
| 133 | \return a Vector to the calculated position |
---|
| 134 | */ |
---|
| 135 | Vector BezierCurve::getPos() const |
---|
| 136 | { |
---|
| 137 | return curvePoint; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
[3019] | 141 | int ncr(int n, int i) |
---|
[3018] | 142 | { |
---|
| 143 | int ret = 1; |
---|
| 144 | for (int k=1; k<=n; k++) |
---|
| 145 | ret*=k; |
---|
| 146 | for (int k=1; k<=i; k++) |
---|
| 147 | ret/=k; |
---|
| 148 | for (int k=1; k<=n-i; k++) |
---|
| 149 | ret/=k; |
---|
| 150 | |
---|
| 151 | return ret; |
---|
| 152 | } |
---|