Loading [MathJax]/extensions/tex2jax.js
ProtoTracer  1.0
Real-time 3D rendering and animation engine
All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros Pages
DampedSpring.cpp
Go to the documentation of this file.
1#include "DampedSpring.h"
2
4 currentVelocity(0.001f),
5 currentPosition(0.001f),
6 springConstant(0.0f),
7 springForce(0.0f),
8 dampingForce(0.0f),
9 force(0.0f),
10 damping(0.0f),
11 previousMillis(0) {
12}
13
14DampedSpring::DampedSpring(float springConstant, float damping) :
15 currentVelocity(0.001f),
16 currentPosition(0.001f),
17 springConstant(-1.0f * springConstant),
18 springForce(0.0f),
19 dampingForce(0.0f),
20 force(0.0f),
21 damping(-1.0f * damping),
22 previousMillis(0) {
23}
24
28
29void DampedSpring::SetConstants(float springConstant, float damping) {
30 this->springConstant = -1.0f * springConstant;
31 this->damping = -1.0f * damping;
32}
33
34float DampedSpring::Calculate(float target, unsigned long currentMillis) {
35 float dT = ((float)(currentMillis - previousMillis)) / 1000.0f;
36
37 if (dT > 0.01f && dT < 2.0f) {
40 force = springForce - dampingForce + target;
41
42 currentVelocity += force * dT;
44 }
45
46 previousMillis = currentMillis;
47
48 return currentPosition;
49}
50
51float DampedSpring::Calculate(float target, float dT) {
52 if (!Mathematics::IsClose(target, currentPosition, 0.01f)) {
55 force = springForce + dampingForce + target;
56
57 currentVelocity += force * dT;
59 }
60
61 return currentPosition;
62}
Defines the DampedSpring class for simulating damped spring motion.
float currentVelocity
Current velocity of the spring.
float springForce
Force exerted by the spring.
float Calculate(float target, unsigned long currentMillis)
Calculates the spring's position and velocity using a target position and timestamp.
float currentPosition
Current position of the spring.
unsigned long previousMillis
Previous timestamp for time calculations.
float springConstant
Spring constant determining stiffness.
float GetCurrentPosition()
Gets the current position of the spring.
float damping
Damping coefficient.
DampedSpring()
Default constructor initializing a damped spring with default values.
void SetConstants(float springConstant, float damping)
Sets the spring constant and damping coefficient.
float dampingForce
Damping force resisting motion.
float force
Combined force acting on the spring.
static bool IsClose(float v1, float v2, float epsilon)
Checks if two values are close within a specified epsilon.