[CFrame math] Best way to update angular orientation of CFrame matrix via critically damped spring module?

I recently shared a spring module that I imported into roblox lua in a previous post which you can see here. It works great and it ended up being the solution I needed to create my ‘dragged’/‘chase’ camera for the hover car I’m working on. But, I can’t figure out how to use the spring to update not just the position of the camera but also it’s orientation. This is what I’ve attempted so far although it produces buggy results which I’ll show below:

-- p = pos, tv = top vector, bv = back vector

local updateCam do
	local vec3 = Vector3.new
	local CF = CFrame.new
	
	local function dc(cf)
		local px, py, pz, 
			xx, yx, zx, 
			xy, yy, zy, 
			xz, yz, zz = cf:components()
	    local p = vec3(px, py, pz)
		local rv = vec3(xx, xy, xz)
		local tv = vec3(yx, yy, yz)
		local bv = vec3(zx, zy, zz)
		return p, rv, tv, bv
	end
	
	local function CFrameFromTopBack(p, tv, bv)
		local right = tv:Cross(bv)
		return CF(p.x, p.y, p.z,
			right.x, tv.x, bv.x,
			right.y, tv.y, bv.y,
			right.z, tv.z, bv.z)
	end
	
	local cp, _, ctv, cbv = dc(Camera.CFrame)
	local m_spring = require(script.Spring)
	local pspring = m_spring.new(cp, vec3())
	local tvspring = m_spring.new(ctv, vec3())
	local bvspring = m_spring.new(cbv, vec3())
	
	function updateCam(dt)
		local bp, _, btv, bbv = dc(carCFrame*Offset)
		pspring.target = bp
		tvspring.target = btv
		bvspring.target = bbv
		pspring:update(dt)
		tvspring:update(dt)
		bvspring:update(dt)
		local fp, ftv, fbv = pspring.pos, tvspring.pos, bvspring.pos
		Camera.CFrame = CFrameFromTopBack(fp, ftv, fbv)
	end
end

This method resulted in this:
https://i.gyazo.com/4840ab2f523f1dcfc53a5027abf83e24.mp4

Awesome UI is awesome

As you can see in the video above, when the camera moves fast the camera begins to get warped with the above method.

Does anyone know a solid solution for this that won’t result in the cframe matrix being warped?

6 Likes

That’s interesting, I didn’t know the camera could get skewed like that.
Have you orthonormalized the rotation matrix?

You might be able to fix it if you just take the lookVector from the CFrame you’re getting, and use that to construct a directional CFrame using the position you want the camera in, and that position + the lookVector of the CFrame you’re getting.

8 Likes

Wooow how could I forget to orthonormalize the rotation matrix facepalm
Orthonormalizing the rotation matrix completely solved the issue, thanks!

2 Likes