X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrackpart.cpp;h=71845bb73c2c22591d3f99b59aaf4d455b2d7988;hb=66c2c7ca5a4bd369293959bc211b040834343381;hp=d60d374091556b2f89441e9febfc4ddda4ebd7fa;hpb=277e8e7e4230aa8867f507ace62476afe575cff1;p=r2c2.git diff --git a/source/libr2c2/trackpart.cpp b/source/libr2c2/trackpart.cpp index d60d374..71845bb 100644 --- a/source/libr2c2/trackpart.cpp +++ b/source/libr2c2/trackpart.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of R²C² -Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - #include #include "trackpart.h" @@ -55,6 +48,37 @@ TrackPoint TrackPart::get_point(float d) const return result; } +TrackPoint TrackPart::get_nearest_point(const Vector &p) const +{ + TrackPoint tp; + float c = cos(dir); + float s = sin(dir); + if(radius) + { + Vector v(p.x-pos.x+radius*s, p.y-pos.y-radius*c); + float a = atan2(v.y, v.x)+M_PI/2-dir; + if(radius<0) + a = M_PI-a; + while(alength/2+M_PI) + a -= M_PI*2; + a = min(max(a, 0.0f), length); + if(radius<0) + a = -a; + tp.pos = Vector(pos.x+radius*(sin(dir+a)-s), pos.y+radius*(c-cos(dir+a))); + tp.dir = dir+a; + } + else + { + Vector v(p.x-pos.x, p.y-pos.y); + float d = min(max(c*v.x+s*v.y, 0.0f), length); + tp.pos = Vector(pos.x+c*d, pos.y+s*d); + tp.dir = dir; + } + return tp; +} + void TrackPart::check_link(TrackPart &other) { unsigned n_eps = (dead_end ? 1 : 2); @@ -88,7 +112,7 @@ void TrackPart::check_link(TrackPart &other) TrackPart *TrackPart::get_link(unsigned i) const { if(i>=2) - throw InvalidParameterValue("Index out of range"); + throw out_of_range("TrackPart::get_link"); return links[i]; } @@ -121,7 +145,7 @@ bool TrackPart::collide_ray(const Vector &start, const Vector &ray, float width) TrackPart::Loader::Loader(TrackPart &p): - Msp::DataFile::BasicLoader(p) + Msp::DataFile::ObjectLoader(p) { add("start", &Loader::start); add("length", &TrackPart::length);