intravenous

view src/ship.cc @ 3:94d4c60af435

some progress
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Apr 2012 03:35:18 +0300
parents 472c28b8b875
children 2723dc026c4f
line source
1 #include "opengl.h"
2 #include "ship.h"
4 Ship::Ship()
5 {
6 friction = 0.2;
7 }
9 void Ship::accelerate(double a)
10 {
11 velocity += get_direction() * a;
12 }
14 void Ship::accelerate_side(double a)
15 {
16 velocity += get_right() * a;
17 }
19 void Ship::turn(double yaw, double pitch)
20 {
21 Quaternion qyaw(Vector3(0, 1, 0), yaw);
22 Quaternion qpitch(Vector3(1, 0, 0), pitch);
24 rot *= qpitch;
25 rot *= qyaw;
26 }
28 void Ship::update(time_sec_t dt)
29 {
30 pos += velocity * dt;
31 velocity -= velocity * friction * dt;
32 }
34 const Vector3 &Ship::get_position() const
35 {
36 return pos;
37 }
39 Vector3 Ship::get_direction() const
40 {
41 static const Vector3 dir{0, 0, -1};
42 return dir.transformed(rot);
43 }
45 Vector3 Ship::get_right() const
46 {
47 static const Vector3 dir{1, 0, 0};
48 return dir.transformed(rot);
49 }
51 Matrix4x4 Ship::get_matrix() const
52 {
53 Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
54 Matrix4x4 tmat;
55 tmat.set_translation(pos);
57 return tmat * Matrix4x4(rmat);
58 }
60 void Ship::dbg_draw() const
61 {
62 glPushAttrib(GL_POINT_BIT | GL_ENABLE_BIT);
64 glPointSize(3.0);
65 glDisable(GL_LIGHTING);
67 glBegin(GL_LINES);
68 glColor3f(0, 0, 1);
69 glVertex3f(pos.x, pos.y, pos.z);
71 Vector3 end = pos + get_direction();
72 glVertex3f(end.x, end.y, end.z);
73 glEnd();
75 glBegin(GL_POINTS);
76 glColor3f(1, 0, 0);
77 glVertex3f(pos.x, pos.y, pos.z);
78 glEnd();
80 glMatrixMode(GL_MODELVIEW);
81 glPushMatrix();
82 mult_gl_matrix(get_matrix());
84 glScalef(1, 1, -1);
85 glColor3f(1, 1, 0);
86 glutWireCone(0.1, 0.6, 12, 2);
88 glPopMatrix();
90 glPopAttrib();
91 }