9 #include "vehicletype.h"
16 Vehicle::Vehicle(Layout &l, const VehicleType &t):
26 axles.assign(type.get_axles().begin(), type.get_axles().end());
27 for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
29 fixed_axles.push_back(&*i);
30 bogies.assign(type.get_bogies().begin(), type.get_bogies().end());
31 rods.assign(type.get_rods().begin(), type.get_rods().end());
32 for(vector<Bogie>::iterator i=bogies.begin(); i!=bogies.end(); ++i)
33 for(unsigned j=0; j<i->axles.size(); ++j)
34 i->axles[j] = &axles[i->type->first_axle+j];
48 Vehicle *Vehicle::clone(Layout *to_layout) const
50 Vehicle *veh = new Vehicle((to_layout ? *to_layout : layout), type);
51 veh->set_position(position);
52 veh->set_rotation(rotation);
56 void Vehicle::set_train(Train *t)
61 void Vehicle::attach_back(Vehicle &veh)
64 throw attachment_error("already attached");
73 void Vehicle::attach_front(Vehicle &veh)
76 throw attachment_error("already attached");
82 prev->propagate_backward();
85 void Vehicle::detach_back()
88 throw attachment_error("not attached");
94 void Vehicle::detach_front()
97 throw attachment_error("not attached");
103 void Vehicle::place(const TrackOffsetIter &t, VehiclePlacement::Anchor a)
106 throw invalid_argument("Vehicle::place");
108 placement.place(t, a);
111 propagate_position();
114 void Vehicle::unplace()
116 if(!placement.is_placed())
127 void Vehicle::advance(float d)
129 placement.advance(d);
131 update_position(d<0 ? -1 : 1);
132 propagate_position();
135 const Vehicle::Axle &Vehicle::get_axle(unsigned i) const
138 throw out_of_range("Vehicle::get_axle");
142 const Vehicle::Axle &Vehicle::get_fixed_axle(unsigned i) const
144 if(i>=fixed_axles.size())
145 throw out_of_range("Vehicle::get_fixed_axle");
146 return *fixed_axles[i];
149 const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const
152 throw out_of_range("Vehicle::get_bogie");
156 const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const
159 throw out_of_range("Vehicle::get_bogie_axle");
160 if(j>=bogies[i].axles.size())
161 throw out_of_range("Vehicle::get_bogie_axle");
162 return *bogies[i].axles[j];
165 const Vehicle::Rod &Vehicle::get_rod(unsigned i) const
168 throw out_of_range("Vehicle::get_rod");
172 void Vehicle::update_position(int sign)
174 OrientedPoint p = placement.get_point();
175 position = p.position;
176 position.z += layout.get_catalogue().get_rail_elevation();
177 rotation = p.rotation;
182 OrientedPoint front_point = placement.get_bogie_point(bogies.front().type->index);
183 bogies.front().direction = front_point.rotation-p.rotation;
185 OrientedPoint back_point = placement.get_bogie_point(bogies.back().type->index);
186 bogies.back().direction = back_point.rotation-p.rotation;
190 check_sensor(placement.get_position(VehiclePlacement::FRONT_AXLE), front_sensor, sign<0);
192 check_sensor(placement.get_position(VehiclePlacement::BACK_AXLE), back_sensor, sign>0);
197 void Vehicle::update_position_from(const Vehicle &veh)
199 int sign = (&veh==prev ? -1 : 1);
201 float tdist = (type.get_length()+veh.type.get_length())/2;
202 float margin = layout.get_catalogue().get_scale();
204 float dist = distance(veh.position, position);
205 if(!is_placed() || dist<tdist-margin || dist>tdist+margin)
208 placement.place_after(veh.placement);
210 placement.place_before(veh.placement);
213 dist = distance(veh.position, position);
216 float d = sign*(tdist-dist);
217 placement.advance(d);
218 update_position(d<0 ? -1 : 1);
222 void Vehicle::propagate_position()
227 propagate_backward();
230 void Vehicle::propagate_forward()
232 prev->update_position_from(*this);
235 prev->propagate_forward();
238 void Vehicle::propagate_backward()
240 next->update_position_from(*this);
243 next->propagate_backward();
246 void Vehicle::check_sensor(const TrackOffsetIter &t, unsigned &sensor, bool release)
248 unsigned s = t->get_sensor_id();
251 unsigned old = sensor;
254 layout.get_driver().set_sensor(old, false);
256 layout.get_driver().set_sensor(sensor, true);
260 void Vehicle::turn_axles(float d)
262 for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
263 i->angle += Angle::from_radians(d*2/i->type->wheel_dia);
268 void Vehicle::update_rods()
270 for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
272 if(i->type->pivot==VehicleType::Rod::BODY)
273 i->position = i->type->pivot_point;
274 else if(i->type->pivot==VehicleType::Rod::AXLE)
276 const Axle &axle = get_fixed_axle(i->type->pivot_index);
277 const Vector &pp = i->type->pivot_point;
278 Transform trans = Transform::rotation(axle.angle, Vector(0, -1, 0));
279 i->position = Vector(axle.type->position, 0, axle.type->wheel_dia/2)+trans.transform(pp);
281 else if(i->type->pivot==VehicleType::Rod::ROD)
283 const Rod &prod = get_rod(i->type->pivot_index);
284 const Vector &pos = prod.position;
285 const Vector &off = i->type->pivot_point;
286 Transform trans = Transform::rotation(prod.angle, Vector(0, 1, 0));
287 i->position = pos+trans.transform(off);
290 if(i->type->connect_index>=0)
292 Rod &crod = rods[i->type->connect_index];
293 if(i->type->limit==VehicleType::Rod::ROTATE && crod.type->limit==VehicleType::Rod::SLIDE_X)
295 Vector span = crod.position+i->type->connect_offset-i->position;
296 float cd = i->type->connect_point.norm();
297 Angle ca = Geometry::atan2(i->type->connect_point.z, i->type->connect_point.x);
298 span.x = sqrt(cd*cd-span.z*span.z)*(span.x>0 ? 1 : -1);
299 i->angle = Geometry::atan2(span.z, span.x)-ca;
300 crod.position.x = i->position.x+span.x-i->type->connect_offset.x;
302 else if(i->type->limit==VehicleType::Rod::ROTATE && crod.type->limit==VehicleType::Rod::ROTATE)
304 Vector span = crod.position-i->position;
305 float d = span.norm();
306 float cd1 = i->type->connect_point.norm();
307 float cd2 = i->type->connect_offset.norm();
308 float a = (d*d+cd1*cd1-cd2*cd2)/(2*d);
309 float b = sqrt(cd1*cd1-a*a);
310 float sign = (cross(i->type->connect_point, span).y>0 ? 1 : -1);
311 Vector conn = Vector(span.x*a-span.z*b, 0, span.z*a+span.x*b)/(d*sign);
312 Angle ca1 = Geometry::atan2(i->type->connect_point.z, i->type->connect_point.x);
313 Angle ca2 = Geometry::atan2(i->type->connect_offset.z, i->type->connect_offset.x);
314 i->angle = Geometry::atan2(conn.z, conn.x)-ca1;
315 crod.angle = Geometry::atan2(conn.z-span.z, conn.x-span.x)-ca2;
321 unsigned Vehicle::get_n_link_slots() const
326 Vehicle *Vehicle::get_link(unsigned i) const
329 throw out_of_range("Vehicle::get_link");
331 return (i==0 ? prev : next);
334 int Vehicle::get_link_slot(const Object &other) const
338 else if(&other==next)
345 Vehicle::Axle::Axle(const VehicleType::Axle &t):
350 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
352 axles(t.axles.size())
356 Vehicle::Rod::Rod(const VehicleType::Rod &t):