Made a simple damped harmonic oscillator but need to make it driven instead of simple?

Sorry for taking a long time to reply again :exclamation:
Things ended with that friend, so I haven’t been that happy or motivated about it, but at least its a toxic person out of my life I don’t have to be worried about :man_shrugging:
Its also easter half term and my mum is making me revise for gcses which honestly im not sure I care about anymore but I’m trying to do that aswell which is 2 and half hours a day :roll_eyes: :joy:
Also been focusing a lot on my physical health alot more, meaning I have to dedicate at least 1 hour of my day to that as well, combined with focusing on my nutrition but its paying off :exclamation: :smile:
Not really had much time to be able to focus on this with lack of motivation, but hey today I’m gonna try to do it again, so this may be full with content or questions… :wink:
I know you have linked me stuff to many things but I’m ngl I’m kinda retarded so I won’t be able to figure out stuff like that on my own unless I tried really hard so I appreciate so much that you are able to explain this stuff to me :exclamation: :smile:
Please make sure you read through everything I send, I will try to cover everything I can in one post… :slightly_smiling_face:

So far, I’ve looked at all your replies and came up with this code stitched together however I would like it you could check it and tell me of any errors or necessary changes to be made?

note: multiple springs will be made so they all need their own variables, including time, as they could all change at any time…

I saw this in one of your earlier posts…

I have linked to what my application would be by using video footage: here
Or in words, an angular based spring (?) I think thats what it would be called, that would act in a firt person game, where the character moves their arms it causes a spring like action to occur, where the more they move it, the more the spring acts, and when they stop it kind of ricochets, I think it would be described as a driven damped harmonic oscillator, but I hope this is clearing things up?

You also said;

However, in the equations used, there are some exponential?
Wondering about the effects on performance in this case?

I have wrote up somewhat of the program for a spring so far, however I am aware there will be mistakes or unfinished code…

local target = workspace.Target
local current = workspace.Current

local function create(c, w, d)
	local s = {}
	
	--w is undamped frequency
	s.w = w
	--d is damping ratio
	s.d = d
	--X is the target value
	s.X = c
	--x is the current value
	s.x = c
	--x0 is the initial position
	s.x0 = c
	--E is the error (calculated by doing X-x)
	s.E = Vector3.new()
	--v0 is the initial velocity (set at the start or when target changes)
	s.v0 = Vector3.new()
	--v is the current velocity
	s.v = Vector3.new()
	--dx is the total time passed
	s.dx = 0
	--B is the bias term 
	s.B = 0
	--sum is for integration
	s.sum = 0
	
	
	s.update = function(x)
		s.E = s.X - s.x
		s.sum = s.sum + s.E * x
		s.dx = s.dx + x
		local a = s.w ^ 2 * (s.X - s.x) - 2 * s.d * s.w * s.dx/x
		s.B = s.v0 - 2 * s.d * s.w * (s.X - s.x0)
		s.v = s.B + s.w ^ 2 * s.sum - 2 * s.d * s.w * (s.x - s.x0)
		if s.v ~= Vector3.new() then
			--reset inital position and time passed, when velocity is 0
		end
		--need to set current position as s.x, so that when accessing spring can reach value
	end
	return s
end

local spring = create(Vector3.new(0, 0, 1.05), 2, 3)
current.Position = spring.x -- set the position of visible spring as the current value


game:GetService("RunService").RenderStepped:Connect(function(s)
	spring.X = target.Position --set target every frame
	spring.update(s) --update the spring itself
	current.Position = spring.x --update to the current position
end)

I am most concerned about the s.update function, as that is the main part of the program;
I assume that s.E, s.sum and s.dx are calculated correctly here and in the correct order?

I was a little confused about calculating a, where at the end I have s.dx/x and I’m not sure if thats correct in itself…

I think that s.B is calculated correctly, based off of your calculations but I’m not a hundred percent sure its correct…

I was confused about the s.v calue about the integration (I have just multiplied it by the term of sum and I don’t know if thats correct at all…

I have also come to wonder about when you specifally set s.v0 or what it is set to…

Also, there are some values im not sure if it changes when you set them? such as x0, would this be set when the target velocity is 0?

I am aware of there being a couple of cases where when the target is being updated or not being updated that certain things need to be changed, so I have included the if statement, however I wonder if there is already a value that calculates, or not, if there isn’t that’s pretty easy to calculate…

The current value is also pretty important but I can’t see anywhere where that is actually set, as you have to be able to access the current value to be able to set the spring or object thats being used? like for example if the starting point was 0 and the target is at 1 and the current is calculated to be 0.5 you want to set the object or spring at 0.5, however it wasn’t completely clear where that would be set… :?

I think thats all of my questions or queries, I am hoping you can help here on my problem, I have tried to be clear and concise as possible but explaining everything is necessary :smile:

Yet again, I am very grateful you have stuck by in helping figure out this, it has been stuck on my head for a very long time! I really want to be able to do it to be able to progress in my game :exclamation: :smile:

1 Like