1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
// ----------------------------------------------------------
//
// Copyright (C) 2002 Brad Wasson <bard@systemtoolbox.com>
//
// This file is part of 3ddesktop.
//
// 3ddesktop is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// 3ddesktop is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with 3ddesktop; see the file COPYING. If not, write to
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//
#ifndef _MOVE_HPP
#define _MOVE_HPP
#include <math.h> // for sin etc
#include "3ddesk.h"
class Movement {
protected:
float start_value;
float end_value;
float *current;
float step;
float start_iter;
float end_iter;
float iter;
bool inited;
float scale_factor;
float (Movement::*pos_func)(float);
// These are all a bunch of different movements to play with
// Basically this means using different functions to determine
// position which makes the "thing" being moved apear to move
// at different rates. For example linear is just a constant
// speed (y = t where y is position and t is time - take the
// derivative of this and you get velocity as a constant)
float sin_func (float t) { return (sin(t) + t); }
float x_squared_func (float t) { return (4 - t*t); }
float log_func (float t) { return (log (t)); }
float e_func (float t) { return (5 - exp(-t)); }
float linear_func (float t) { return (t); }
float circle_func (float t) { return ( sqrt(9 - t*t) ); }
float waver_func (float t) { return ( (-sin(t)/(2*t)) + 1 ); }
float plain_sine_func(float t) { return (sin(t)); }
public:
enum MOVE_TYPE {
MOVE_TYPE_WACK,
MOVE_TYPE_SMOOTH,
MOVE_TYPE_WACK2,
MOVE_TYPE_WACK3,
MOVE_TYPE_WACK4,
MOVE_TYPE_WACK5,
MOVE_TYPE_OVERSHOOT,
MOVE_TYPE_CONSTANT,
MOVE_TYPE_WAVER,
MOVE_TYPE_SINE,
MOVE_TYPE_HALF_SINE
};
MOVE_TYPE type;
Movement () { inited = false; current = NULL;}
void init (float *ref, float sv, float ev, float times, MOVE_TYPE _type) {
type = _type;
current = ref;
start_value = *current = sv;
end_value = ev;
float min = 999, max = 999;
switch (type) {
case MOVE_TYPE_WACK5:
pos_func = &Movement::sin_func;
start_iter = 2.0 * PI;
end_iter = 3.0 * PI;
break;
case MOVE_TYPE_SMOOTH:
pos_func = &Movement::sin_func;
start_iter = PI;
end_iter = 3.0 * PI;
break;
case MOVE_TYPE_WACK2:
pos_func = &Movement::x_squared_func;
start_iter = -2;
end_iter = 0;
break;
case MOVE_TYPE_WACK3:
pos_func = &Movement::log_func;
start_iter = 1;
end_iter = 30;
break;
case MOVE_TYPE_WACK4:
pos_func = &Movement::e_func;
start_iter = 0;
end_iter = 5;
break;
case MOVE_TYPE_CONSTANT:
pos_func = &Movement::linear_func;
start_iter = 0;
end_iter = 5;
break;
case MOVE_TYPE_WACK:
pos_func = &Movement::circle_func;
start_iter = -2.8;
end_iter = .1;
break;
case MOVE_TYPE_OVERSHOOT:
pos_func = &Movement::circle_func;
start_iter = -2.8;
end_iter = 1.1;
break;
case MOVE_TYPE_WAVER:
pos_func = &Movement::waver_func;
start_iter = 1;
end_iter = 39; //26.5;
break;
case MOVE_TYPE_SINE:
pos_func = &Movement::plain_sine_func;
start_iter = 0;
end_iter = 2 * PI;
max = 1;
min = -1;
break;
case MOVE_TYPE_HALF_SINE:
pos_func = &Movement::plain_sine_func;
start_iter = 0;
end_iter = PI;
max = 1;
min = 0;
break;
default:
//panic();
break;
}
if (max == 999) { // if not set use function
max = (this->*pos_func)(end_iter);
min = (this->*pos_func)(start_iter);
}
iter = start_iter;
// this is the trick to it all -- you scale the position
// function to the actual distance you are travelling. then
// it doesn't matter the range or scale of the position
// function.
scale_factor = (ev - sv) / (max - min);
step = (end_iter - start_iter) / times;
inited = true;
}
bool at_destination (void) {
if ((iter > end_iter) || !inited) {
// this enforces that we end up where we want to...
if (current)
*current = end_value;
return true;
}
#if 0
if (direction > 0) {
if (*current >= end_value) {
printf("value end:: %lf\n", *current);
*current = end_value;
return true;
}
} else {
if (*current <= end_value) {
printf("value end: %lf\n", *current);
*current = end_value;
return true;
}
}
#endif
return false;
}
float value (void) { return (current ? *current : 0.0); }
float destination (void) { return end_value; }
void disable (void) { iter = 1; end_iter = 0; current = NULL; }
void change_a_bit (void) {
if (at_destination())
return;
*current = (scale_factor * ((this->*pos_func)(iter) - (this->*pos_func)(start_iter)))
+ start_value;
iter += step;
}
};
#endif // _MOVE_HPP
|