intravenous

annotate src/ship.cc @ 9:90af225f469a

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