9 TrackPart::TrackPart():
20 float TrackPart::get_length() const
23 return abs(radius)*length;
28 TrackPoint TrackPart::get_point(float d) const
37 float rx = radius*sin(dir);
38 float ry = -radius*cos(dir);
39 result.pos = Vector(pos.x+c*rx-s*ry-rx, pos.y+c*ry+s*rx-ry);
44 result.pos = Vector(pos.x+cos(dir)*d, pos.y+sin(dir)*d);
51 void TrackPart::check_link(TrackPart &other)
53 unsigned n_eps = (dead_end ? 1 : 2);
54 unsigned n_other_eps = (other.is_dead_end() ? 1 : 2);
55 for(unsigned i=0; i<n_eps; ++i)
57 TrackPoint p1 = get_point(i ? get_length() : 0);
58 for(unsigned j=0; j<n_other_eps; ++j)
60 TrackPoint p2 = other.get_point(j ? other.get_length() : 0);
62 float dx = p2.pos.x-p1.pos.x;
63 float dy = p2.pos.y-p1.pos.y;
65 float da = p2.dir-p1.dir+M_PI*((i+j+1)%2);
71 if(dx*dx+dy*dy<1e-6 && da>=-0.01 && da<=0.01)
74 other.links[j] = this;
81 TrackPart *TrackPart::get_link(unsigned i) const
84 throw out_of_range("TrackPart::get_link");
88 bool TrackPart::collide_ray(const Vector &start, const Vector &ray, float width) const
90 Vector local_start(start.x-pos.x, start.y-pos.y, start.z);
93 local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z);
94 Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z);
96 float d = -local_start.z/local_ray.z;
100 Vector base(local_start.x+d*local_ray.x, local_start.y+d*local_ray.y);
107 float r = sqrt(base.x*base.x+base.y*base.y)-abs(radius);
108 float a = atan2(base.x, -base.y);
109 return (a>=0 && a<=length && r>=-width/2 && r<=width/2);
112 return (base.x>=0 && base.x<=length && base.y>=-width/2 && base.y<=width/2);
116 TrackPart::Loader::Loader(TrackPart &p):
117 Msp::DataFile::ObjectLoader<TrackPart>(p)
119 add("start", &Loader::start);
120 add("length", &TrackPart::length);
121 add("radius", &TrackPart::radius);
122 add("path", &TrackPart::path);
123 add("dead_end", &TrackPart::dead_end);
126 void TrackPart::Loader::finish()
130 obj.length *= M_PI/180;
141 void TrackPart::Loader::start(float x, float y, float d)
143 obj.pos = Vector(x, y);