Nebula
Loading...
Searching...
No Matches
pquatfeedbackloop.h
Go to the documentation of this file.
1#ifndef Math_PQUATFEEDBACKLOOP_H
2#define Math_PQUATFEEDBACKLOOP_H
3//------------------------------------------------------------------------------
13#include "core/types.h"
14#include "math/quaternion.h"
15#include "math/vector.h"
16#include "timing/time.h"
17
18namespace Math
19{
20using namespace Timing;
21
22//------------------------------------------------------------------------------
24{
25public:
29 void Reset(Time time, float stepSize, float gain, const quaternion& curState);
31 void SetGain(float g);
33 float GetGain() const;
35 void SetGoal(const quaternion& c);
37 const quaternion& GetGoal() const;
39 void SetState(const quaternion& s);
41 const quaternion& GetState() const;
43 void Update(Time time);
44
45private:
46 Time time; // the time at which the simulation is
47 float stepSize;
48 float gain;
49 quaternion goal;
50 quaternion state;
51};
52
53//------------------------------------------------------------------------------
56inline
57PQuatFeedbackLoop::PQuatFeedbackLoop() :
58 time(0.0),
59 stepSize(0.001f),
60 gain(-1.0f)
61{
62 // empty
63}
64
65//------------------------------------------------------------------------------
68inline
69void
70PQuatFeedbackLoop::Reset(Time t, float s, float g, const quaternion& curState)
71{
72 this->time = t;
73 this->stepSize = s;
74 this->gain = g;
75 this->state = curState;
76}
77
78//------------------------------------------------------------------------------
81inline
82void
83PQuatFeedbackLoop::SetGain(float g)
84{
85 n_assert(g > 0.0f);
86 this->gain = g;
87}
88
89//------------------------------------------------------------------------------
92inline
93float
94PQuatFeedbackLoop::GetGain() const
95{
96 return this->gain;
97}
98
99//------------------------------------------------------------------------------
102inline
103void
104PQuatFeedbackLoop::SetGoal(const quaternion& g)
105{
106 this->goal = g;
107}
108
109//------------------------------------------------------------------------------
112inline
113const quaternion&
114PQuatFeedbackLoop::GetGoal() const
115{
116 return this->goal;
117}
118
119//------------------------------------------------------------------------------
122inline
123void
124PQuatFeedbackLoop::SetState(const quaternion& s)
125{
126 this->state = s;
127}
128
129//------------------------------------------------------------------------------
132inline
133const quaternion&
134PQuatFeedbackLoop::GetState() const
135{
136 return this->state;
137}
138
139//------------------------------------------------------------------------------
142inline
143void
144PQuatFeedbackLoop::Update(Time curTime)
145{
146 Time dt = curTime - this->time;
147
148 // catch time exceptions
149 if (dt < 0.0)
150 {
151 this->time = curTime;
152 }
153 else if (dt > 0.5)
154 {
155 this->time = curTime;
156 }
157
158 while (this->time < curTime)
159 {
160 // compute angular error between state and goal
161 vector stateVec = this->state.rotate(vector(0.0f, 0.0f, 1.0f));
162 vector goalVec = this->goal.rotate(vector(0.0f, 0.0f, 1.0f));
163 float dot = stateVec % goalVec;
164
165 // dot is now between -1 (opposite vectors) and 1 (identical vectors),
166 // generate a lerp value between 0 (represents goal) and 1 (represents state)
167 float error = (-dot + 1.0f) * 0.5f;
168
169 // error now 0 for "no error" and 1 for "max error"
170 float lerp = error * this->gain * this->stepSize;
171 quaternion res;
172 res.slerp(this->state, this->goal, lerp);
173 this->state = res;
174
175 this->time += this->stepSize;
176 }
177}
178
179//------------------------------------------------------------------------------
180#endif
A specialized proportional feedback loop for rotations, using a quaternion representation.
#define n_assert(exp)
Definition debug.h:50
Different curves.
Definition angularpfeedbackloop.h:17
__forceinline scalar dot(const plane &p, const vec4 &v1)
Definition plane.h:246
__forceinline float lerp(float x, float y, float l)
Linearly interpolate between 2 values: ret = x + l * (y - x)
Definition scalar.h:597
@ Update
Definition resourceloader.h:53
struct StaticUI::@7 state
Definition calendartimebase.h:19
double Time
the time datatype
Definition time.h:18
Typedefs for the Timing subsystem.