Added gitignore
[tagua/yd.git] / src / movement.cpp
blob4b282a9bfb754a0ca04ea0e12cf0fc53e2eec3e3
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "movement.h"
12 #include "common.h"
13 #include <cmath>
15 using std::abs;
16 #define QUINTIC_SIGMOID
18 double Movement::sigmoid(double t) {
19 #ifdef CUBIC_SIGMOID
20 return -2 * pow(t - 0.5, 3) + 1.5 * t - 0.25;
21 #else
22 return 6 * pow(t - 0.5, 5) - 5 * pow(t, 3) + 7.5 * t * t - 15 * 0.125 * t + 3.0 / 16;
23 #endif
26 double Movement::half_sigmoid(double t) {
27 // use the second half of the sigmoid parameterization
28 return sigmoid(0.5 * (t + 1));
31 LinearMovement::LinearMovement(const QPoint& from, const QPoint& to, bool /*rotate*/)
32 : m_from(from)
33 , m_velocity(to - from) { }
35 QPoint LinearMovement::pos(double t) const {
36 return m_from + m_velocity * t;
39 LMovement::LMovement(const QPoint& from, const QPoint& to, bool rotate)
40 : m_from(from)
41 , m_velocity(to - from)
42 , m_rotate(rotate) {
43 if (abs(static_cast<double>(m_velocity.x())) >=
44 abs(static_cast<double>(m_velocity.y()))) {
45 m_x = LMovement::nonlinear;
46 m_y = LMovement::linear;
48 else {
49 m_y = LMovement::nonlinear;
50 m_x = LMovement::linear;
54 double LMovement::nonlinear(double t) {
55 return (exp( (double)(3 * t) ) - 1) / (exp( (double)3 ) - 1);
58 QPoint LMovement::pos(double t) const {
59 return m_from + QPoint(
60 static_cast<int>(m_velocity.x() * m_x(t)),
61 static_cast<int>(m_velocity.y() * m_y(t)));
64 double LMovement::rotation(double t) const {
65 if(!m_rotate)
66 return 0.0;
68 double r = (1.0-cos(t*M_PI))*M_PI;
69 return m_velocity.x() >= 0 ? r : -r;