# HG changeset patch # User John Tsiombikas # Date 1360621420 -7200 # Node ID 2eac424f58b2e63b7ba65aa7d10d52f7ef35ce97 # Parent 76d4b3e8e9412a44a929c668ee207aad76c60096 foo diff -r 76d4b3e8e941 -r 2eac424f58b2 Makefile --- a/Makefile Mon Feb 11 19:48:47 2013 +0200 +++ b/Makefile Tue Feb 12 00:23:40 2013 +0200 @@ -3,7 +3,7 @@ dep = $(obj:.o=.d) bin = cloth -CXXFLAGS = -pedantic -Wall -g +CXXFLAGS = -std=c++11 -pedantic -Wall -g LDFLAGS = $(libgl) -limago -lvmath ifeq ($(shell uname -s), Darwin) diff -r 76d4b3e8e941 -r 2eac424f58b2 src/object.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/object.h Tue Feb 12 00:23:40 2013 +0200 @@ -0,0 +1,13 @@ +#ifndef OBJECT_H_ +#define OBJECT_H_ + +struct Collision; + +class Object { +public: + virtual ~Object() = default; + + virtual bool collision(const Ray &ray, float rad, Collision *col) const = 0; +}; + +#endif // OBJECT_H_ diff -r 76d4b3e8e941 -r 2eac424f58b2 src/plane.cc --- a/src/plane.cc Mon Feb 11 19:48:47 2013 +0200 +++ b/src/plane.cc Tue Feb 12 00:23:40 2013 +0200 @@ -8,7 +8,7 @@ } Plane::Plane(const Vector3 &pt, const Vector3 &norm) - this->pt(pt), normal(pt) + : pt(pt), normal(pt) { } @@ -34,6 +34,7 @@ return false; } + col->dist = t; col->pos = ray.origin + ray.dir * t; col->normal = normal; col->elast = 1.0; diff -r 76d4b3e8e941 -r 2eac424f58b2 src/simworld.cc --- a/src/simworld.cc Mon Feb 11 19:48:47 2013 +0200 +++ b/src/simworld.cc Tue Feb 12 00:23:40 2013 +0200 @@ -1,21 +1,12 @@ +#include #include "simworld.h" SimWorld::SimWorld() { - bbmin = Vector2(-1, -1); - bbmax = Vector2(1, 1); grav = Vector3(0, -9.81, 0); damping = 0.99; } -void SimWorld::set_bounds(float xmin, float xmax, float ymin, float ymax) -{ - bbmin.x = xmin; - bbmin.y = ymin; - bbmax.x = xmax; - bbmax.y = ymax; -} - void SimWorld::add_particle(Particle *p) { part.push_back(p); @@ -23,47 +14,18 @@ bool SimWorld::collision(const Ray &ray, float rad, Collision *col) const { - bool found = false; + Collision nearest; + nearest.dist = FLT_MAX; - Vector2 min = bbmin + Vector2(rad, rad); - Vector2 max = bbmax - Vector2(rad, rad); - - // collision with the boundaries - Vector3 npos = ray.origin + ray.dir; - - Vector3 col_pos, col_norm; - float d, col_depth = 0; - - if((d = min.x - npos.x) > col_depth) { - col->pos = npos; - col->pos.x = min.x; - col->normal = Vector3(1, 0, 0); - col_depth = d; - found = true; - } - if((d = min.y - npos.z) > col_depth) { - col->pos = npos; - col->pos.z = min.y; - col->normal = Vector3(0, 0, 1); - col_depth = d; - found = true; - } - if((d = npos.x - max.x) > col_depth) { - col->pos = npos; - col->pos.x = max.x; - col->normal = Vector3(-1, 0, 0); - col_depth = d; - found = true; - } - if((d = npos.z - max.y) > col_depth) { - col->pos = npos; - col->pos.z = max.y; - col->normal = Vector3(0, 0, -1); - col_depth = d; - found = true; + for(auto obj : objects) { + Collision tmpcol; + if(obj->collision(ray, rad, &tmpcol) && tmpcol.dist < nearest.dist) { + nearest = tmpcol; + } } - return found; + *col = nearest; + return true; } void SimWorld::step(float dt) diff -r 76d4b3e8e941 -r 2eac424f58b2 src/simworld.h --- a/src/simworld.h Mon Feb 11 19:48:47 2013 +0200 +++ b/src/simworld.h Tue Feb 12 00:23:40 2013 +0200 @@ -4,8 +4,10 @@ #include #include "particle.h" #include "vmath/vmath.h" +#include "object.h" struct Collision { + float dist; Vector3 pos; Vector3 normal; float elast;