-
Notifications
You must be signed in to change notification settings - Fork 0
/
LookWhereYoureGoing.cpp
69 lines (56 loc) · 2.39 KB
/
LookWhereYoureGoing.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cmath>
#include <vector>
#include "LookWhereYoureGoing.hpp"
#include "Mobile.hpp"
#include "Triple.hpp"
#include "util.hpp"
//#define DEBUG_LOOKWHEREYOUREGOING
#ifdef DEBUG_LOOKWHEREYOUREGOING
# include <iostream>
#endif
LookWhereYoureGoing::LookWhereYoureGoing(std::string name, Mobile *character, double maxAngularVelocity, double targetRadius, double slowRadius):
DirectKinematicA(name),
character(character),
maxAngularVelocity(maxAngularVelocity),
targetRadius(targetRadius),
slowRadius(slowRadius)
{}
std::vector<double> LookWhereYoureGoing::getAngVel(unsigned int ticks, unsigned int delta_ticks) {
double steering;
double rotation, rotationSize, targetRotation;
Triple direction;
direction = character->vel;
if (direction.length() == 0) {
return std::vector<double>();
}
rotation = mapToRange(mapToRange(atan2(direction.y, direction.x)) - mapToRange(character->ang));
rotationSize = abs(rotation);
if (rotationSize < targetRadius) {
#ifdef DEBUG_LOOKWHEREYOUREGOING
std::cout << "LookWhereYoureGoing " << static_cast<void *>(this) << ": dentro de targetRadius" << std::endl;
#endif
steering = character->vrot;
if (abs(steering) > maxAngularVelocity) {
steering = maxAngularVelocity;
}
return std::vector<double>(1, steering);
}
targetRotation = maxAngularVelocity;
if (rotationSize < slowRadius) {
#ifdef DEBUG_LOOKWHEREYOUREGOING
std::cout << "LookWhereYoureGoing " << static_cast<void *>(this) << ": entre targetRadius y slowRadius" << std::endl;
#endif
targetRotation *= rotationSize / slowRadius;
}
#ifdef DEBUG_LOOKWHEREYOUREGOING
else {
std::cout << "LookWhereYoureGoing " << static_cast<void *>(this) << ": fuera de slowRadius" << std::endl;
}
#endif
targetRotation *= map_atan(20000*character->vel.length());
#ifdef DEBUG_LOOKWHEREYOUREGOING
std::cout << "LookWhereYoureGoing " << static_cast<void *>(this) << ": targetRotation == " << targetRotation << " after mapping vel with factor " << map_atan(200*character->vel.length()) << std::endl;
#endif
steering = targetRotation * (rotation > 0 ? -1 : 1);
return std::vector<double>(1, steering);
}