-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpring.cpp
More file actions
32 lines (28 loc) · 783 Bytes
/
Spring.cpp
File metadata and controls
32 lines (28 loc) · 783 Bytes
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
#include "Core.h"
#include "Node.h"
#include "Spring.h"
Spring::Spring(Node& a, Node& b)
:node_a(a), node_b(b), stiffness(2.5f), damping(0.995f)
{
baseLength = Core::distance(node_a.pos, node_b.pos);
}
void Spring::update()
{
sf::Vector2f force = node_b.pos - node_a.pos;
float mag = (Core::length(force) - baseLength) * stiffness;
Core::setMag(force, mag);
node_a.applyForce(force);
node_a.vel.x = node_a.vel.x * damping;
node_a.vel.y = node_a.vel.y * damping;
force.x = -force.x;
force.y = -force.y;
node_b.applyForce(force);
node_b.vel.x = node_b.vel.x * damping;
node_b.vel.y = node_b.vel.y * damping;
}
void Spring::draw(sf::RenderWindow& window)
{
line[0] = node_a.pos;
line[1] = node_b.pos;
window.draw(line, 2, sf::Lines);
}