aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/source/Irrlicht/CSceneNodeAnimatorFollowSpline.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/irrlicht-1.8.1/source/Irrlicht/CSceneNodeAnimatorFollowSpline.cpp')
-rw-r--r--src/others/irrlicht-1.8.1/source/Irrlicht/CSceneNodeAnimatorFollowSpline.cpp161
1 files changed, 161 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/source/Irrlicht/CSceneNodeAnimatorFollowSpline.cpp b/src/others/irrlicht-1.8.1/source/Irrlicht/CSceneNodeAnimatorFollowSpline.cpp
new file mode 100644
index 0000000..efc7493
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/source/Irrlicht/CSceneNodeAnimatorFollowSpline.cpp
@@ -0,0 +1,161 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#include "CSceneNodeAnimatorFollowSpline.h"
6
7namespace irr
8{
9namespace scene
10{
11
12
13//! constructor
14CSceneNodeAnimatorFollowSpline::CSceneNodeAnimatorFollowSpline(u32 time,
15 const core::array<core::vector3df>& points, f32 speed,
16 f32 tightness, bool loop, bool pingpong)
17: ISceneNodeAnimatorFinishing(0), Points(points), Speed(speed), Tightness(tightness), StartTime(time)
18, Loop(loop), PingPong(pingpong)
19{
20 #ifdef _DEBUG
21 setDebugName("CSceneNodeAnimatorFollowSpline");
22 #endif
23}
24
25
26inline s32 CSceneNodeAnimatorFollowSpline::clamp(s32 idx, s32 size)
27{
28 return ( idx<0 ? size+idx : ( idx>=size ? idx-size : idx ) );
29}
30
31
32//! animates a scene node
33void CSceneNodeAnimatorFollowSpline::animateNode(ISceneNode* node, u32 timeMs)
34{
35 if(!node)
36 return;
37
38 const u32 pSize = Points.size();
39 if (pSize==0)
40 {
41 if ( !Loop )
42 HasFinished = true;
43 return;
44 }
45 if (pSize==1)
46 {
47 if ( timeMs > StartTime )
48 {
49 node->setPosition(Points[0]);
50 if ( !Loop )
51 HasFinished = true;
52 }
53 return;
54 }
55
56 const f32 dt = ( (timeMs-StartTime) * Speed * 0.001f );
57 const s32 unwrappedIdx = core::floor32( dt );
58 if ( !Loop && unwrappedIdx >= (s32)pSize-1 )
59 {
60 node->setPosition(Points[pSize-1]);
61 HasFinished = true;
62 return;
63 }
64 const bool pong = PingPong && (unwrappedIdx/(pSize-1))%2;
65 const f32 u = pong ? 1.f-core::fract ( dt ) : core::fract ( dt );
66 const s32 idx = pong ? (pSize-2) - (unwrappedIdx % (pSize-1))
67 : (PingPong ? unwrappedIdx % (pSize-1)
68 : unwrappedIdx % pSize);
69 //const f32 u = 0.001f * fmodf( dt, 1000.0f );
70
71 const core::vector3df& p0 = Points[ clamp( idx - 1, pSize ) ];
72 const core::vector3df& p1 = Points[ clamp( idx + 0, pSize ) ]; // starting point
73 const core::vector3df& p2 = Points[ clamp( idx + 1, pSize ) ]; // end point
74 const core::vector3df& p3 = Points[ clamp( idx + 2, pSize ) ];
75
76 // hermite polynomials
77 const f32 h1 = 2.0f * u * u * u - 3.0f * u * u + 1.0f;
78 const f32 h2 = -2.0f * u * u * u + 3.0f * u * u;
79 const f32 h3 = u * u * u - 2.0f * u * u + u;
80 const f32 h4 = u * u * u - u * u;
81
82 // tangents
83 const core::vector3df t1 = ( p2 - p0 ) * Tightness;
84 const core::vector3df t2 = ( p3 - p1 ) * Tightness;
85
86 // interpolated point
87 node->setPosition(p1 * h1 + p2 * h2 + t1 * h3 + t2 * h4);
88}
89
90
91//! Writes attributes of the scene node animator.
92void CSceneNodeAnimatorFollowSpline::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
93{
94 out->addFloat("Speed", Speed);
95 out->addFloat("Tightness", Tightness);
96 out->addBool("Loop", Loop);
97 out->addBool("PingPong", PingPong);
98
99 u32 count = Points.size();
100
101 if ( options && (options->Flags & io::EARWF_FOR_EDITOR))
102 {
103 // add one point in addition when serializing for editors
104 // to make it easier to add points quickly
105 count += 1;
106 }
107
108 for (u32 i=0; i<count; ++i)
109 {
110 core::stringc tname = "Point";
111 tname += (int)(i+1);
112
113 out->addVector3d(tname.c_str(), i<Points.size() ? Points[i] : core::vector3df(0,0,0) );
114 }
115}
116
117
118//! Reads attributes of the scene node animator.
119void CSceneNodeAnimatorFollowSpline::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
120{
121 Speed = in->getAttributeAsFloat("Speed");
122 Tightness = in->getAttributeAsFloat("Tightness");
123 Loop = in->getAttributeAsBool("Loop");
124 PingPong = in->getAttributeAsBool("PingPong");
125 Points.clear();
126
127 for(u32 i=1; true; ++i)
128 {
129 core::stringc pname = "Point";
130 pname += i;
131
132 if (in->existsAttribute(pname.c_str()))
133 Points.push_back(in->getAttributeAsVector3d(pname.c_str()));
134 else
135 break;
136 }
137
138 // remove last point if double entry from editor
139 if ( options && (options->Flags & io::EARWF_FOR_EDITOR) &&
140 Points.size() > 2 && Points.getLast() == core::vector3df(0,0,0))
141 {
142 Points.erase(Points.size()-1);
143
144 if (Points.size() > 2 && Points.getLast() == core::vector3df(0,0,0))
145 Points.erase(Points.size()-1);
146 }
147}
148
149
150ISceneNodeAnimator* CSceneNodeAnimatorFollowSpline::createClone(ISceneNode* node, ISceneManager* newManager)
151{
152 CSceneNodeAnimatorFollowSpline * newAnimator =
153 new CSceneNodeAnimatorFollowSpline(StartTime, Points, Speed, Tightness);
154
155 return newAnimator;
156}
157
158
159} // end namespace scene
160} // end namespace irr
161