fixed bug on ghost collision and added a more elegant way to lose the plane on water

master
Alexsandro Percy 2022-05-04 09:12:23 -03:00
parent 4ad212f50a
commit 52202cecc9
3 changed files with 85 additions and 36 deletions

View File

@ -6,13 +6,6 @@ local abs = math.abs
function pa28.physics(self)
local friction = 0.99
local vel=self.object:get_velocity()
-- dumb friction
if self.isonground and not self.isinliquid then
vel = {x=vel.x*friction,
y=vel.y,
z=vel.z*friction}
self.object:set_velocity(vel)
end
-- bounciness
if self.springiness and self.springiness > 0 then
@ -37,6 +30,78 @@ function pa28.physics(self)
self.object:set_velocity(vnew)
end
--buoyancy
local surface = nil
local surfnodename = nil
local spos = mobkit.get_stand_pos(self)
spos.y = spos.y+0.01
-- get surface height
local snodepos = mobkit.get_node_pos(spos)
local surfnode = mobkit.nodeatpos(spos)
while surfnode and (surfnode.drawtype == 'liquid' or surfnode.drawtype == 'flowingliquid') do
surfnodename = surfnode.name
surface = snodepos.y +0.5
if surface > spos.y+self.height then break end
snodepos.y = snodepos.y+1
surfnode = mobkit.nodeatpos(snodepos)
end
local new_velocity = nil
self.isinliquid = surfnodename
if surface then -- standing in liquid
self.isinliquid = true
end
local accell = {x=0, y=0, z=0}
self.water_drag = 0.1
if self.isinliquid then
local height = self.height
local submergence = min(surface-spos.y,height)/height
-- local balance = self.buoyancy*self.height
local buoyacc = mobkit.gravity*(self.buoyancy-submergence)
--[[mobkit.set_acceleration(self.object,
{x=-vel.x*self.water_drag,y=buoyacc-vel.y*abs(vel.y)*0.4,z=-vel.z*self.water_drag})]]--
accell = {x=-vel.x*self.water_drag,y=buoyacc-(vel.y*abs(vel.y)*0.4),z=-vel.z*self.water_drag}
--local v_accell = {x=0,y=buoyacc-(vel.y*abs(vel.y)*0.4),z=0}
--mobkit.set_acceleration(self.object,v_accell)
new_velocity = vector.add(vel, vector.multiply(accell, self.dtime))
else
mobkit.set_acceleration(self.object,{x=0,y=0,z=0})
self.isinliquid = false
new_velocity = vector.add(vel, {x=0,y=mobkit.gravity * self.dtime,z=0})
--self.object:set_velocity(new_velocity)
end
new_velocity = vector.add(new_velocity, vector.multiply(self._last_accell, self.dtime))
--[[
new_velocity correction
under some circunstances the velocity exceeds the max value accepted by set_velocity and
the game crashes with an overflow, so limiting the max velocity in each axis prevents the crash
]]--
local max_factor = 55
local vel_adjusted = 40
if new_velocity.x > max_factor then new_velocity.x = vel_adjusted end
if new_velocity.x < -max_factor then new_velocity.x = -vel_adjusted end
if new_velocity.z > max_factor then new_velocity.z = vel_adjusted end
if new_velocity.z < -max_factor then new_velocity.z = -vel_adjusted end
if new_velocity.y > max_factor then new_velocity.y = vel_adjusted end
if new_velocity.y < -max_factor then new_velocity.y = -vel_adjusted end
-- end correction
self.object:set_pos(self.object:get_pos())
-- dumb friction
if self.isonground and not self.isinliquid then
self.object:set_velocity({x=new_velocity.x*friction,
y=new_velocity.y,
z=new_velocity.z*friction})
else
if pa28.mode == 1 then
self.object:set_velocity(new_velocity)
end
end
if pa28.mode == 2 then
self.object:set_acceleration({x=0,y=mobkit.gravity,z=0})

View File

