-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFollower.cpp
More file actions
47 lines (37 loc) · 1.14 KB
/
Follower.cpp
File metadata and controls
47 lines (37 loc) · 1.14 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
#include <math.h>
#include "Follower.h"
Follower::Follower(){}
Follower::~Follower(){}
const double SPRING_CONSTANT_SLOW = 0.01;
const double FRICTION = .99;
const double EXTRA_FRICTION = .90;
const double SPRING_LENGTH = 100;
void Follower::update()
{
GenericObject* player = ThePlayer;
int leftButtonDown = glfwGetMouseButton(window, 0);
int rightButtonDown = glfwGetMouseButton(window, 1);
double delta_y = (player->y) - y;
double delta_x = (player->x) - x;
double springConstant = leftButtonDown ? SPRING_CONSTANT_SLOW * 5 : SPRING_CONSTANT_SLOW;
double angleToTarget = atan2(delta_y, delta_x);
double equilibrium_y = SPRING_LENGTH * -sin(angleToTarget) + (player->y);
double equilibrium_x = SPRING_LENGTH * -cos(angleToTarget) + (player->x);
yAcc = springConstant * (equilibrium_y - y);
xAcc = springConstant * (equilibrium_x - x);
if (!leftButtonDown)
{
xVel *= FRICTION;
yVel *= FRICTION;
if (rightButtonDown)
{
xVel *= EXTRA_FRICTION;
yVel *= EXTRA_FRICTION;
}
}
xVel += xAcc;
yVel += yAcc;
x += xVel;
y += yVel;
angle += 1;
}