-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrive.cpp
53 lines (43 loc) · 1.57 KB
/
Arrive.cpp
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
#include <vector>
#include "Arrive.hpp"
#include "Mobile.hpp"
#include "Triple.hpp"
//#define DEBUG_ARRIVE
#ifdef DEBUG_ARRIVE
#include <iostream>
#endif
Arrive::Arrive(std::string name, Mobile *character, Mobile *target, double maxSpeed, double targetRadius, double slowRadius):
DirectKinematicV(name),
character(character),
target(target),
maxSpeed(maxSpeed),
targetRadius(targetRadius),
slowRadius(slowRadius)
{}
std::vector<Triple> Arrive::getVel(unsigned int ticks, unsigned int delta_ticks) {
Triple steering;
Triple direction, targetVelocity;
double distance, targetSpeed;
Triple cp, tp;
std::tie(cp, tp) = points(this->character, this->target);
direction = tp - cp;
distance = direction.length();
direction.normalize();
if (distance < targetRadius) {
steering = target->vel;
if (steering.length() > maxSpeed) {
steering.normalize();
steering *= maxSpeed;
}
return std::vector<Triple>(1, steering);
}
// targetSpeed = maxSpeed - character->vel.dot(direction);
targetSpeed = maxSpeed;
if (distance < slowRadius) {
targetSpeed *= (distance - targetRadius) / (slowRadius - targetRadius);
}
// if (targetSpeed < 0 ) targetSpeed = 0 ;
// if (targetSpeed > maxSpeed) targetSpeed = maxSpeed;
steering = direction * targetSpeed;
return std::vector<Triple>(1, steering);
}