Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I create centripetal force via a script?

I know the formula for centripetal force, however, I can't get it to work in Roblox. Here is my current code:

wait(0.5)

local RunService = game:GetService("RunService")

RunService.RenderStepped:connect(function()
	local Part = workspace.Part
	local Center = workspace.Center
	local m = Part:GetMass()
	local r = (Part.Position - Center.Position).magnitude
	local v = Vector3.new(math.sqrt(Part.Velocity.x), math.sqrt(Part.Velocity.y), math.sqrt(Part.Velocity.z))
	local mv = m * v
	local fc = (m * v/r)
	Part.Velocity = fc
end)

Whenever the script runs, it destroys the part (I assume that the velocity is carrying it below the destroy point) and I can't figure out how to fix it. Also, I'm waiting 0.5 seconds so that workspace.Part has enough time to drop someways and gain momentum. Also, here's a link to the Wikipedia page on centripetal force: https://en.wikipedia.org/wiki/Centripetal_force

Thanks in advance, Br4veh3art23

3 comments

0

I don't think the Variable mv was ever used.

0

I also don't think :GetMass() returns Vector3 values.

0

Alright, I now have set m to be a Vector3 variable (All of the values are Part:GetMass()) and am now using mv, and changed it to Vector3.new(m * v), still not working.

1 answer

You have a few problems with your script. Keep in mind the formula for centripetal force is mv^2/r and you are square rooting your velocity when it should actually be squared. Also, Velocity is a read-only value and the only way to actually apply forces and velocities to objects is to use body movers or CFrame. You can get the round about effect of what you want by doing this:

local edgePart = script.Parent
local radius = workspace.rad
local mass = edgePart:GetMass()

local RunService = game:GetService('RunService')

local tanVel = Instance.new('BodyVelocity', edgePart)
tanVel.Velocity = edgePart.CFrame.lookVector * 10

local cenForce = Instance.new('BodyForce', edgePart)

wait()
edgePart.Anchored = false

RunService.RenderStepped:connect(function()
	local v = tanVel.Velocity
	local r = edgePart.Position - radius.Position
	local direction = r.unit
	local radMag = r.magnitude
	cenForce.Force = (mass * (v.magnitude^2) / radMag) * direction
	tanVel.Velocity = 10 * cenForce.Force.unit:Cross(edgePart.CFrame.upVector)
end)

In this script, I create a body velocity that acts as the tangential velocity which always travels normal to the centripetal force. In order to accomplish this, you need to take take the cross product of the centripetal force and the other vector you want the tangential velocity to be normal to. In this case, I opted for the edge part's up facing direction in order to achieve a nice flat orbit. I hope this helps you out :)

On a side note, if you would be interested in accomplishing this with CFrame, I recommend reading up egomoose's guide on Hooke's law and inverse kinematics (http://wiki.roblox.com/index.php?title=Springs) in which he uses metatables to store velocity, acceleration and position vectors. I managed to use it to create a simulation of newtons law of gravitation (basically the same thing as this).

1 comment

0

Okay, I found a different way of doing it using CFrame:toObjectSpace() https://scriptinghelpers.org/questions/45673/how-do-i-make-this-move-in-uniform-circular-motion how would I make this script move the part in uniform circular motion?

This discussion is preserved as it was. New answers, comments, and votes are closed.