Repository navigation
Expand file tree
/
Copy pathBeam.cpp
More file actions
72 lines (69 loc) · 2.3 KB
/
Copy pathBeam.cpp
File metadata and controls
72 lines (69 loc) · 2.3 KB
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
#include "Beam.h"
#include "raymath.h"
Beam::Beam(Node* node1, Node* node2, float k, float d) {
this->n1 = node1;
this->n2 = node2;
this->stiffness = k;
this->damping = d;
this->restLength = Vector2Distance(n1->position, n2->position);
this->isBroken = false;
this->plasticDeflectionThreshold = 0.5f;
this->breakLimit = 0.7f;
}
void Beam::Update(float dt, bool simulateDeflection)
{
//will sum up calculations going along
// 1 calculation
if (isBroken) return;
// 3 calculations
Vector2 deltaPos = Vector2Subtract(n2->position, n1->position);
float currentLength = Vector2Length(deltaPos);
// 4 calculations
if (currentLength == 0) return;
// 5 calculations
float x = currentLength - this->restLength;
// 6 calculations
if (simulateDeflection)
{
// 7 calculations
float deflection = fabsf(x) / restLength;
// 8 calculations
if (deflection > plasticDeflectionThreshold)
{
// 13 calculations
float deflectionAmount = (x > 0 ? 1 : -1) * (deflection - plasticDeflectionThreshold) * restLength * 0.1f;
// 14 calculations
restLength += deflectionAmount;
}
// 15 calculations
if (deflection > breakLimit)
{
isBroken = true;
return;
}
}
// 16 calculations
float springForce = stiffness * x;
// 18 calculations
Vector2 relativeVelocity = Vector2Subtract(n2->velocity, n1->velocity);
// 26 calculations
Vector2 direction = Vector2Normalize(deltaPos);
// 29 calculations
float vRelative = Vector2DotProduct(relativeVelocity, direction);
// 30 calculations
float dampingForce = vRelative * damping;
// 31 calculations
float totalForce = springForce + dampingForce;
// 33 calculations
Vector2 forceVector = Vector2Scale(direction, totalForce);
// 35 calculations
n1->velocity.x += (forceVector.x / n1->mass) * dt;
n1->velocity.y += (forceVector.y / n1->mass) * dt;
// 37 calculations
n2->velocity.x -= (forceVector.x / n2->mass) * dt;
n2->velocity.y -= (forceVector.y / n2->mass) * dt;
}
void Beam::Draw()
{
DrawLineEx(n1->position, n2->position, 2.0f, GREEN);
}