From 8b400329a03502b469f50bd30b2a8ae0429573ef Mon Sep 17 00:00:00 2001 From: orwell96 Date: Thu, 29 Sep 2016 08:41:05 +0200 Subject: [PATCH] add acceleration calculation --- trainlogic.lua | 5 ++++- wagons.lua | 13 +++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/trainlogic.lua b/trainlogic.lua index 6e2b3c8..0291e8f 100644 --- a/trainlogic.lua +++ b/trainlogic.lua @@ -318,7 +318,10 @@ function advtrains.train_step(id, train, dtime) applydiff=(math.min((advtrains.train_brake_force*dtime), math.abs(diff))) end end - train.velocity=train.velocity+(applydiff*math.sign(train.tarvelocity-train.velocity)) + train.last_accel=(applydiff*math.sign(train.tarvelocity-train.velocity)) + train.velocity=train.velocity+train.last_accel + else + train.last_accel=0 end --move diff --git a/wagons.lua b/wagons.lua index 304061b..540a882 100644 --- a/wagons.lua +++ b/wagons.lua @@ -302,9 +302,11 @@ function wagon:on_step(dtime) --FIX: use index of the wagon, not of the train. local velocity=gp.velocity/(gp.path_dist[math.floor(index)] or 1) + local acceleration=(gp.last_accel or 0)/(gp.path_dist[math.floor(index)] or 1) local factor=index-math.floor(index) local actual_pos={x=first_pos.x-(first_pos.x-second_pos.x)*factor, y=first_pos.y-(first_pos.y-second_pos.y)*factor, z=first_pos.z-(first_pos.z-second_pos.z)*factor,} local velocityvec={x=(first_pos.x-second_pos.x)*velocity*-1, z=(first_pos.z-second_pos.z)*velocity*-1, y=(first_pos.y-second_pos.y)*velocity*-1} + local accelerationvec={x=(first_pos.x-second_pos.x)*acceleration*-1, z=(first_pos.z-second_pos.z)*acceleration*-1, y=(first_pos.y-second_pos.y)*acceleration*-1} --some additional positions to determine orientation local aposfwd=gp.path[math.floor(index+2)] @@ -321,9 +323,15 @@ function wagon:on_step(dtime) end self.updatepct_timer=(self.updatepct_timer or 0)-dtime - if not self.old_velocity_vector or not vector.equals(velocityvec, self.old_velocity_vector) or self.old_yaw~=yaw or self.updatepct_timer<=0 then--only send update packet if something changed - self.object:setpos(actual_pos) + if not self.old_velocity_vector + or not vector.equals(velocityvec, self.old_velocity_vector) + or not self.old_acceleration_vector + or not vector.equals(accelerationvec, self.old_acceleration_vector) + or self.old_yaw~=yaw + or self.updatepct_timer<=0 then--only send update packet if something changed + self.object:setpos(actual_pos) self.object:setvelocity(velocityvec) + self.object:setacceleration(accelerationvec) self.object:setyaw(yaw) self.updatepct_timer=2 if self.update_animation then @@ -332,6 +340,7 @@ function wagon:on_step(dtime) end self.old_velocity_vector=velocityvec + self.old_acceleration_vector=accelerationvec self.old_yaw=yaw printbm("wagon step", t) end