Jitter effect when multiplying by delta time

I’m trying to make a weapon that has some basic movement with a spring module, based on recoil, character movement, and mouse movement.

When I add the mouse movement to the spring and multiply it by delta time in a RenderStepped function the product from the spring makes the weapon jitter but when I remove the * deltaTime and rely on my framerate the weapon moves smoothly.
Why is the delta time adding a jitter?

What I get the jitter
What I want the smoothness

Devforum just wouldn’t upload directly

This code is the jist of what happens for the mouse movement

local mouse_spring = Springs.Vector3Spring()

-- RenderStepped function
game.RunService.RenderStepped:Connect(function(deltaTime)
  local mouseDelta = UserInputService:GetMouseDelta()
  mouse_spring:shove(Vector3.new(mouseDelta.X, mouseDelta.Y))
  
  -- The jitter
  local sway_shove = (mouse_spring:update(deltaTime)* 24 * deltaTime) * RADIAN_CONV
  -- No jitter
  local sway_shove = (mouse_spring:update(deltaTime)) * RADIAN_CONV
  
  finalCFrame *= CFrame.Angles(sway_shove.Y, sway_shove.X / 5, sway_shove.X / 3)
end)
The one, the only, the Spring module!
local ITERATIONS = 16
local master = {}

export type Vector3Spring = {
	target: Vector3,
	position: Vector3,
	velocity: Vector3,
	cap: Vector3 | nil,
	
	mass: number,
	force: number,
	damping: number,
	speed: number,

	shove: (self: Spring, force: Vector3) -> (),
	update: (self: Spring, deltaTime: number) -> (Vector3)
}

function master.Vector3Spring(
	mass: number?,
	force: number?,
	damping: number?,
	speed: number?
)

	local spring: Vector3Spring = {
		target = Vector3.new();
		position = Vector3.new();
		velocity = Vector3.new();

		['mass'] = mass or 5;
		['force'] = force or 50;
		['damping'] = damping or 4;
		['speed'] = speed  or 4;

		shove = function(self: Spring, force: Vector3)
			local x, y, z	= force.X, force.Y, force.Z
			if x == math.huge or x == -math.huge then x = 0 end
			if y == math.huge or y == -math.huge then y = 0 end
			if z == math.huge or z == -math.huge then z = 0 end

			self.velocity = self.velocity + Vector3.new(x, y, z)
		end,

		update = function(self: Spring, deltaTime: number)
			local scaledDeltaTime = math.min(deltaTime, 1) * self.speed / ITERATIONS

			for i = 1, ITERATIONS do
				local iterationForce = self.target - self.position
				local acceleration = (iterationForce * self.force) / self.mass

				acceleration = acceleration - self.velocity * self.damping

				self.velocity = self.velocity + acceleration * scaledDeltaTime
				self.position = self.position + self.velocity * scaledDeltaTime
			end

			return self.position
		end
	}

	return spring
end

return master

Thanks!

  • SkyLord_103

What is deltaTime coming from? You are using deltaTime twice in one of the formulas, which can’t be right.

It’s coming from the dt parameter in the RenderStepped function.

And holy guacamole what an oversight… I feel embarrassed to be a programmer rn.

I am multiplying by delta time in the wrong spot :confused:

  mouse_spring:shove(Vector3.new(mouseDelta.X, mouseDelta.Y) * deltaTime)
  local sway_shove = (mouse_spring:update(deltaTime)* 24) * RADIAN_CONV

Do I even need to multiply the mouseDelta by deltaTime :thinking:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.