intravenous

diff src/ship.cc @ 6:2723dc026c4f

collision detection
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Apr 2012 21:43:10 +0300
parents 94d4c60af435
children 90af225f469a
line diff
     1.1 --- a/src/ship.cc	Sun Apr 22 06:26:08 2012 +0300
     1.2 +++ b/src/ship.cc	Mon Apr 23 21:43:10 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 +	this->vein = vein;
    1.13  }
    1.14  
    1.15  void Ship::accelerate(double a)
    1.16 @@ -27,8 +29,37 @@
    1.17  
    1.18  void Ship::update(time_sec_t dt)
    1.19  {
    1.20 -	pos += velocity * dt;
    1.21 +	Vector3 newpos = pos + velocity * dt;
    1.22  	velocity -= velocity * friction * dt;
    1.23 +
    1.24 +	HitPoint hit;
    1.25 +	if(collision(vein, pos, newpos, &hit)) {
    1.26 +		newpos = hit.pos;
    1.27 +		// TODO damp velocity along normal
    1.28 +	}
    1.29 +
    1.30 +	pos = newpos;
    1.31 +}
    1.32 +
    1.33 +#define SHIP_RAD	0.25
    1.34 +bool Ship::collision(const Vein *vein, const Vector3 &start, const Vector3 &end, HitPoint *hit) const
    1.35 +{
    1.36 +	Vector3 cent = vein->calc_center(end);
    1.37 +	float rad = vein->get_radius();
    1.38 +
    1.39 +	Vector3 dir = end - cent;
    1.40 +	float dist = dir.length();
    1.41 +
    1.42 +	if(dist < rad - SHIP_RAD) {
    1.43 +		return false;
    1.44 +	}
    1.45 +
    1.46 +	if(hit) {
    1.47 +		Vector3 dir_norm = dir / dist;
    1.48 +		hit->pos = cent + dir_norm * (rad - SHIP_RAD);
    1.49 +		hit->normal = -dir_norm;
    1.50 +	}
    1.51 +	return true;
    1.52  }
    1.53  
    1.54  const Vector3 &Ship::get_position() const
    1.55 @@ -38,13 +69,13 @@
    1.56  
    1.57  Vector3 Ship::get_direction() const
    1.58  {
    1.59 -	static const Vector3 dir{0, 0, -1};
    1.60 +	static const Vector3 dir(0, 0, -1);
    1.61  	return dir.transformed(rot);
    1.62  }
    1.63  
    1.64  Vector3 Ship::get_right() const
    1.65  {
    1.66 -	static const Vector3 dir{1, 0, 0};
    1.67 +	static const Vector3 dir(1, 0, 0);
    1.68  	return dir.transformed(rot);
    1.69  }
    1.70