]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/signal.cpp
Make use of the mspmath library
[r2c2.git] / source / libr2c2 / signal.cpp
index 650c36232e87d82559fc36cfbb6aba2bc160ab55..3494467d84daf3c166ccbf851742712987b72d8d 100644 (file)
@@ -80,12 +80,8 @@ void Signal::normalize_location()
        unsigned n_endpoints = track->get_type().get_endpoints().size();
        for(unsigned j=0; j<n_endpoints; ++j)
        {
-               float a = track->get_snap_node(j).rotation-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();
@@ -93,19 +89,11 @@ void Signal::normalize_location()
        }
 }
 
-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();
 }
@@ -137,15 +125,14 @@ SnapType Signal::get_default_snap_type_to(const Object &other) const
 bool Signal::collide_ray(const Vector &start, const Vector &ray) 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)
+       Vector center = position+rotated_vector(Vector(0, -0.035, 0), rotation);
+       Vector span = center-start;
+       float x = (span.x*ray.x+span.y*ray.y)/(ray.x*ray.x+ray.y*ray.y);
+       Vector nearest = start+ray*x-center;
+       if(nearest.z<0 || nearest.z>0.12)
                return false;
-       return nearest.x*nearest.x+nearest.y*nearest.y<0.0001;
+       nearest.z = 0;
+       return dot(nearest, nearest)<0.0001;
 }
 
 void Signal::tick(const Time::TimeDelta &)
@@ -225,7 +212,7 @@ void Signal::reset()
 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));
 }
@@ -251,7 +238,7 @@ void Signal::Loader::position(float x, float y, float z)
 
 void Signal::Loader::rotation(float d)
 {
-       obj.set_rotation(d);
+       obj.set_rotation(Angle::from_radians(d));
 }
 
 } // namespace R2C2