intravenous
changeset 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 | 0f305fc0b548 2723dc026c4f |
children | 8fbdc6f84f64 |
files | src/game.cc src/ship.cc src/vein.cc |
diffstat | 9 files changed, 99 insertions(+), 50 deletions(-) [+] |
line diff
1.1 --- a/Makefile Sun Apr 22 07:15:28 2012 +0300 1.2 +++ b/Makefile Sat Apr 28 17:10:29 2012 +0300 1.3 @@ -9,6 +9,8 @@ 1.4 LDFLAGS = -L/usr/local/lib $(add_lib) $(libgl) $(libal) -lm -lvmath -limago 1.5 1.6 ifeq ($(shell uname -s), Darwin) 1.7 + CC = clang 1.8 + CXX = clang++ 1.9 libgl = -framework OpenGL -framework GLUT -lGLEW 1.10 add_inc += -I/opt/local/include 1.11 add_lib += -L/opt/local/lib
2.1 --- a/sdr/vein.p.glsl Sun Apr 22 07:15:28 2012 +0300 2.2 +++ b/sdr/vein.p.glsl Sat Apr 28 17:10:29 2012 +0300 2.3 @@ -1,7 +1,7 @@ 2.4 uniform sampler2D tex_norm; 2.5 uniform vec3 fog_col; 2.6 2.7 -varying vec3 vpos, vnorm; 2.8 +varying vec3 vpos; 2.9 varying vec3 ldir, vdir; 2.10 2.11 const float fog_dens = 0.075; 2.12 @@ -13,7 +13,7 @@ 2.13 vec3 tnorm = texture2D(tex_norm, gl_TexCoord[0].st).xyz * 2.0 - 1.0; 2.14 vec3 n = normalize(tnorm); 2.15 vec3 v = normalize(vdir); 2.16 - vec3 l = v;//normalize(ldir); 2.17 + vec3 l = normalize(ldir); 2.18 vec3 h = normalize(l + v); 2.19 2.20 vec3 diff = vec3(0.8, 0.25, 0.2) * (1.0 - max(dot(l, n), 0.0));
3.1 --- a/sdr/vein.v.glsl Sun Apr 22 07:15:28 2012 +0300 3.2 +++ b/sdr/vein.v.glsl Sat Apr 28 17:10:29 2012 +0300 3.3 @@ -1,6 +1,6 @@ 3.4 attribute vec3 attr_tang; 3.5 3.6 -varying vec3 vpos, vnorm; 3.7 +varying vec3 vpos; 3.8 varying vec3 ldir, vdir; 3.9 3.10 void main() 3.11 @@ -18,9 +18,8 @@ 3.12 t.x, b.x, n.x, 3.13 t.y, b.y, n.y, 3.14 t.z, b.z, n.z); 3.15 - ldir = tbn_mat * (gl_LightSource[0].position.xyz - vpos); 3.16 + ldir = tbn_mat * vec3(0, 0, 1); 3.17 vdir = tbn_mat * -vpos; 3.18 - vnorm = n; 3.19 3.20 gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; 3.21 }
4.1 --- a/src/game.cc Sun Apr 22 07:15:28 2012 +0300 4.2 +++ b/src/game.cc Sat Apr 28 17:10:29 2012 +0300 4.3 @@ -6,10 +6,10 @@ 4.4 #include "camera.h" 4.5 #include "cockpit.h" 4.6 4.7 -static const Vector3 fog_color{0.64, 0.1, 0.1}; 4.8 +static const Vector3 fog_color(0.64, 0.1, 0.1); 4.9 4.10 static Vein *vein; 4.11 -static Ship ship; 4.12 +static Ship *ship; 4.13 4.14 static int keystate[256]; 4.15 static int dbg_inside = false; 4.16 @@ -39,6 +39,9 @@ 4.17 } 4.18 vein->set_fog_color(fog_color); 4.19 4.20 + // create the player's ship 4.21 + ship = new Ship(vein); 4.22 + 4.23 glClearColor(fog_color.x, fog_color.y, fog_color.z, 1); 4.24 4.25 return true; 4.26 @@ -64,25 +67,19 @@ 4.27 4.28 // handle key input 4.29 if(keystate['w'] || keystate['W']) { 4.30 - ship.accelerate(1.0 * dt); 4.31 + ship->accelerate(4.0 * dt); 4.32 } 4.33 if(keystate['s'] || keystate['S']) { 4.34 - ship.accelerate(-1.0 * dt); 4.35 + ship->accelerate(-4.0 * dt); 4.36 } 4.37 if(keystate['d'] || keystate['D']) { 4.38 - ship.accelerate_side(1.0 * dt); 4.39 + ship->accelerate_side(4.0 * dt); 4.40 } 4.41 if(keystate['a'] || keystate['A']) { 4.42 - ship.accelerate_side(-1.0 * dt); 4.43 + ship->accelerate_side(-4.0 * dt); 4.44 } 4.45 - /*if(keystate['e'] || keystate['E']) { 4.46 - ship.turn(0.0, 1.0 * dt); 4.47 - } 4.48 - if(keystate['c'] || keystate['C']) { 4.49 - ship.turn(0.0, -1.0 * dt); 4.50 - }*/ 4.51 4.52 - ship.update(dt); 4.53 + ship->update(dt); 4.54 4.55 last_upd = msec; 4.56 } 4.57 @@ -92,15 +89,15 @@ 4.58 glMatrixMode(GL_MODELVIEW); 4.59 4.60 if(dbg_inside) { 4.61 - load_gl_matrix(ship.get_matrix().inverse()); 4.62 + load_gl_matrix(ship->get_matrix().inverse()); 4.63 } else { 4.64 dbgcam.use(); 4.65 } 4.66 4.67 - vein->draw(ship.get_position()); 4.68 + vein->draw(ship->get_position()); 4.69 4.70 if(!dbg_inside) { 4.71 - ship.dbg_draw(); 4.72 + ship->dbg_draw(); 4.73 } else { 4.74 cockpit_draw(); 4.75 } 4.76 @@ -115,6 +112,7 @@ 4.77 break; 4.78 4.79 case '\b': 4.80 + case 127: 4.81 dbg_inside = !dbg_inside; 4.82 if(dbg_inside) { 4.83 glutPassiveMotionFunc(game_input_mmotion); 4.84 @@ -166,7 +164,7 @@ 4.85 } 4.86 4.87 if(dbg_inside) { 4.88 - ship.turn((float)-dx / win_xsz, (float)-dy / win_ysz); 4.89 + ship->turn((float)-dx / win_xsz, (float)-dy / win_ysz); 4.90 4.91 if(x < EDGE_PIXELS || x >= win_xsz - EDGE_PIXELS || y < EDGE_PIXELS || y >= win_ysz - EDGE_PIXELS) { 4.92 glutWarpPointer(win_xsz / 2, win_ysz / 2);
5.1 --- a/src/ship.cc Sun Apr 22 07:15:28 2012 +0300 5.2 +++ b/src/ship.cc Sat Apr 28 17:10:29 2012 +0300 5.3 @@ -1,9 +1,11 @@ 5.4 #include "opengl.h" 5.5 #include "ship.h" 5.6 +#include "vein.h" 5.7 5.8 -Ship::Ship() 5.9 +Ship::Ship(Vein *vein) 5.10 { 5.11 - friction = 0.2; 5.12 + friction = 0.95; 5.13 + this->vein = vein; 5.14 } 5.15 5.16 void Ship::accelerate(double a) 5.17 @@ -27,8 +29,37 @@ 5.18 5.19 void Ship::update(time_sec_t dt) 5.20 { 5.21 - pos += velocity * dt; 5.22 + Vector3 newpos = pos + velocity * dt; 5.23 velocity -= velocity * friction * dt; 5.24 + 5.25 + HitPoint hit; 5.26 + if(collision(vein, pos, newpos, &hit)) { 5.27 + newpos = hit.pos; 5.28 + // TODO damp velocity along normal 5.29 + } 5.30 + 5.31 + pos = newpos; 5.32 +} 5.33 + 5.34 +#define SHIP_RAD 0.25 5.35 +bool Ship::collision(const Vein *vein, const Vector3 &start, const Vector3 &end, HitPoint *hit) const 5.36 +{ 5.37 + Vector3 cent = vein->calc_center(end); 5.38 + float rad = vein->get_radius(); 5.39 + 5.40 + Vector3 dir = end - cent; 5.41 + float dist = dir.length(); 5.42 + 5.43 + if(dist < rad - SHIP_RAD) { 5.44 + return false; 5.45 + } 5.46 + 5.47 + if(hit) { 5.48 + Vector3 dir_norm = dir / dist; 5.49 + hit->pos = cent + dir_norm * (rad - SHIP_RAD); 5.50 + hit->normal = -dir_norm; 5.51 + } 5.52 + return true; 5.53 } 5.54 5.55 const Vector3 &Ship::get_position() const 5.56 @@ -38,13 +69,13 @@ 5.57 5.58 Vector3 Ship::get_direction() const 5.59 { 5.60 - static const Vector3 dir{0, 0, -1}; 5.61 + static const Vector3 dir(0, 0, -1); 5.62 return dir.transformed(rot); 5.63 } 5.64 5.65 Vector3 Ship::get_right() const 5.66 { 5.67 - static const Vector3 dir{1, 0, 0}; 5.68 + static const Vector3 dir(1, 0, 0); 5.69 return dir.transformed(rot); 5.70 } 5.71
6.1 --- a/src/ship.h Sun Apr 22 07:15:28 2012 +0300 6.2 +++ b/src/ship.h Sat Apr 28 17:10:29 2012 +0300 6.3 @@ -4,14 +4,22 @@ 6.4 #include <vmath/vmath.h> 6.5 #include "game.h" 6.6 6.7 +class Vein; 6.8 + 6.9 +struct HitPoint { 6.10 + Vector3 pos; 6.11 + Vector3 normal; 6.12 +}; 6.13 + 6.14 class Ship { 6.15 private: 6.16 Vector3 pos, velocity; 6.17 Quaternion rot; 6.18 double friction; 6.19 + Vein *vein; 6.20 6.21 public: 6.22 - Ship(); 6.23 + Ship(Vein *vein); 6.24 6.25 void accelerate(double a); 6.26 void accelerate_side(double a); 6.27 @@ -19,6 +27,8 @@ 6.28 6.29 void update(time_sec_t dt); 6.30 6.31 + bool collision(const Vein *vein, const Vector3 &start, const Vector3 &end, HitPoint *hit) const; 6.32 + 6.33 const Vector3 &get_position() const; 6.34 Vector3 get_direction() const; 6.35 Vector3 get_right() const;
7.1 --- a/src/sys.c Sun Apr 22 07:15:28 2012 +0300 7.2 +++ b/src/sys.c Sat Apr 28 17:10:29 2012 +0300 7.3 @@ -5,9 +5,6 @@ 7.4 #elif defined(WIN32) 7.5 #include <windows.h> 7.6 #elif defined(__APPLE__) 7.7 -#error "not ported to mac yet" 7.8 -#else 7.9 -#error "unrecognized or unsupported platform" 7.10 #endif 7.11 7.12 int sys_lock_mouse(void)
8.1 --- a/src/vein.cc Sun Apr 22 07:15:28 2012 +0300 8.2 +++ b/src/vein.cc Sat Apr 28 17:10:29 2012 +0300 8.3 @@ -29,20 +29,6 @@ 8.4 free_program(sdr); 8.5 } 8.6 8.7 -Vector3 Vein::calc_center(const Vector3 &ppos) const 8.8 -{ 8.9 - Vector3 pt{0, 0, ppos.z}; 8.10 - pt.x = sin(ppos.z * 0.75); 8.11 - pt.y = cos(ppos.z * 0.2) * 0.6; 8.12 - return pt; 8.13 -} 8.14 - 8.15 -Vector3 Vein::calc_dir(const Vector3 &ppos) const 8.16 -{ 8.17 - Vector3 dir = calc_center(ppos + Vector3(0, 0, 0.01)) - calc_center(ppos - Vector3(0, 0, 0.01)); 8.18 - return dir.normalized(); 8.19 -} 8.20 - 8.21 void Vein::build_idxbuf() 8.22 { 8.23 delete [] idxbuf; 8.24 @@ -80,6 +66,16 @@ 8.25 return true; 8.26 } 8.27 8.28 +void Vein::set_radius(float rad) 8.29 +{ 8.30 + this->rad = rad; 8.31 +} 8.32 + 8.33 +float Vein::get_radius() const 8.34 +{ 8.35 + return rad; 8.36 +} 8.37 + 8.38 void Vein::set_fog_color(const Vector3 &col) 8.39 { 8.40 fog_color = col; 8.41 @@ -110,7 +106,7 @@ 8.42 Vector3 right = cross_product(up, dir); 8.43 up = cross_product(dir, right); 8.44 8.45 - Matrix3x3 vrot{right, up, dir}; 8.46 + Matrix3x3 vrot(right, up, dir); 8.47 vrot.transpose(); 8.48 8.49 float theta = 0.0, dtheta = 2.0 * M_PI / (ring_subdiv - 1); 8.50 @@ -149,3 +145,17 @@ 8.51 8.52 glPopMatrix(); 8.53 } 8.54 + 8.55 +Vector3 Vein::calc_center(const Vector3 &ppos) const 8.56 +{ 8.57 + Vector3 pt(0, 0, ppos.z); 8.58 + pt.x = sin(ppos.z * 0.75); 8.59 + pt.y = cos(ppos.z * 0.2) * 0.6; 8.60 + return pt; 8.61 +} 8.62 + 8.63 +Vector3 Vein::calc_dir(const Vector3 &ppos) const 8.64 +{ 8.65 + Vector3 dir = calc_center(ppos + Vector3(0, 0, 0.01)) - calc_center(ppos - Vector3(0, 0, 0.01)); 8.66 + return dir.normalized(); 8.67 +}
9.1 --- a/src/vein.h Sun Apr 22 07:15:28 2012 +0300 9.2 +++ b/src/vein.h Sat Apr 28 17:10:29 2012 +0300 9.3 @@ -16,20 +16,22 @@ 9.4 unsigned int tex_norm; 9.5 Vector3 fog_color; 9.6 9.7 - Vector3 calc_center(const Vector3 &ppos) const; 9.8 - Vector3 calc_dir(const Vector3 &ppos) const; 9.9 - 9.10 void build_idxbuf(); 9.11 9.12 public: 9.13 Vein(); 9.14 ~Vein(); 9.15 + bool init(); 9.16 9.17 - bool init(); 9.18 + void set_radius(float rad); 9.19 + float get_radius() const; 9.20 9.21 void set_fog_color(const Vector3 &col); 9.22 9.23 void draw(const Vector3 &player_pos) const; 9.24 + 9.25 + Vector3 calc_center(const Vector3 &ppos) const; 9.26 + Vector3 calc_dir(const Vector3 &ppos) const; 9.27 }; 9.28 9.29 #endif // VEIN_H_