Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : /** 19 : * Contains classes for smooth splines 20 : * Borrowed from Game Programming Gems 4. (Slightly changed to better suit our purposes 21 : * and compatability. Any references to external material can be found there. 22 : */ 23 : 24 : #ifndef INCLUDED_NUSPLINE 25 : #define INCLUDED_NUSPLINE 26 : 27 : #define MAX_SPLINE_NODES 128 28 : 29 : #include "maths/Fixed.h" 30 : #include "maths/FixedVector3D.h" 31 : #include "maths/Vector3D.h" 32 : 33 : #include <vector> 34 : 35 : /** 36 : * Describes a node of the spline 37 : */ 38 38 : struct SplineData 39 : { 40 : // Should be fixed, because used in the simulation 41 : CFixedVector3D Position; 42 : CVector3D Velocity; 43 : // TODO: make rotation as other spline 44 : CFixedVector3D Rotation; 45 : // Time interval to the previous node, should be 0 for the first node 46 : fixed Distance; 47 : }; 48 : 49 : 50 : /** 51 : * Rounded Nonuniform Spline for describing spatial curves or paths with constant speed 52 : */ 53 7 : class RNSpline 54 : { 55 : public: 56 : 57 : RNSpline(); 58 : virtual ~RNSpline(); 59 : 60 : void AddNode(const CFixedVector3D& pos); 61 : void BuildSpline(); 62 : CVector3D GetPosition(float time) const; 63 : CVector3D GetRotation(float time) const; 64 : const std::vector<SplineData>& GetAllNodes() const; 65 : 66 5 : fixed GetMaxDistance() const { return m_MaxDistance; } 67 : 68 : protected: 69 : fixed m_MaxDistance; 70 : int m_NodeCount{0}; 71 : std::vector<SplineData> m_Nodes; 72 : CVector3D GetStartVelocity(int index); 73 : CVector3D GetEndVelocity(int index); 74 : }; 75 : 76 : 77 : /** 78 : * Smooth Nonuniform Spline for describing paths with smooth acceleration and deceleration, 79 : * but without turning 80 : */ 81 40 : class SNSpline : public RNSpline 82 : { 83 : public: 84 : virtual ~SNSpline(); 85 : 86 : void BuildSpline(); 87 : void Smooth(); 88 : }; 89 : 90 : 91 : /** 92 : * Timed Nonuniform Spline for paths with different time intervals between nodes 93 : */ 94 26 : class TNSpline : public SNSpline 95 : { 96 : public: 97 : virtual ~TNSpline(); 98 : 99 : void AddNode(const CFixedVector3D& pos, const CFixedVector3D& rotation, fixed timePeriod); 100 : void InsertNode(const int index, const CFixedVector3D& pos, const CFixedVector3D& rotation, fixed timePeriod); 101 : void RemoveNode(const int index); 102 : void UpdateNodePos(const int index, const CFixedVector3D& pos); 103 : void UpdateNodeTime(const int index, fixed time); 104 : 105 : void BuildSpline(); 106 : void Smooth(); 107 : void Constrain(); 108 : }; 109 : 110 : #endif // INCLUDED_NUSPLINE