Log In  


Arrow keys to edit the variables.
Click (and hold, if you like,) to set the target position.

Cart #sod_demo-0 | 2024-07-16 | Embed ▽ | License: CC4-BY-NC-SA
5

If you're just interested in the API, you can grab it here:

local Sod = {}
Sod.__index = Sod

function Sod:update(dt)
	local target_vel = (self.target-self.prev_target)/dt

	self.pos += self.vel*dt

	self.vel += (
		self.target
		+self.k3*target_vel
		-self.pos
		-self.k1*self.vel
	)*dt/self.k2

	self.prev_target = self.target
end

function Sod:config(frequency_rads,damping,response)
	local ifreq = 1/frequency_rads
	self.k1,self.k2,self.k3 =
		damping*ifreq*2,
		ifreq*ifreq,
		response*damping*ifreq
end

function sod(pos,frequency_rads,damping,response)
	local o = {
		pos = pos,
		prev_target = pos,
		target = pos,
		-- In case pos is a vector, or any other arithmetically capable data
		-- type, *0 guarantees we get the zero position of whatever type pos is.
		vel = pos*0,
	}
	setmetatable(o,Sod)
	o:config(frequency_rads,damping,response)
	return o
end

5



[Please log in to post a comment]