intravenous

diff 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
line diff
     1.1 --- a/src/ship.cc	Sun Apr 22 07:15:28 2012 +0300
     1.2 +++ b/src/ship.cc	Sat Apr 28 17:10:29 2012 +0300
     1.3 @@ -1,9 +1,11 @@
     1.4  #include "opengl.h"
     1.5  #include "ship.h"
     1.6 +#include "vein.h"
     1.7  
     1.8 -Ship::Ship()
     1.9 +Ship::Ship(Vein *vein)
    1.10  {
    1.11 -	friction = 0.2;
    1.12 +	friction = 0.95;
    1.13 +	this->vein = vein;
    1.14  }
    1.15  
    1.16  void Ship::accelerate(double a)
    1.17 @@ -27,8 +29,37 @@
    1.18  
    1.19  void Ship::update(time_sec_t dt)
    1.20  {
    1.21 -	pos += velocity * dt;
    1.22 +	Vector3 newpos = pos + velocity * dt;
    1.23  	velocity -= velocity * friction * dt;
    1.24 +
    1.25 +	HitPoint hit;
    1.26 +	if(collision(vein, pos, newpos, &hit)) {
    1.27 +		newpos = hit.pos;
    1.28 +		// TODO damp velocity along normal
    1.29 +	}
    1.30 +
    1.31 +	pos = newpos;
    1.32 +}
    1.33 +
    1.34 +#define SHIP_RAD	0.25
    1.35 +bool Ship::collision(const Vein *vein, const Vector3 &start, const Vector3 &end, HitPoint *hit) const
    1.36 +{
    1.37 +	Vector3 cent = vein->calc_center(end);
    1.38 +	float rad = vein->get_radius();
    1.39 +
    1.40 +	Vector3 dir = end - cent;
    1.41 +	float dist = dir.length();
    1.42 +
    1.43 +	if(dist < rad - SHIP_RAD) {
    1.44 +		return false;
    1.45 +	}
    1.46 +
    1.47 +	if(hit) {
    1.48 +		Vector3 dir_norm = dir / dist;
    1.49 +		hit->pos = cent + dir_norm * (rad - SHIP_RAD);
    1.50 +		hit->normal = -dir_norm;
    1.51 +	}
    1.52 +	return true;
    1.53  }
    1.54  
    1.55  const Vector3 &Ship::get_position() const
    1.56 @@ -38,13 +69,13 @@
    1.57  
    1.58  Vector3 Ship::get_direction() const
    1.59  {
    1.60 -	static const Vector3 dir{0, 0, -1};
    1.61 +	static const Vector3 dir(0, 0, -1);
    1.62  	return dir.transformed(rot);
    1.63  }
    1.64  
    1.65  Vector3 Ship::get_right() const
    1.66  {
    1.67 -	static const Vector3 dir{1, 0, 0};
    1.68 +	static const Vector3 dir(1, 0, 0);
    1.69  	return dir.transformed(rot);
    1.70  }
    1.71