3 This file is part of R²C²
4 Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
13 #include "trackiter.h"
14 #include "tracktype.h"
16 #include "vehicletype.h"
23 Vehicle::Vehicle(Layout &l, const VehicleType &t):
32 axles.assign(type.get_axles().begin(), type.get_axles().end());
33 bogies.assign(type.get_bogies().begin(), type.get_bogies().end());
34 rods.assign(type.get_rods().begin(), type.get_rods().end());
36 layout.add_vehicle(*this);
45 layout.remove_vehicle(*this);
48 void Vehicle::attach_back(Vehicle &veh)
51 throw InvalidState("Already attached");
60 void Vehicle::attach_front(Vehicle &veh)
63 throw InvalidState("Already attached");
69 prev->propagate_backward();
72 void Vehicle::detach_back()
75 throw InvalidState("Not attached");
81 void Vehicle::detach_front()
84 throw InvalidState("Not attached");
90 void Vehicle::place(Track &t, unsigned e, float o, PlaceMode m)
92 track_pos = TrackPosition(&t, e, o);
95 track_pos.advance(-type.get_front_axle_offset());
96 else if(m==FRONT_BUFFER)
97 track_pos.advance(-type.get_length()/2);
99 track_pos.advance(-type.get_back_axle_offset());
100 else if(m==BACK_BUFFER)
101 track_pos.advance(type.get_length()/2);
104 propagate_position();
107 void Vehicle::unplace()
112 track_pos = TrackPosition();
120 void Vehicle::advance(float d)
122 track_pos.advance(d);
125 propagate_position();
128 const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const
131 throw InvalidParameterValue("Bogie index out of range");
135 const Vehicle::Axle &Vehicle::get_axle(unsigned i) const
138 throw InvalidParameterValue("Axle index out of range");
142 const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const
145 throw InvalidParameterValue("Bogie index out of range");
146 if(j>=bogies[i].axles.size())
147 throw InvalidParameterValue("Axle index out of range");
148 return bogies[i].axles[j];
151 const Vehicle::Rod &Vehicle::get_rod(unsigned i) const
154 throw InvalidParameterValue("Rod index out of range");
158 void Vehicle::update_position()
164 float wheelbase = axles.front().type->position-axles.back().type->position;
165 tp = get_point(track_pos, wheelbase, -axles.back().type->position/wheelbase);
167 else if(bogies.size()>=2)
169 TrackPosition front = track_pos;
170 front.advance(bogies.front().type->position);
171 TrackPosition back = track_pos;
172 back.advance(bogies.back().type->position);
173 float bogie_spacing = bogies.front().type->position-bogies.back().type->position;
174 adjust_for_distance(front, back, bogie_spacing);
176 const vector<Axle> &front_axles = bogies.front().axles;
177 float wheelbase = front_axles.front().type->position-front_axles.back().type->position;
178 TrackPoint front_point = get_point(front, wheelbase, -front_axles.back().type->position/wheelbase);
180 const vector<Axle> &back_axles = bogies.back().axles;
181 wheelbase = back_axles.front().type->position-back_axles.back().type->position;
182 TrackPoint back_point = get_point(back, wheelbase, -back_axles.back().type->position/wheelbase);
184 tp = get_point(front_point.pos, back_point.pos, -bogies.back().type->position/bogie_spacing);
186 bogies.front().direction = front_point.dir-tp.dir;
187 bogies.back().direction = back_point.dir-tp.dir;
190 tp = track_pos.get_point();
193 check_sensor(type.get_front_axle_offset(), front_sensor);
195 check_sensor(type.get_back_axle_offset(), back_sensor);
198 position.z += layout.get_catalogue().get_rail_elevation();
202 void Vehicle::update_position_from(const Vehicle &veh)
204 int sign = (&veh==prev ? -1 : 1);
206 float tdist = (type.get_length()+veh.type.get_length())/2;
207 float margin = layout.get_catalogue().get_scale();
209 float dist = distance(veh.position, position);
210 if(dist<tdist-margin || dist>tdist+margin)
212 track_pos = veh.track_pos;
213 track_pos.advance(sign*tdist);
216 dist = distance(veh.position, position);
219 track_pos.advance(sign*(tdist-dist));
221 turn_axles(sign*(tdist-dist));
224 void Vehicle::propagate_position()
229 propagate_backward();
232 void Vehicle::propagate_forward()
234 prev->update_position_from(*this);
237 prev->propagate_forward();
240 void Vehicle::propagate_backward()
242 next->update_position_from(*this);
245 next->propagate_backward();
248 void Vehicle::check_sensor(float offset, unsigned &sensor)
250 TrackPosition pos = track_pos;
252 unsigned s = pos.track->get_sensor_id();
255 /* Sensor ID under axle has changed. Deduce movement direction by using
256 the sensor ID under the midpoint of the vehicle. */
257 /* XXX This depends on the simulation running fast enough. Something
258 more robust would be preferable. */
259 unsigned old = sensor;
261 unsigned mid = track_pos.track->get_sensor_id();
264 /* There's a sensor and it's different from mid. We've just entered
266 layout.get_driver().set_sensor(sensor, true);
268 /* A sensor was under the axle and it was different from mid. We've
269 just left that sensor. */
270 layout.get_driver().set_sensor(old, false);
274 void Vehicle::turn_axles(float d)
276 for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
277 i->angle += d*2/i->type->wheel_dia;
278 for(vector<Bogie>::iterator i=bogies.begin(); i!=bogies.end(); ++i)
279 for(vector<Axle>::iterator j=i->axles.begin(); j!=i->axles.end(); ++j)
280 j->angle += d*2/j->type->wheel_dia;
285 void Vehicle::update_rods()
287 for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
289 if(i->type->pivot==VehicleType::Rod::BODY)
290 i->position = i->type->pivot_point;
291 else if(i->type->pivot==VehicleType::Rod::AXLE)
293 const Axle &axle = get_axle(i->type->pivot_index);
294 float c = cos(axle.angle);
295 float s = sin(axle.angle);
296 const Vector &pp = i->type->pivot_point;
297 i->position = Vector(axle.type->position+pp.x*c+pp.z*s, pp.y, axle.type->wheel_dia/2+pp.z*c-pp.x*s);
299 else if(i->type->pivot==VehicleType::Rod::ROD)
301 const Rod &prod = get_rod(i->type->pivot_index);
302 float c = cos(prod.angle);
303 float s = sin(prod.angle);
304 const Vector &pos = prod.position;
305 const Vector &off = i->type->pivot_point;
306 i->position = Vector(pos.x+off.x*c-off.z*s, pos.y+off.y, pos.z+off.z*c+off.x*s);
309 if(i->type->connect_index>=0)
311 Rod &crod = rods[i->type->connect_index];
312 if(i->type->limit==VehicleType::Rod::ROTATE && crod.type->limit==VehicleType::Rod::SLIDE_X)
314 float dx = (crod.position.x+i->type->connect_offset.x)-i->position.x;
315 float dz = (crod.position.z+i->type->connect_offset.z)-i->position.z;
316 float cd = sqrt(i->type->connect_point.x*i->type->connect_point.x+i->type->connect_point.z*i->type->connect_point.z);
317 float ca = atan2(i->type->connect_point.z, i->type->connect_point.x);
318 dx = sqrt(cd*cd-dz*dz)*(dx>0 ? 1 : -1);
319 i->angle = atan2(dz, dx)-ca;
320 crod.position.x = i->position.x+dx-i->type->connect_offset.x;
322 else if(i->type->limit==VehicleType::Rod::ROTATE && crod.type->limit==VehicleType::Rod::ROTATE)
324 float dx = crod.position.x-i->position.x;
325 float dz = crod.position.z-i->position.z;
326 float d = sqrt(dx*dx+dz*dz);
327 float cd1 = sqrt(i->type->connect_point.x*i->type->connect_point.x+i->type->connect_point.z*i->type->connect_point.z);
328 float cd2 = sqrt(i->type->connect_offset.x*i->type->connect_offset.x+i->type->connect_offset.z*i->type->connect_offset.z);
329 float a = (d*d+cd1*cd1-cd2*cd2)/(2*d);
330 float b = sqrt(cd1*cd1-a*a);
331 float sign = (dx*i->type->connect_point.z-dz*i->type->connect_point.x>0 ? 1 : -1);
332 float cx = (dx*a-dz*b*sign)/d;
333 float cz = (dz*a+dx*b*sign)/d;
334 float ca1 = atan2(i->type->connect_point.z, i->type->connect_point.x);
335 float ca2 = atan2(i->type->connect_offset.z, i->type->connect_offset.x);
336 i->angle = atan2(cz, cx)-ca1;
337 crod.angle = atan2(cz-dz, cx-dx)-ca2;
343 void Vehicle::adjust_for_distance(TrackPosition &front, TrackPosition &back, float tdist, float ratio) const
345 float margin = 0.01*layout.get_catalogue().get_scale();
349 Vector front_point = front.get_point().pos;
350 Vector back_point = back.get_point().pos;
352 float dx = front_point.x-back_point.x;
353 float dy = front_point.y-back_point.y;
354 float dz = front_point.z-back_point.z;
355 float dist = sqrt(dx*dx+dy*dy+dz*dz);
357 float diff = tdist-dist;
358 if(diff<-margin && adjust_dir<=0)
363 else if(diff>margin && adjust_dir>=0)
371 front.advance(diff*(1-ratio));
372 back.advance(-diff*ratio);
376 TrackPoint Vehicle::get_point(const Vector &front, const Vector &back, float ratio) const
378 float dx = front.x-back.x;
379 float dy = front.y-back.y;
380 float dz = front.z-back.z;
383 tp.pos = Vector(back.x+dx*ratio, back.y+dy*ratio, back.z+dz*ratio);
384 tp.dir = atan2(dy, dx);
389 TrackPoint Vehicle::get_point(const TrackPosition &pos, float tdist, float ratio) const
391 TrackPosition front = pos;
392 front.advance(tdist*(1-ratio));
394 TrackPosition back = pos;
395 back.advance(-tdist*ratio);
397 adjust_for_distance(front, back, tdist, ratio);
398 return get_point(front.get_point().pos, back.get_point().pos, ratio);
402 Vehicle::Axle::Axle(const VehicleType::Axle &t):
408 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
412 for(VehicleType::AxleArray::const_iterator i=type->axles.begin(); i!=type->axles.end(); ++i)
417 Vehicle::Rod::Rod(const VehicleType::Rod &t):
423 Vehicle::TrackPosition::TrackPosition():
429 Vehicle::TrackPosition::TrackPosition(Track *t, unsigned e, float o):
435 void Vehicle::TrackPosition::advance(float d)
441 TrackIter iter(track, ep);
444 float path_len = iter->get_type().get_path_length(iter->get_active_path());
455 while(iter && offs<0)
457 iter = iter.flip().reverse();
461 float path_len = iter->get_type().get_path_length(iter->get_active_path());
466 track = iter.track();
472 TrackPoint Vehicle::TrackPosition::get_point() const
475 return track->get_point(ep, offs);