intravenous
view src/ship.cc @ 10:8fbdc6f84f64
fixed after the change in vmath
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 31 May 2013 01:30:14 +0300 |
parents | 90af225f469a |
children |
line source
1 #include "opengl.h"
2 #include "ship.h"
3 #include "vein.h"
5 Ship::Ship(Vein *vein)
6 {
7 friction = 0.95;
8 this->vein = vein;
9 }
11 void Ship::accelerate(double a)
12 {
13 velocity += get_direction() * a;
14 }
16 void Ship::accelerate_side(double a)
17 {
18 velocity += get_right() * a;
19 }
21 void Ship::turn(double yaw, double pitch)
22 {
23 Quaternion qyaw(Vector3(0, 1, 0), yaw);
24 Quaternion qpitch(Vector3(1, 0, 0), pitch);
26 rot *= qpitch;
27 rot *= qyaw;
28 }
30 void Ship::update(time_sec_t dt)
31 {
32 Vector3 newpos = pos + velocity * dt;
33 velocity -= velocity * friction * dt;
35 HitPoint hit;
36 if(collision(vein, pos, newpos, &hit)) {
37 newpos = hit.pos;
38 // TODO damp velocity along normal
39 }
41 pos = newpos;
42 }
44 #define SHIP_RAD 0.25
45 bool Ship::collision(const Vein *vein, const Vector3 &start, const Vector3 &end, HitPoint *hit) const
46 {
47 Vector3 cent = vein->calc_center(end);
48 float rad = vein->get_radius();
50 Vector3 dir = end - cent;
51 float dist = dir.length();
53 if(dist < rad - SHIP_RAD) {
54 return false;
55 }
57 if(hit) {
58 Vector3 dir_norm = dir / dist;
59 hit->pos = cent + dir_norm * (rad - SHIP_RAD);
60 hit->normal = -dir_norm;
61 }
62 return true;
63 }
65 const Vector3 &Ship::get_position() const
66 {
67 return pos;
68 }
70 Vector3 Ship::get_direction() const
71 {
72 static const Vector3 dir(0, 0, -1);
73 return dir.transformed(rot);
74 }
76 Vector3 Ship::get_right() const
77 {
78 static const Vector3 dir(1, 0, 0);
79 return dir.transformed(rot);
80 }
82 Matrix4x4 Ship::get_matrix() const
83 {
84 Matrix3x3 rmat = rot.get_rotation_matrix();//.transposed();
85 Matrix4x4 tmat;
86 tmat.set_translation(pos);
88 return tmat * Matrix4x4(rmat);
89 }
91 void Ship::dbg_draw() const
92 {
93 glPushAttrib(GL_POINT_BIT | GL_ENABLE_BIT);
95 glPointSize(3.0);
96 glDisable(GL_LIGHTING);
98 glBegin(GL_LINES);
99 glColor3f(0, 0, 1);
100 glVertex3f(pos.x, pos.y, pos.z);
102 Vector3 end = pos + get_direction();
103 glVertex3f(end.x, end.y, end.z);
104 glEnd();
106 glBegin(GL_POINTS);
107 glColor3f(1, 0, 0);
108 glVertex3f(pos.x, pos.y, pos.z);
109 glEnd();
111 glMatrixMode(GL_MODELVIEW);
112 glPushMatrix();
113 mult_gl_matrix(get_matrix());
115 glScalef(1, 1, -1);
116 glColor3f(1, 1, 0);
117 glutWireCone(0.1, 0.6, 12, 2);
119 glPopMatrix();
121 glPopAttrib();
122 }