-
Notifications
You must be signed in to change notification settings - Fork 0
/
IKModule.cpp
85 lines (73 loc) · 2.1 KB
/
IKModule.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "IKModule.hpp"
#include <Graphics/Graphics.hpp>
#include <Graphics/Font.hpp>
#include <Vector/VectorGL.hpp>
namespace {
Module *create_ikmodule(const std::string ¶ms) {
return new IKModule();
}
class Fred {
public:
Fred() {
register_module("ik", create_ikmodule);
}
} fred;
}
Vector2f IKModule::size() {
return make_vector(0.5f, 1.0f);
}
void IKModule::draw(Box2f viewport, Box2f screen_viewport, float scale, unsigned int recurse) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glBoxToBox(viewport, screen_viewport);
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-0.25f, -0.5f);
glVertex2f( 0.25f, -0.5f);
glVertex2f( 0.25f, 0.5f);
glVertex2f(-0.25f, 0.5f);
glEnd();
glColor3f(1.0f, 1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
Graphics::FontRef gentium = Graphics::get_font("gentium.txf");
float len = gentium->get_length("IK", 0.3f);
glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);
gentium->draw("IK", make_vector(-0.5f * len, -0.15f), 0.3f);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(-0.25f, -0.5f);
glVertex2f( 0.25f, -0.5f);
glVertex2f( 0.25f, 0.5f);
glVertex2f(-0.25f, 0.5f);
glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
Graphics::gl_errors("IK::draw");
}
void IKModule::update(float elapsed_time) {
Vector2f target = hand().xy;
if (length(target - BasePos) > BaseLen + SegLen) {
target = BasePos + normalize(target - BasePos) * (BaseLen + SegLen);
}
{
float a = BaseLen;
float b = SegLen;
float c = length(target - BasePos);
float val = (c * c - a * a - b * b) / (-2.0f * a * b);
if (val < -1.0f) val = -1.0f;
if (val > 1.0f) val = 1.0f;
seg_ang() = float(M_PI) + acosf(val);
}
{
Vector2f tip = BasePos + make_vector(BaseLen, 0.0f) + SegLen * make_vector(cosf(seg_ang()), sinf(seg_ang()));
base_ang() = -atan2(tip.y - BasePos.y, tip.x - BasePos.x);
}
base_ang() += atan2(target.y - BasePos.y, target.x - BasePos.x);
hand_ang() = hand().z - base_ang() - seg_ang();
}
bool IKModule::handle_event(SDL_Event const &event, Vector2f local_mouse) {
return false;
}