How would I go about simulating a pendulum motion with BodyPositioning?
I have a basic idea but I’m unsure of where to start.
If you have any ideas, any to all feedback would be greatly appreciated. ![]()
How would I go about simulating a pendulum motion with BodyPositioning?
I have a basic idea but I’m unsure of where to start.
If you have any ideas, any to all feedback would be greatly appreciated. ![]()
You could simulate it with constraints.
Or you could simulate the motion yourself every frame with some simple physics and forward euler method (i.e. every frame you calculate the accel, add the accel to the velocity, and add the velocity to the position).
Or, if the pendulum doesn’t swing too wide, you can use small angle approximation to set the position directly each frame.
The last one is the best option if you’re just doing this for a visual effect, probably.
Something like this (I just updated the cframe, but you could use cframe.Position to update a BodyPosition instead):
local part = workspace:WaitForChild("Part")
local AMPLITUDE = math.rad(20) -- outside angles
local L = 4 -- length of the pendulum in studs
local G = workspace.Gravity -- studs/s/s
-- pivot point - the orientation of this determines the orientation of the pendulum
local PIVOT = CFrame.new(0, 10, 0) * CFrame.Angles(0, math.rad(45), 0)
local SQRTLG = math.sqrt(G / L)
game:GetService("RunService").Stepped:Connect(function(t)
-- from https://en.wikipedia.org/wiki/Pendulum_(mathematics)
local theta = AMPLITUDE * math.cos(SQRTLG * t)
local cframe = PIVOT * CFrame.Angles(0, 0, theta) * CFrame.new(0, -L, 0)
part.CFrame = cframe -- or update a bodyposition or whatever
end)
I’m not on my pc at the moment, but Ill try this out in studio later. I really appreciate the explained response, Ill make sure to tell you if it works with my beam! ![]()