stash/unstash clunky as fuck
[laserbrain_demo] / src / avatar.cc
1 #include "avatar.h"
2 #include "logger.h"
3
4 Avatar::Avatar()
5 {
6         mode = 0;
7         body_rot = 0;
8         head_alt = 0;
9 }
10
11 void Avatar::set_tracking_mode(unsigned int mode)
12 {
13         this->mode = mode;
14 }
15
16 unsigned int Avatar::get_tracking_mode() const
17 {
18         return mode;
19 }
20
21 void Avatar::set_position(const Vec3 &p)
22 {
23         pos = p;
24 }
25
26 const Vec3 &Avatar::get_position() const
27 {
28         return pos;
29 }
30
31 void Avatar::set_body_rotation(float rot)
32 {
33         body_rot = rot;
34
35         float theta = deg_to_rad(body_rot);
36         fwd.x = sin(theta);
37         fwd.y = 0;
38         fwd.z = -cos(theta);
39         right = Vec3(-fwd.z, 0, fwd.x);
40 }
41
42 float Avatar::get_body_rotation() const
43 {
44         return body_rot;
45 }
46
47 const Vec3 &Avatar::get_body_fwd() const
48 {
49         return fwd;
50 }
51
52 const Vec3 &Avatar::get_body_right() const
53 {
54         return right;
55 }
56
57 const Quat &Avatar::get_head_rotation() const
58 {
59         return head_rot;
60 }
61
62 void Avatar::tracked_head_rotation(const Quat &q)
63 {
64         head_rot = q;
65 }
66
67 // TODO maybe transform from head-relative first
68 void Avatar::tracked_hand_position(int hand, const Vec3 &p)
69 {
70         hand_pos[hand] = p;
71 }
72
73 // TODO maybe transform from head-relative first
74 void Avatar::tracked_hand_rotation(int hand, const Quat &q)
75 {
76         hand_rot[hand] = q;
77 }
78
79 void Avatar::mouselook(float horiz, float vert)
80 {
81         body_rot += horiz;
82         head_alt += vert;
83         if(head_alt > M_PI / 2.0) head_alt = M_PI / 2.0;
84         if(head_alt < -M_PI / 2.0) head_alt = -M_PI / 2.0;
85 }
86
87 Vec3 Avatar::calc_walk_dir(float fwd, float right) const
88 {
89         float theta = M_PI * body_rot / 180.0f;
90         float dx = cos(theta) * right - sin(theta) * fwd;
91         float dz = sin(theta) * right + cos(theta) * fwd;
92         return Vec3(dx, 0, dz);
93 }
94
95 void Avatar::walk(float fwd, float right)
96 {
97         Vec3 wdir = calc_walk_dir(fwd, right);
98         pos += wdir;
99 }