X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrackpart.cpp;h=d60d374091556b2f89441e9febfc4ddda4ebd7fa;hb=34f8bf04e4bdffefeceea59b186386b51c542b9d;hp=f677c703ee5a7e89f7ffa9d241fbaa69754f433c;hpb=f8a7788cee0261babfc4c804a58515aad6dfbc3d;p=r2c2.git diff --git a/source/libr2c2/trackpart.cpp b/source/libr2c2/trackpart.cpp index f677c70..d60d374 100644 --- a/source/libr2c2/trackpart.cpp +++ b/source/libr2c2/trackpart.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of R²C² -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -92,6 +92,33 @@ TrackPart *TrackPart::get_link(unsigned i) const return links[i]; } +bool TrackPart::collide_ray(const Vector &start, const Vector &ray, float width) const +{ + Vector local_start(start.x-pos.x, start.y-pos.y, start.z); + float c = cos(dir); + float s = sin(dir); + local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z); + Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z); + + float d = -local_start.z/local_ray.z; + if(d<0) + return false; + + Vector base(local_start.x+d*local_ray.x, local_start.y+d*local_ray.y); + + if(radius) + { + base.y -= radius; + if(radius<0) + base.y = -base.y; + float r = sqrt(base.x*base.x+base.y*base.y)-abs(radius); + float a = atan2(base.x, -base.y); + return (a>=0 && a<=length && r>=-width/2 && r<=width/2); + } + else + return (base.x>=0 && base.x<=length && base.y>=-width/2 && base.y<=width/2); +} + TrackPart::Loader::Loader(TrackPart &p): Msp::DataFile::BasicLoader(p)