From a879ce8d3c179b5c6b88c9db610d560228d1e5b4 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Thu, 9 Jun 2016 17:35:06 +0200 Subject: Fix train collision and coupling on opposing trains, and try to implement animation (currently does not work) --- helpers.lua | 3 +++ models/engine-with-animation.blend | Bin 0 -> 599504 bytes models/newlocomotive.b3d | Bin 181112 -> 298724 bytes trainhud.lua | 4 ++-- trainlogic.lua | 8 +++++--- wagons.lua | 14 ++++++++++++++ 6 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 models/engine-with-animation.blend diff --git a/helpers.lua b/helpers.lua index 737d15b..cd67aa9 100644 --- a/helpers.lua +++ b/helpers.lua @@ -219,4 +219,7 @@ function advtrains.get_real_index_position(path, index) end function advtrains.pos_median(pos1, pos2) return {x=pos1.x-(pos1.x-pos2.x)*0.5, y=pos1.y-(pos1.y-pos2.y)*0.5, z=pos1.z-(pos1.z-pos2.z)*0.5} +end +function advtrains.abs_ceil(i) + return math.ceil(math.abs(i))*math.sign(i) end \ No newline at end of file diff --git a/models/engine-with-animation.blend b/models/engine-with-animation.blend new file mode 100644 index 0000000..2649587 Binary files /dev/null and b/models/engine-with-animation.blend differ diff --git a/models/newlocomotive.b3d b/models/newlocomotive.b3d index da36222..9043b70 100644 Binary files a/models/newlocomotive.b3d and b/models/newlocomotive.b3d differ diff --git a/trainhud.lua b/trainhud.lua index b64c81c..601b28f 100644 --- a/trainhud.lua +++ b/trainhud.lua @@ -33,8 +33,8 @@ function advtrains.hud_train_format(train, flip) if flip then fct=-1 end if not train then return "" end local max=advtrains.all_traintypes[train.traintype].max_speed or 10 - local vel=math.ceil(train.velocity)*fct - local tvel=math.ceil(train.tarvelocity)*fct + local vel=advtrains.abs_ceil(train.velocity)*fct + local tvel=advtrains.abs_ceil(train.tarvelocity)*fct local firstLine, secondLine if vel<0 then firstLine="Speed: <"..string.rep("_", vel+max)..string.rep("+", -vel).."|"..string.rep("_", max)..">" diff --git a/trainlogic.lua b/trainlogic.lua index de56d4e..f7a9363 100644 --- a/trainlogic.lua +++ b/trainlogic.lua @@ -574,16 +574,18 @@ function advtrains.try_connect_trains_and_check_collision(id1, id2) for i=(advtrains.get_train_end_index(train2)+0.5),train2.index-0.5 do local testpos=advtrains.get_real_index_position(train2.path,i) if vector.distance(testpos, backpos1) < 0.5 then + local v2_sign = math.sign(i - ((train2.index-0.5) - ( (train2.index-0.5)-(advtrains.get_train_end_index(train2)+0.5) / 2 ))) --TODO physics train1.velocity=1 - train2.velocity=-1 + train2.velocity=v2_sign train1.recently_collided_with_env=true train2.recently_collided_with_env=true return end if vector.distance(testpos, frontpos1) < 0.5 then + local v2_sign = math.sign(i - ((train2.index-0.5) - ( (train2.index-0.5)-(advtrains.get_train_end_index(train2)+0.5) / 2 ))) train1.velocity=-1 - train2.velocity=1 + train2.velocity=v2_sign train1.recently_collided_with_env=true train2.recently_collided_with_env=true return @@ -680,7 +682,7 @@ function advtrains.invert_train(train_id) local old_path=advtrains.get_or_create_path(train_id, train) train.path={} - train.index= - advtrains.get_train_end_index(train1) + train.index= - advtrains.get_train_end_index(train) train.velocity=-train.velocity train.tarvelocity=-train.tarvelocity for k,v in pairs(old_path) do diff --git a/wagons.lua b/wagons.lua index 31a07b2..69504a1 100644 --- a/wagons.lua +++ b/wagons.lua @@ -268,6 +268,9 @@ function wagon:on_step(dtime) self.object:setvelocity(velocityvec) self.object:setyaw(yaw) self.updatepct_timer=2 + if self.update_animation then + self:update_animation(gp.velocity) + end end self.old_velocity_vector=velocityvec @@ -336,6 +339,11 @@ advtrains.register_wagon("greenwagon", "steam",{textures = {"green.png"}}) advtrains.register_wagon("redwagon", "steam",{textures = {"red.png"}}) advtrains.register_wagon("yellowwagon", "steam",{textures = {"yellow.png"}}) ]] + +--[[ + wagons can define update_animation(self, velocity) if they have a speed-dependent animation + this function will be called when the velocity vector changes or every 2 seconds. +]] advtrains.register_wagon("newlocomotive", "steam",{ mesh="newlocomotive.b3d", textures = {"advtrains_newlocomotive.png"}, @@ -345,6 +353,12 @@ advtrains.register_wagon("newlocomotive", "steam",{ visual_size = {x=1, y=1}, wagon_span=1.85, collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0}, + update_animation=function(self, velocity) + if self.old_anim_velocity~=advtrains.abs_ceil(velocity) then + self.object:set_animation({x=1,y=60}, math.floor(velocity)) + self.old_anim_velocity=advtrains.abs_ceil(velocity) + end + end }) advtrains.register_wagon("wagon_default", "steam",{ mesh="wagon.b3d", -- cgit v1.2.3