mirror of
https://github.com/APercy/airutils.git
synced 2025-03-21 18:41:21 +00:00
improved phisycs
This commit is contained in:
parent
028b9c3ecd
commit
3f48109809
2 changed files with 20 additions and 17 deletions
|
@ -1,7 +1,7 @@
|
||||||
function airutils.physics(self)
|
function airutils.physics(self)
|
||||||
local friction = self._ground_friction or 0.99
|
local friction = self._ground_friction or 0.99
|
||||||
local vel=self.object:get_velocity()
|
local vel=self.object:get_velocity()
|
||||||
local new_velocity = vel
|
local new_velocity = vector.new()
|
||||||
|
|
||||||
--buoyancy
|
--buoyancy
|
||||||
local surface = nil
|
local surface = nil
|
||||||
|
@ -24,6 +24,7 @@ function airutils.physics(self)
|
||||||
if surface then -- standing in liquid
|
if surface then -- standing in liquid
|
||||||
self.isinliquid = true
|
self.isinliquid = true
|
||||||
end
|
end
|
||||||
|
local last_accel = self._last_accel
|
||||||
|
|
||||||
if self.isinliquid then
|
if self.isinliquid then
|
||||||
local accell = {x=0, y=0, z=0}
|
local accell = {x=0, y=0, z=0}
|
||||||
|
@ -37,14 +38,22 @@ function airutils.physics(self)
|
||||||
--local buoyacc = self._baloon_buoyancy*(self.buoyancy-submergence)
|
--local buoyacc = self._baloon_buoyancy*(self.buoyancy-submergence)
|
||||||
accell = {x=-vel.x*self.water_drag,y=buoyacc-(vel.y*math.abs(vel.y)*0.4),z=-vel.z*self.water_drag}
|
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 self.buoyancy >= 1 then self._engine_running = false end
|
||||||
airutils.set_acceleration(self.object,accell)
|
if last_accel then
|
||||||
--new_velocity = vector.add(new_velocity, vector.multiply(accell, self.dtime))
|
accell = vector.add(accell,last_accel)
|
||||||
self.object:move_to(self.object:get_pos())
|
end
|
||||||
return
|
new_velocity = vector.multiply(accell,self.dtime)
|
||||||
|
--airutils.set_acceleration(self.object,accell)
|
||||||
|
--self.object:move_to(self.object:get_pos())
|
||||||
else
|
else
|
||||||
--airutils.set_acceleration(self.object,{x=0,y=airutils.gravity,z=0})
|
--airutils.set_acceleration(self.object,{x=0,y=airutils.gravity,z=0})
|
||||||
self.isinliquid = false
|
self.isinliquid = false
|
||||||
--new_velocity = vector.add(new_velocity, {x=0,y=airutils.gravity * self.dtime,z=0})
|
|
||||||
|
if self._last_accel then
|
||||||
|
last_accel.y = last_accel.y + airutils.gravity --gravity here
|
||||||
|
|
||||||
|
new_velocity = vector.multiply(last_accel,self.dtime)
|
||||||
|
end
|
||||||
|
--self.object:set_acceleration({x=0,y=new_accel.y, z=0})
|
||||||
end
|
end
|
||||||
|
|
||||||
if self.isonground and not self.isinliquid then
|
if self.isonground and not self.isinliquid then
|
||||||
|
@ -85,10 +94,10 @@ function airutils.physics(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
--self.object:set_velocity(new_velocity)
|
--self.object:set_velocity(new_velocity)
|
||||||
local vel_sum = vector.subtract(new_velocity,vel)
|
--new_velocity = vector.subtract(new_velocity,vel)
|
||||||
self.object:add_velocity(vel_sum)
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.object:add_velocity(new_velocity)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -183,6 +183,7 @@ function airutils.logic(self)
|
||||||
local velocity = self.object:get_velocity()
|
local velocity = self.object:get_velocity()
|
||||||
local curr_pos = self.object:get_pos()
|
local curr_pos = self.object:get_pos()
|
||||||
self._curr_pos = curr_pos --shared
|
self._curr_pos = curr_pos --shared
|
||||||
|
self._last_accel = self.object:get_acceleration()
|
||||||
|
|
||||||
self._last_time_command = self._last_time_command + self.dtime
|
self._last_time_command = self._last_time_command + self.dtime
|
||||||
|
|
||||||
|
@ -503,14 +504,6 @@ function airutils.logic(self)
|
||||||
local limit = (self._max_speed/self.dtime)
|
local limit = (self._max_speed/self.dtime)
|
||||||
if new_accel.y > limit then new_accel.y = limit end --it isn't a rocket :/
|
if new_accel.y > limit then new_accel.y = limit end --it isn't a rocket :/
|
||||||
|
|
||||||
if not self.isinliquid then --why? because whe work water movements in the phisics code
|
|
||||||
new_accel.y = new_accel.y + airutils.gravity --gravity here
|
|
||||||
end
|
|
||||||
|
|
||||||
local vel_to_add = vector.multiply(new_accel,self.dtime)
|
|
||||||
--vel_to_add.y = 0
|
|
||||||
self.object:add_velocity(vel_to_add)
|
|
||||||
--self.object:set_acceleration({x=0,y=new_accel.y, z=0})
|
|
||||||
else
|
else
|
||||||
if stop == true then
|
if stop == true then
|
||||||
self._last_accell = self.object:get_acceleration()
|
self._last_accell = self.object:get_acceleration()
|
||||||
|
@ -614,6 +607,7 @@ function airutils.logic(self)
|
||||||
airutils.testImpact(self, velocity, curr_pos)
|
airutils.testImpact(self, velocity, curr_pos)
|
||||||
|
|
||||||
--saves last velocity for collision detection (abrupt stop)
|
--saves last velocity for collision detection (abrupt stop)
|
||||||
|
self._last_accel = new_accel
|
||||||
self._last_vel = self.object:get_velocity()
|
self._last_vel = self.object:get_velocity()
|
||||||
self._last_longit_speed = longit_speed
|
self._last_longit_speed = longit_speed
|
||||||
self._yaw = newyaw
|
self._yaw = newyaw
|
||||||
|
|
Loading…
Add table
Reference in a new issue