for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
if(!(*i)->get_type().is_turnout())
{
- TrackPoint n = (*i)->get_nearest_point(p);
- float d = distance(p, n.pos);
+ Snap sn;
+ sn.position = p;
+ sn.rotation = rotation;
+ (*i)->snap(sn, 1000, SNAP_SEGMENT);
+ float d = distance(p, sn.position);
if(d<dist || dist<0)
{
- position = n.pos;
- rotation = n.dir;
+ position = sn.position;
+ rotation = sn.rotation;
track = *i;
dist = d;
}
unsigned n_endpoints = track->get_type().get_endpoints().size();
for(unsigned j=0; j<n_endpoints; ++j)
{
- float a = track->get_endpoint_direction(j)-rotation;
- while(a<-M_PI/2)
- a += M_PI*2;
- while(a>M_PI*3/2)
- a -= M_PI*2;
- if(a>=M_PI/2)
+ Angle a = wrap_with_base(track->get_snap_node(j).rotation-rotation, -Angle::quarter_turn());
+ if(a>=Angle::quarter_turn())
{
BlockIter biter = TrackIter(track, j).block_iter();
entry = biter.entry();
}
}
-void Signal::set_rotation(float r)
+void Signal::set_rotation(const Angle &r)
{
- float a = rotation-r;
- while(a>M_PI*3/2)
- a -= M_PI*2;
- while(a<-M_PI/2)
- a += M_PI*2;
- if(a>=M_PI/2)
- {
- rotation += M_PI;
- if(rotation>M_PI*2)
- rotation -= M_PI*2;
- }
+ Angle a = wrap_with_base(rotation-r, -Angle::quarter_turn());
+ if(a>=Angle::quarter_turn())
+ rotation = wrap_positive(rotation+Angle::half_turn());
normalize_location();
}
-bool Signal::collide_ray(const Vector &start, const Vector &ray) const
+unsigned Signal::get_n_snap_nodes() const
+{
+ return 1;
+}
+
+Snap Signal::get_snap_node(unsigned i) const
{
- // XXX Totally hardcoded stuff, should be replaced with a geometry system
- Vector center = position;
- center.x += sin(rotation)*0.035;
- center.y -= cos(rotation)*0.035;
- Vector d(center.x-start.x, center.y-start.y);
- float x = (d.x*ray.x+d.y*ray.y)/(ray.x*ray.x+ray.y*ray.y);
- Vector nearest(start.x+ray.x*x-center.x, start.y+ray.y*x-center.y, start.z+ray.z*x-center.z);
- if(nearest.z<0|| nearest.z>0.12)
- return false;
- return nearest.x*nearest.x+nearest.y*nearest.y<0.0001;
+ if(i>=1)
+ throw out_of_range("Signal::get_snap_node");
+
+ Snap sn;
+ sn.position = position;
+ sn.rotation = rotation;
+ return sn;
+}
+
+SnapType Signal::get_default_snap_type_to(const Object &other) const
+{
+ if(dynamic_cast<const Track *>(&other))
+ return SNAP_SEGMENT;
+
+ return NO_SNAP;
}
void Signal::tick(const Time::TimeDelta &)
void Signal::save(list<DataFile::Statement> &st) const
{
st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
- st.push_back((DataFile::Statement("rotation"), rotation));
+ st.push_back((DataFile::Statement("rotation"), rotation.radians()));
if(address)
st.push_back((DataFile::Statement("address"), address));
}
void Signal::Loader::rotation(float d)
{
- obj.set_rotation(d);
+ obj.set_rotation(Angle::from_radians(d));
}
} // namespace R2C2