What do you want to achieve? Hello there! I’ve been wondering recently how to calculate time taken to reach the DesiredAngle property from the CurrentAngle property on Motor6Ds depending on the MaxVelocity of that motor so I can simulate something like a pendulum swing smoothly?
What is the issue? I’ve conducted some tests using various formulas and none seem to help for the fact that Roblox doesn’t provide any easy method for calculating this, I’m just guessing at this point.
What solutions have you tried so far? I have looked on the DevForum for answers to this or other similar topics, none have made anything very clear to me. I have also tried using Roblox’s Assistant AI for help, which did provide a formula which didn’t help me in my case. I also tried asking ChatGPT, which gave a different formula which also didn’t help, adding to the confusion I’m currently experiencing…
Overall, the swing should basically reach a certain point, then drop down and swing to the opposing side after I apply the negative of the DesiredAngle to that property once it has been reached - I have already achieved that, but I need to know the time taken for me to use this in a Tween I’m planning on adding to synchronise the deceleration of the pendulum as it loses more energy to the surroundings when going higher, think of this due to gravity in real life (I may be wrong here not being the best at physics). If there is an easier way than tweening it going down please tell me, however I wanted to try this method out if possible!
Many thanks, Green!
Portion of my Current Code:
local function calculateSwingTime(swingMotor: Motor6D, desiredAngleDegrees: number)
-- Convert desired angle from degrees to radians
local desiredAngle = math.rad(desiredAngleDegrees)
-- Get the current angle of the motor
local currentAngle = swingMotor.CurrentAngle
-- Calculate the angle difference and normalize it to -π to π
local angleDifference = (desiredAngle - currentAngle) % (2 * math.pi) -- Wrap to [0, 2π]
if angleDifference > math.pi then
angleDifference = angleDifference - 2 * math.pi
elseif angleDifference < -math.pi then
angleDifference = angleDifference + 2 * math.pi
end
-- Calculate and return the time to rotate to the desired angle
local maxVelocity = swingMotor.MaxVelocity
if maxVelocity <= 0 then
error("MaxVelocity must be greater than 0.")
end
return math.abs(angleDifference) / maxVelocity
end
local function swingHammer(swingMotor: Motor6D)
local currentSwingerRotationAngle = SWINGER_MAX_ROTATION_ANGLE
task.spawn(function()
while true do
swingMotor.DesiredAngle = math.rad(SWINGER_MAX_ROTATION_ANGLE)
repeat
task.wait()
until swingMotor.CurrentAngle == swingMotor.DesiredAngle
swingMotor.DesiredAngle = math.rad(-SWINGER_MAX_ROTATION_ANGLE)
repeat
task.wait()
until swingMotor.CurrentAngle == swingMotor.DesiredAngle
end
end)
end
Also note that the calculation of the Motor6D was tested before, I tried having it so that it could tween via creating a Tween via TweenInfo. The main issue here and why I need to have it calculated is because the speed of the Motor6D is randomly generated on creation.
If I understand correctly - you want to measure the time it takes until the object gets to the desired angle?
If so, I would recommend using tick() (there are few alternatives to tick, but tick works fine) before and after the initial movement. Then, get the difference between the times to determine the time it took.
Or do you wish to calculate it before the movement?
Correct, yeah. Unfortunately I’m not the smartest when it comes to figuring things out like that from scratch. I apologise if this has been mentioned elsewhere, however I didn’t see anywhere that really stood out after checking some previous posts that concerned Motor6Ds.
So you don’t want to measure the time, and want to calculate it instead, to basically just tween the pendulem entirely, removing the need to use a Motor6D?
The tweening would be required in slowing down the MaxVelocity property as it reaches the DesiredAngle, like it would in reality. It is supposed to simulate actual physics in that way.
Is any other object going to be able to come into contact with the pendulum at all (e.g. a player)?
You may be able to get away with not using a script for that.
(Although it may end up being more difficult than it is worth xD)
I’ve already programmed that and that function works - the player will come into contact with any of the parts of the pendulum and as a result the player will receive a BodyVelocity being applied along with the Humanoid’s PlatformStanding property being disabled, which will then result the player to be hit with a force.
One last clarification - Is the time you want to calculate the time it takes assuming linear motion, or is it meant to be as if it was moving naturally due to gravity?
When you end up tweening it, make sure to set the movement type in the TweenInfo to be whatever the sinusoidal one is.
Well it should be due to gravity if possible, however I didn’t see any feasible method for smoothly allowing this other than Motor6Ds. Cylindrical constraints may have worked however I’m fairly familiar with Motor6Ds over C Constraints so I tried that out instead.
The above is currently how I have structured my, well hammer actually (I mentioned pendulum as it is easier to think and I’ve seen it is a more common reference over a hammer).
Yeah, that seems to be the most applicable value to Tween with Motor6Ds.
Is the length of the arm going to be constant?
(Sorry about all the questions, just trying to fully understand the question, and figure out if the different answers can be used in the calculation. It’s a bit late at night so its taking me a moment - anyone else looking at the thread can help if they wish ).
Feel free to ask lol, but yeah the arm will stay the same length, the only thing that is happening is that from this part (see below), the rotation will take place, also shown below are the properties of the motor. The Part1 is the Rotator which is selected, and all other things you can see inside the “Swinger” model are to be joined via WeldConstraints (as seen below):
Very nice, I’ll run these calculations and report back to you what I receive! Is there a way to do this purely through the Motor6D itself as I had no idea that the parts it was joined to had an impact at all (as they are just welded)? I’m saying that the main part that is initially being impacted altogether is the cylindrical part that I selected above.
Motor6D doesn’t seem to be affected by what sort of parts are attached. (I think - as they are somewhat an older part I haven’t really dealt with them).
You will need to use a script, which tweens the max velocity. As long as you set the EasingStyle of the tween to Sine, it should work fine