- float sign = (dx*i->type->connect_point.z-dz*i->type->connect_point.x>0 ? 1 : -1);
- float cx = (dx*a-dz*b*sign)/d;
- float cz = (dz*a+dx*b*sign)/d;
- float ca1 = atan2(i->type->connect_point.z, i->type->connect_point.x);
- float ca2 = atan2(i->type->connect_offset.z, i->type->connect_offset.x);
- i->angle = atan2(cz, cx)-ca1;
- crod.angle = atan2(cz-dz, cx-dx)-ca2;
+ float sign = (cross(i->type->connect_point, span).y>0 ? 1 : -1);
+ Vector conn = Vector(span.x*a-span.z*b, 0, span.z*a+span.x*b)/(d*sign);
+ Angle ca1 = Geometry::atan2(i->type->connect_point.z, i->type->connect_point.x);
+ Angle ca2 = Geometry::atan2(i->type->connect_offset.z, i->type->connect_offset.x);
+ i->angle = Geometry::atan2(conn.z, conn.x)-ca1;
+ crod.angle = Geometry::atan2(conn.z-span.z, conn.x-span.x)-ca2;