diff options
author | orwell96 <mono96.mml@gmail.com> | 2016-09-29 08:41:05 +0200 |
---|---|---|
committer | orwell96 <mono96.mml@gmail.com> | 2016-09-29 08:41:05 +0200 |
commit | 8b400329a03502b469f50bd30b2a8ae0429573ef (patch) | |
tree | aaa7ed203eb9f94efecf87c4b8ff0f2eab6260bd | |
parent | 76bd8eaabaf7a81d723e0db5d55004eadb343e92 (diff) | |
download | advtrains-8b400329a03502b469f50bd30b2a8ae0429573ef.tar.gz advtrains-8b400329a03502b469f50bd30b2a8ae0429573ef.tar.bz2 advtrains-8b400329a03502b469f50bd30b2a8ae0429573ef.zip |
add acceleration calculation
-rw-r--r-- | trainlogic.lua | 5 | ||||
-rw-r--r-- | 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 @@ -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
|