changes to try to reduce the rubberband effect on versions above 5.8

This commit is contained in:
Alexsandro Percy 2024-11-16 10:32:39 -03:00
parent 075c1b0278
commit f5393b11e1
3 changed files with 162 additions and 56 deletions

View File

@ -0,0 +1,65 @@
local min = math.min
local abs = math.abs
function automobiles_lib.physics(self)
local friction = 0.99
local vel=self.object:get_velocity()
-- dumb friction
if self.isonground and not self.isinliquid then
--minetest.chat_send_all('okay')
self.object:add_velocity({x=vel.x*friction-vel.x,
y=0,
z=vel.z*friction-vel.z})
end
-- bounciness
if self.springiness and self.springiness > 0 then
local vnew = vector.new(vel)
if not self.collided then -- ugly workaround for inconsistent collisions
for _,k in ipairs({'y','z','x'}) do
if vel[k]==0 and abs(self.lastvelocity[k])> 0.1 then
vnew[k]=-self.lastvelocity[k]*self.springiness
end
end
end
if not vector.equals(vel,vnew) then
self.collided = true
else
if self.collided then
vnew = vector.new(self.lastvelocity)
end
self.collided = false
end
self.object:add_velocity(vector.subtract(vel, vnew))
end
-- buoyancy
local surface = nil
local surfnodename = nil
local spos = automobiles_lib.get_stand_pos(self)
spos.y = spos.y+0.01
-- get surface height
local snodepos = automobiles_lib.get_node_pos(spos)
local surfnode = automobiles_lib.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 = automobiles_lib.nodeatpos(snodepos)
end
self.isinliquid = surfnodename
--normal use
if surface then -- standing in liquid
-- self.isinliquid = true
local submergence = min(surface-spos.y,self.height)/self.height
-- local balance = self.buoyancy*self.height
local buoyacc = (automobiles_lib.gravity*-1)*(self.buoyancy-submergence)
automobiles_lib.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})
end
end

View File

