cloth

changeset 2:2eac424f58b2

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 12 Feb 2013 00:23:40 +0200
parents 76d4b3e8e941
children 28a31079dcdf
files Makefile src/object.h src/plane.cc src/simworld.cc src/simworld.h
diffstat 5 files changed, 28 insertions(+), 50 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Mon Feb 11 19:48:47 2013 +0200
     1.2 +++ b/Makefile	Tue Feb 12 00:23:40 2013 +0200
     1.3 @@ -3,7 +3,7 @@
     1.4  dep = $(obj:.o=.d)
     1.5  bin = cloth
     1.6  
     1.7 -CXXFLAGS = -pedantic -Wall -g
     1.8 +CXXFLAGS = -std=c++11 -pedantic -Wall -g
     1.9  LDFLAGS = $(libgl) -limago -lvmath
    1.10  
    1.11  ifeq ($(shell uname -s), Darwin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/object.h	Tue Feb 12 00:23:40 2013 +0200
     2.3 @@ -0,0 +1,13 @@
     2.4 +#ifndef OBJECT_H_
     2.5 +#define OBJECT_H_
     2.6 +
     2.7 +struct Collision;
     2.8 +
     2.9 +class Object {
    2.10 +public:
    2.11 +	virtual ~Object() = default;
    2.12 +
    2.13 +	virtual bool collision(const Ray &ray, float rad, Collision *col) const = 0;
    2.14 +};
    2.15 +
    2.16 +#endif	// OBJECT_H_
     3.1 --- a/src/plane.cc	Mon Feb 11 19:48:47 2013 +0200
     3.2 +++ b/src/plane.cc	Tue Feb 12 00:23:40 2013 +0200
     3.3 @@ -8,7 +8,7 @@
     3.4  }
     3.5  
     3.6  Plane::Plane(const Vector3 &pt, const Vector3 &norm)
     3.7 -	this->pt(pt), normal(pt)
     3.8 +	: pt(pt), normal(pt)
     3.9  {
    3.10  }
    3.11  
    3.12 @@ -34,6 +34,7 @@
    3.13  		return false;
    3.14  	}
    3.15  
    3.16 +	col->dist = t;
    3.17  	col->pos = ray.origin + ray.dir * t;
    3.18  	col->normal = normal;
    3.19  	col->elast = 1.0;
     4.1 --- a/src/simworld.cc	Mon Feb 11 19:48:47 2013 +0200
     4.2 +++ b/src/simworld.cc	Tue Feb 12 00:23:40 2013 +0200
     4.3 @@ -1,21 +1,12 @@
     4.4 +#include <float.h>
     4.5  #include "simworld.h"
     4.6  
     4.7  SimWorld::SimWorld()
     4.8  {
     4.9 -	bbmin = Vector2(-1, -1);
    4.10 -	bbmax = Vector2(1, 1);
    4.11  	grav = Vector3(0, -9.81, 0);
    4.12  	damping = 0.99;
    4.13  }
    4.14  
    4.15 -void SimWorld::set_bounds(float xmin, float xmax, float ymin, float ymax)
    4.16 -{
    4.17 -	bbmin.x = xmin;
    4.18 -	bbmin.y = ymin;
    4.19 -	bbmax.x = xmax;
    4.20 -	bbmax.y = ymax;
    4.21 -}
    4.22 -
    4.23  void SimWorld::add_particle(Particle *p)
    4.24  {
    4.25  	part.push_back(p);
    4.26 @@ -23,47 +14,18 @@
    4.27  
    4.28  bool SimWorld::collision(const Ray &ray, float rad, Collision *col) const
    4.29  {
    4.30 -	bool found = false;
    4.31 +	Collision nearest;
    4.32 +	nearest.dist = FLT_MAX;
    4.33  
    4.34 -	Vector2 min = bbmin + Vector2(rad, rad);
    4.35 -	Vector2 max = bbmax - Vector2(rad, rad);
    4.36 -
    4.37 -	// collision with the boundaries
    4.38 -	Vector3 npos = ray.origin + ray.dir;
    4.39 -
    4.40 -	Vector3 col_pos, col_norm;
    4.41 -	float d, col_depth = 0;
    4.42 -
    4.43 -	if((d = min.x - npos.x) > col_depth) {
    4.44 -		col->pos = npos;
    4.45 -		col->pos.x = min.x;
    4.46 -		col->normal = Vector3(1, 0, 0);
    4.47 -		col_depth = d;
    4.48 -		found = true;
    4.49 -	}
    4.50 -	if((d = min.y - npos.z) > col_depth) {
    4.51 -		col->pos = npos;
    4.52 -		col->pos.z = min.y;
    4.53 -		col->normal = Vector3(0, 0, 1);
    4.54 -		col_depth = d;
    4.55 -		found = true;
    4.56 -	}
    4.57 -	if((d = npos.x - max.x) > col_depth) {
    4.58 -		col->pos = npos;
    4.59 -		col->pos.x = max.x;
    4.60 -		col->normal = Vector3(-1, 0, 0);
    4.61 -		col_depth = d;
    4.62 -		found = true;
    4.63 -	}
    4.64 -	if((d = npos.z - max.y) > col_depth) {
    4.65 -		col->pos = npos;
    4.66 -		col->pos.z = max.y;
    4.67 -		col->normal = Vector3(0, 0, -1);
    4.68 -		col_depth = d;
    4.69 -		found = true;
    4.70 +	for(auto obj : objects) {
    4.71 +		Collision tmpcol;
    4.72 +		if(obj->collision(ray, rad, &tmpcol) && tmpcol.dist < nearest.dist) {
    4.73 +			nearest = tmpcol;
    4.74 +		}
    4.75  	}
    4.76  
    4.77 -	return found;
    4.78 +	*col = nearest;
    4.79 +	return true;
    4.80  }
    4.81  
    4.82  void SimWorld::step(float dt)
     5.1 --- a/src/simworld.h	Mon Feb 11 19:48:47 2013 +0200
     5.2 +++ b/src/simworld.h	Tue Feb 12 00:23:40 2013 +0200
     5.3 @@ -4,8 +4,10 @@
     5.4  #include <vector>
     5.5  #include "particle.h"
     5.6  #include "vmath/vmath.h"
     5.7 +#include "object.h"
     5.8  
     5.9  struct Collision {
    5.10 +	float dist;
    5.11  	Vector3 pos;
    5.12  	Vector3 normal;
    5.13  	float elast;