X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Fobject.cpp;h=7a896b6581b40c849e5a3296c513260038b4c230;hb=3907520897611cb1b5f2f938c60d3eaff34477a5;hp=e9e58abf97041c44351a7becd185895e95f79304;hpb=bde934f7d5ba07a9442a305192e10024820d13ed;p=r2c2.git diff --git a/source/libr2c2/object.cpp b/source/libr2c2/object.cpp index e9e58ab..7a896b6 100644 --- a/source/libr2c2/object.cpp +++ b/source/libr2c2/object.cpp @@ -2,9 +2,15 @@ #include "object.h" using namespace std; +using namespace Msp; namespace R2C2 { +Object::Object(Layout &l): + layout(l), + shape(0) +{ } + Snap Object::get_snap_node(unsigned) const { throw out_of_range("Object::get_snap_node"); @@ -45,9 +51,15 @@ bool Object::snap_to(const Object &other, float limit, SnapType what) Snap ssn = sn; if(other.snap(ssn, limit, what)) { - set_rotation(rotation+ssn.rotation-sn.rotation-Angle::half_turn()); + if(what==SNAP_NODE) + set_rotation(rotation+ssn.rotation-sn.rotation-Angle::half_turn()); + else + { + Angle adiff = wrap_balanced((ssn.rotation-sn.rotation)*2.0f)/2.0f; + set_rotation(rotation+adiff); + } sn = get_snap_node(i); - position += ssn.position-sn.position; + set_position(position+ssn.position-sn.position); return true; } } @@ -77,26 +89,42 @@ void Object::break_links() break_link(i); } -bool Object::collide_ray(const Ray &ray) const +const Shape *Object::get_shape() const +{ + if(shape) + return shape; + return get_type().get_shape(); +} + +bool Object::collide_ray(const Ray &ray, float *d) const { - const Shape *shape = get_type().get_shape(); - if(!shape) + const Shape *s = get_shape(); + if(!s) return false; Transform reverse_trans = Transform::rotation(rotation, Vector(0, 0, -1))* Transform::translation(-position); - return shape->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 { - const Shape *shape = get_type().get_shape(); - if(!shape) + const Shape *s = get_shape(); + if(!s) return BoundingBox(); Transform trans = Transform::translation(position)* Transform::rotation(rotation, Vector(0, 0, 1)); - return trans.transform(shape->get_axis_aligned_bounding_box()); + return trans.transform(s->get_axis_aligned_bounding_box()); } } // namespace R2C2