-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspring.cpp
More file actions
31 lines (25 loc) · 1.13 KB
/
spring.cpp
File metadata and controls
31 lines (25 loc) · 1.13 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
#include "spring.hpp"
// Spring constructor
Spring::Spring(double x, double y, double width, double height, double springConstant)
: x(x), y(y), width(width), height(height), springConstant(springConstant) {}
bool isBallOnSpring(const RigidBody& ball, const Spring& spring) {
auto pos = ball.getPosition();
double r = ball.getRadius();
double ballBottom = pos[1] - r;
double springTop = spring.getY() + spring.getHeight();
return pos[0] >= spring.getX() && pos[0] <= spring.getX() + spring.getWidth() &&
ballBottom <= springTop && ballBottom >= spring.getY();
}
void applySpringImpulse(RigidBody& ball, const Spring& spring) {
auto pos = ball.getPosition();
auto vel = ball.getVelocity();
double r = ball.getRadius();
double ballBottom = pos[1] - r;
double springTop = spring.getY() + spring.getHeight();
double compression = springTop - ballBottom;
if (compression > 0 && vel[1] < 0) {
double bounceStrength = 1.0 + spring.getSpringConstant() * compression * 0.1;
ball.setVelocityY(-vel[1] * std::min(bounceStrength, 2.0));
ball.setPositionY(springTop + r);
}
}