@ -159,6 +159,7 @@ minetest.register_entity("pa28:pa28", {
show_on_minimap = true,
springiness = 0.1,
physics = pa28.physics,
buoyancy = 1.02,
_passenger = nil,
_color = "#0063b0",
_rudder_angle = 0,

View File

@ -245,12 +245,14 @@ function pa28.destroy(self)
end
function pa28.testImpact(self, velocity, position)
if self._last_accell == nil then return end
local p = position --self.object:get_pos()
local collision = false
local low_node_pos = -2.0
if self._last_vel == nil then return end
--lets calculate the vertical speed, to avoid the bug on colliding on floor with hard lag
if abs(velocity.y - self._last_vel.y) > 2 then
local noded = mobkit.nodeatpos(mobkit.pos_shift(p,{y=-1.5}))
local noded = mobkit.nodeatpos(mobkit.pos_shift(p,{y=low_node_pos}))
if (noded and noded.drawtype ~= 'airlike') then
collision = true
else
@ -266,7 +268,7 @@ function pa28.testImpact(self, velocity, position)
end
if impact > 1.2 and self._longit_speed > 3 then
local noded = mobkit.nodeatpos(mobkit.pos_shift(p,{y=-1.5}))
local noded = mobkit.nodeatpos(mobkit.pos_shift(p,{y=low_node_pos}))
if (noded and noded.drawtype ~= 'airlike') then
minetest.sound_play("pa28_touch", {
--to_player = self.driver_name,
@ -515,7 +517,7 @@ function pa28.flightstep(self)
local accel = vector.add(longit_drag,later_drag)
local stop = false
local node_bellow = mobkit.nodeatpos(mobkit.pos_shift(curr_pos,{y=-1.51}))
local node_bellow = mobkit.nodeatpos(mobkit.pos_shift(curr_pos,{y=-2.0}))
local is_flying = true
if node_bellow and node_bellow.drawtype ~= 'airlike' then is_flying = false end
--if is_flying then minetest.chat_send_all('is flying') end
@ -546,7 +548,10 @@ function pa28.flightstep(self)
end
if longit_speed == 0 and is_flying == false and is_attached == false and self._engine_running == false then
if pa28.mode == 1 then self.object:set_velocity(vector.add(velocity, {x=0,y=mobkit.gravity * self.dtime,z=0})) end
if pa28.mode == 1 then
self.object:move_to(curr_pos)
self.object:set_acceleration({x=0,y=mobkit.gravity,z=0})
end
return
end
@ -648,6 +653,7 @@ function pa28.flightstep(self)
--lets apply some bob in water
if self.isinliquid then
self._engine_running = false
local bob = pa28.minmax(pa28.dot(accel,hull_direction),0.2) -- vertical bobbing
accel.y = accel.y + bob
local max_pitch = 6
@ -671,28 +677,7 @@ function pa28.flightstep(self)
pa28.attach(self, player, false)
end
if pa28.mode == 1 then
local gravity_velocity = {x=0,y=mobkit.gravity * self.dtime,z=0}
local new_velocity = vector.add(velocity, vector.multiply(new_accel, self.dtime))
new_velocity = vector.add(gravity_velocity, new_velocity)
--[[
new_velocity correction
under some circunstances the velocity exceeds the max value accepted by set_velocity and
the game crashes with an overflow, so limiting the max velocity in each axis prevents the crash
]]--
local max_factor = 55
local vel_adjusted = 40
if new_velocity.x > max_factor then new_velocity.x = vel_adjusted end
if new_velocity.x < -max_factor then new_velocity.x = -vel_adjusted end
if new_velocity.z > max_factor then new_velocity.z = vel_adjusted end
if new_velocity.z < -max_factor then new_velocity.z = -vel_adjusted end
if new_velocity.y > max_factor then new_velocity.y = vel_adjusted end
if new_velocity.y < -max_factor then new_velocity.y = -vel_adjusted end
--minetest.chat_send_all(dump(new_velocity))
self.object:set_velocity(new_velocity)
end
--for mode==1, see at custom_physics
if pa28.mode == 2 then
self.object:move_to(curr_pos)
mobkit.set_acceleration(self.object, new_accel)
@ -715,9 +700,6 @@ function pa28.flightstep(self)
--stop wheels
self.object:set_animation_frame_speed(0)
end
------------------------------------------------------
-- end accell
------------------------------------------------------
@ -847,3 +829,4 @@ function pa28.flightstep(self)
self._last_vel = self.object:get_velocity()
end