diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-12-20 18:57:23 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-12-20 18:57:23 -0500 |
commit | 62069b31c7d23055f999c70a58ccf7d58acd333f (patch) | |
tree | 7ea4c57e74d978a297d34cf61d9c85db846e8900 /src/systems/pondering.cpp | |
parent | 90aadf3844386824140a20d7fbb847bc16009a94 (diff) | |
download | therapy-master.tar.gz therapy-master.tar.bz2 therapy-master.zip |
The acceleration of a Ponderable entity is now only really a magnitude. The direction of acceleration is such that the velocity goes toward the target velocity. If accelerating would cause the velocity to pass the target velocity, it is instead set to the target. This is currently being used to 1) generalize terminal velocity due to gravity, and 2) allow an Orientable entity to accelerate quickly to a walking speed (and to a halt) rather than instantly achieve it.
Diffstat (limited to 'src/systems/pondering.cpp')
-rw-r--r-- | src/systems/pondering.cpp | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index d841679..649a03a 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp | |||
@@ -44,6 +44,7 @@ void PonderingSystem::initializeBody( | |||
44 | 44 | ||
45 | if (type == PonderableComponent::Type::freefalling) | 45 | if (type == PonderableComponent::Type::freefalling) |
46 | { | 46 | { |
47 | ponderable.targetVel.y() = TERMINAL_VELOCITY; | ||
47 | ponderable.accel.y() = NORMAL_GRAVITY; | 48 | ponderable.accel.y() = NORMAL_GRAVITY; |
48 | } | 49 | } |
49 | } | 50 | } |
@@ -57,6 +58,8 @@ void PonderingSystem::initPrototype(id_type prototype) | |||
57 | ponderable.vel.y() = 0.0; | 58 | ponderable.vel.y() = 0.0; |
58 | ponderable.accel.x() = 0.0; | 59 | ponderable.accel.x() = 0.0; |
59 | ponderable.accel.y() = 0.0; | 60 | ponderable.accel.y() = 0.0; |
61 | ponderable.targetVel.x() = 0.0; | ||
62 | ponderable.targetVel.y() = 0.0; | ||
60 | ponderable.grounded = false; | 63 | ponderable.grounded = false; |
61 | ponderable.frozen = false; | 64 | ponderable.frozen = false; |
62 | ponderable.collidable = true; | 65 | ponderable.collidable = true; |
@@ -98,12 +101,48 @@ void PonderingSystem::tickBody( | |||
98 | // Accelerate | 101 | // Accelerate |
99 | if (!ponderable.frozen) | 102 | if (!ponderable.frozen) |
100 | { | 103 | { |
101 | ponderable.vel += ponderable.accel * dt; | 104 | // Determine the effective acceleration, which should be in the direction of |
105 | // the target velocity. | ||
106 | vec2d effAcc = ponderable.accel; | ||
102 | 107 | ||
103 | if ((ponderable.type == PonderableComponent::Type::freefalling) | 108 | if (ponderable.vel.x() == ponderable.targetVel.x()) |
104 | && (ponderable.vel.y() > TERMINAL_VELOCITY)) | ||
105 | { | 109 | { |
106 | ponderable.vel.y() = TERMINAL_VELOCITY; | 110 | effAcc.x() = 0.0; |
111 | } | ||
112 | else if ((ponderable.accel.x() > 0 && | ||
113 | ponderable.targetVel.x() < ponderable.vel.x()) || | ||
114 | (ponderable.accel.x() < 0 && | ||
115 | ponderable.targetVel.x() > ponderable.vel.x())) | ||
116 | { | ||
117 | effAcc.x() = -effAcc.x(); | ||
118 | } | ||
119 | |||
120 | if (ponderable.vel.y() == ponderable.targetVel.y()) | ||
121 | { | ||
122 | effAcc.y() = 0.0; | ||
123 | } | ||
124 | else if ((ponderable.accel.y() > 0 && | ||
125 | ponderable.targetVel.y() < ponderable.vel.y()) || | ||
126 | (ponderable.accel.y() < 0 && | ||
127 | ponderable.targetVel.y() > ponderable.vel.y())) | ||
128 | { | ||
129 | effAcc.y() = -effAcc.y(); | ||
130 | } | ||
131 | |||
132 | // Accelerate | ||
133 | ponderable.vel += effAcc * dt; | ||
134 | |||
135 | // If the velocity crossed the target velocity, set it to the target | ||
136 | if ((effAcc.x() > 0 && ponderable.vel.x() > ponderable.targetVel.x()) || | ||
137 | (effAcc.x() < 0 && ponderable.vel.x() < ponderable.targetVel.x())) | ||
138 | { | ||
139 | ponderable.vel.x() = ponderable.targetVel.x(); | ||
140 | } | ||
141 | |||
142 | if ((effAcc.y() > 0 && ponderable.vel.y() > ponderable.targetVel.y()) || | ||
143 | (effAcc.y() < 0 && ponderable.vel.y() < ponderable.targetVel.y())) | ||
144 | { | ||
145 | ponderable.vel.y() = ponderable.targetVel.y(); | ||
107 | } | 146 | } |
108 | } | 147 | } |
109 | 148 | ||