@ -1,45 +1,13 @@
local min = math.min
local abs = math.abs
function automobiles_lib.physics(self)
local friction = 0.99
local friction = self._ground_friction or 0.99
local vel=self.object:get_velocity()
-- dumb friction
if self.isonground and not self.isinliquid then
--minetest.chat_send_all('okay')
self.object:add_velocity({x=vel.x*friction-vel.x,
y=0,
z=vel.z*friction-vel.z})
end
-- bounciness
if self.springiness and self.springiness > 0 then
local vnew = vector.new(vel)
if not self.collided then -- ugly workaround for inconsistent collisions
for _,k in ipairs({'y','z','x'}) do
if vel[k]==0 and abs(self.lastvelocity[k])> 0.1 then
vnew[k]=-self.lastvelocity[k]*self.springiness
end
end
end
if not vector.equals(vel,vnew) then
self.collided = true
else
if self.collided then
vnew = vector.new(self.lastvelocity)
end
self.collided = false
end
self.object:add_velocity(vector.subtract(vel, vnew))
end
-- buoyancy
local new_velocity = vector.new()
--buoyancy
local surface = nil
local surfnodename = nil
local spos = automobiles_lib.get_stand_pos(self)
if not spos then return end
spos.y = spos.y+0.01
-- get surface height
local snodepos = automobiles_lib.get_node_pos(spos)
@ -51,15 +19,93 @@ function automobiles_lib.physics(self)
snodepos.y = snodepos.y+1
surfnode = automobiles_lib.nodeatpos(snodepos)
end
self.isinliquid = surfnodename
--normal use
if surface then -- standing in liquid
-- self.isinliquid = true
local submergence = min(surface-spos.y,self.height)/self.height
-- local balance = self.buoyancy*self.height
local buoyacc = (automobiles_lib.gravity*-1)*(self.buoyancy-submergence)
automobiles_lib.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})
self.isinliquid = surfnodename
if surface then -- standing in liquid
self.isinliquid = true
end
local last_accel = vector.new()
if self._last_accel then
last_accel = vector.new(self._last_accel)
end
if self.isinliquid then
self.water_drag = 0.2
self.isinliquid = true
local height = self.height
local submergence = math.min(surface-spos.y,height)/height
-- local balance = self.buoyancy*self.height
local buoyacc = automobiles_lib.gravity*(-1)*(self.buoyancy-submergence)
--local buoyacc = self._baloon_buoyancy*(self.buoyancy-submergence)
local accell = {
x=-vel.x*self.water_drag,
y=buoyacc-(vel.y*math.abs(vel.y)*0.4),
z=-vel.z*self.water_drag
}
if self.buoyancy >= 1 then self._engine_running = false end
if last_accel then
accell = vector.add(accell,last_accel)
end
new_velocity = vector.multiply(accell,self.dtime)
else
self.isinliquid = false
new_velocity = vector.multiply(last_accel,self.dtime)
--[[if last_accel then
last_accel.y = last_accel.y + (automobiles_lib.gravity*-1) --gravity here
new_velocity = vector.multiply(last_accel,self.dtime)
end]]--
--self.object:set_acceleration({x=0,y=new_accel.y, z=0})
end
if self.isonground and not self.isinliquid then
--dumb friction
new_velocity = {x=new_velocity.x*friction,
y=new_velocity.y,
z=new_velocity.z*friction}
-- bounciness
if self.springiness and self.springiness > 0 and self.buoyancy >= 1 then
local vnew = vector.new(new_velocity)
if not self.collided then -- ugly workaround for inconsistent collisions
for _,k in ipairs({'y','z','x'}) do
if new_velocity[k]==0 and math.abs(self.lastvelocity[k])> 0.1 then
vnew[k]=-self.lastvelocity[k]*self.springiness
end
end
end
if not vector.equals(new_velocity,vnew) then
self.collided = true
else
if self.collided then
vnew = vector.new(self.lastvelocity)
end
self.collided = false
end
new_velocity = vnew
end
--damage if the friction is below .97
if self._last_longit_speed then
if friction <= 0.97 and self._last_longit_speed > 0 then
self.hp_max = self.hp_max - 0.001
automobiles_lib.setText(self, self._vehicle_name)
end --damage the plane if it have hard friction
end
--self.object:set_velocity(new_velocity)
--new_velocity = vector.subtract(new_velocity,vel)
--fix bug with unexpe3cted moving
if not self.driver_name and math.abs(vel.x) < 0.2 and math.abs(vel.z) < 0.2 then
self.object:set_velocity({x=0,y=automobiles_lib.gravity*(-1)*self.dtime,z=0})
if self.wheels then self.wheels:set_animation_frame_speed(0) end
return
end
end
self.object:add_velocity(new_velocity)
end

View File

@ -404,6 +404,7 @@ function automobiles_lib.on_step(self, dtime)
local accel = vector.add(longit_drag,later_drag)
local stop = nil
local curr_pos = self.object:get_pos()
self.object:move_to(curr_pos)
if self._show_rag == true then
if self._windshield_pos and self._windshield_ext_rotation then
@ -491,7 +492,6 @@ function automobiles_lib.on_step(self, dtime)
end
-- impacts and control
self.object:move_to(curr_pos)
if is_attached then --and self.driver_name == self.owner then
local impact = automobiles_lib.get_hipotenuse_value(velocity, self.lastvelocity)
if impact > 1 then
@ -701,15 +701,9 @@ function automobiles_lib.on_step(self, dtime)
accel.y = y_accel --sets the anti gravity
end
if stop ~= true then
--self.object:set_velocity(velocity)
self.object:add_velocity(vector.multiply(accel,dtime))
--self.object:set_acceleration(accel)
else
if stop == true then
self.object:set_acceleration({x=0,y=0,z=0})
self.object:set_velocity({x=0,y=0,z=0})
end
if stop == true then
self.object:set_acceleration({x=0,y=0,z=0})
self.object:set_velocity({x=0,y=0,z=0})
end
self._last_ground_check = self._last_ground_check + dtime
@ -748,5 +742,6 @@ function automobiles_lib.on_step(self, dtime)
--saves last velocity for collision detection (abrupt stop)
self.lastvelocity = self.object:get_velocity()
self._last_accel = accel
self._longit_speed = longit_speed
end