From: Mikko Rasa Date: Tue, 25 Mar 2014 23:56:00 +0000 (+0200) Subject: Report the closest object when picking X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e3f081a762c1bd21c051590540d21ec21e9b36b1;p=r2c2.git Report the closest object when picking --- diff --git a/source/libr2c2/layout.cpp b/source/libr2c2/layout.cpp index ac7101d..c7dc346 100644 --- a/source/libr2c2/layout.cpp +++ b/source/libr2c2/layout.cpp @@ -188,12 +188,21 @@ template T *Layout::pick(const Ray &ray) { const set &objs = objects.get(); + T *closest = 0; + float distance = -1; for(set::const_iterator i=objs.begin(); i!=objs.end(); ++i) if(T *t = dynamic_cast(*i)) - if(t->collide_ray(ray)) - return t; - - return 0; + { + float d = -1; + if(t->collide_ray(ray, &d)) + if(!closest || d(const Ray &); diff --git a/source/libr2c2/object.cpp b/source/libr2c2/object.cpp index 7a23d11..7a896b6 100644 --- a/source/libr2c2/object.cpp +++ b/source/libr2c2/object.cpp @@ -2,6 +2,7 @@ #include "object.h" using namespace std; +using namespace Msp; namespace R2C2 { @@ -95,7 +96,7 @@ const Shape *Object::get_shape() const return get_type().get_shape(); } -bool Object::collide_ray(const Ray &ray) const +bool Object::collide_ray(const Ray &ray, float *d) const { const Shape *s = get_shape(); if(!s) @@ -103,7 +104,16 @@ bool Object::collide_ray(const Ray &ray) const Transform reverse_trans = Transform::rotation(rotation, Vector(0, 0, -1))* Transform::translation(-position); - return s->check_intersection(reverse_trans.transform(ray)); + if(d) + { + vector > points = s->get_intersections(reverse_trans.transform(ray)); + if(points.empty()) + return false; + *d = points.front().distance; + return true; + } + else + return s->check_intersection(reverse_trans.transform(ray)); } BoundingBox Object::get_bounding_box() const diff --git a/source/libr2c2/object.h b/source/libr2c2/object.h index 2293792..a0bd4f3 100644 --- a/source/libr2c2/object.h +++ b/source/libr2c2/object.h @@ -53,7 +53,7 @@ public: virtual void break_links(); const Shape *get_shape() const; - virtual bool collide_ray(const Ray &) const; + virtual bool collide_ray(const Ray &, float * = 0) const; virtual BoundingBox get_bounding_box() const; }; diff --git a/source/libr2c2/vehicle.cpp b/source/libr2c2/vehicle.cpp index dee1e76..cb776f4 100644 --- a/source/libr2c2/vehicle.cpp +++ b/source/libr2c2/vehicle.cpp @@ -356,10 +356,10 @@ int Vehicle::get_link_slot(const Object &other) const return -1; } -bool Vehicle::collide_ray(const Ray &ray) const +bool Vehicle::collide_ray(const Ray &ray, float *d) const { if(is_placed()) - return Object::collide_ray(ray); + return Object::collide_ray(ray, d); else return false; } diff --git a/source/libr2c2/vehicle.h b/source/libr2c2/vehicle.h index 33ca728..b522b42 100644 --- a/source/libr2c2/vehicle.h +++ b/source/libr2c2/vehicle.h @@ -108,7 +108,7 @@ public: virtual Vehicle *get_link(unsigned) const; virtual int get_link_slot(const Object &) const; - virtual bool collide_ray(const Ray &) const; + virtual bool collide_ray(const Ray &, float * = 0) const; }; } // namespace R2C2