demoiselle/demoiselle_custom_physics.lua

44 lines
983 B
Lua
Raw Normal View History

2021-09-12 16:13:01 -07:00
local min = math.min
local abs = math.abs
--local deg = math.deg
function demoiselle.physics(self)
local friction = 0.99
local vel=self.object:get_velocity()
-- dumb friction
if self.isonground and not self.isinliquid then
2022-03-24 12:24:30 -07:00
vel = {x=vel.x*friction,
2021-09-12 16:13:01 -07:00
y=vel.y,
2022-03-24 12:24:30 -07:00
z=vel.z*friction}
self.object:set_velocity(vel)
2021-09-12 16:13:01 -07:00
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:set_velocity(vnew)
end
2022-05-29 09:49:23 -07:00
self.object:set_acceleration({x=0,y=airutils.gravity,z=0})
2021-09-12 16:13:01 -07:00
end