Need help to translate orientation from DragDetector DragFrame to value/angle

  1. What do you want to achieve?
    I have a valve with a DragDetector on it and I want to translate the DragFrame to a value.

  2. What is the issue?
    I can’t figure out how to translate a vector into an angle to then add to a value.

  3. What solutions have you tried so far?
    I have mostly done a lot of experimenting with the CFrame. So far, I’ve figured out how to get the DragFrame to become relative to the world (since the valve is angled) and translate that into a Vector3.

My goal essentially is to track how the valve turns (its on a hinge constraint), multiply it by the DeltaTime and then add it to a value. EX: Rotating the valve left will begin lowering the value.

Here is what I got so far if this context helps:

local RUNSERVICE = game:GetService("RunService")
local dragDetector = script.Parent.DragDetector


local function update(totalTime, deltaTime)
	
	local relativeCFrame = CFrame.new():ToWorldSpace(dragDetector.DragFrame) --Relative to the world/zero.
	
	local x, y, z = relativeCFrame:ToEulerAnglesXYZ()
	
	--Not sure where to continue from here.
	--I did do some experimenting and found the following data:
	
	
	--Z becomes negative if on the left
	--Z becomes positive if on the right

	--Y = 0 on the left
	--Y = 0 on the right
	--Y = -1.6 on the bottom
	--Y = 1.6 on the top
	
	--X is a jumble (not sure what's going on with it but the value of Pi keeps popping up)

end



RUNSERVICE.Stepped:Connect(update)

Thank you for any help or advice yall got! I know the solution is very close and has to do with some more complicated math but I’m just so tired right now lol.

You could just make the HingeConstaint a Servo and use the DragDetector to change the TargetAngle. You can use the CurrentAngle for how much the valve is open.

I’ve done it on light switch levers. Later tonight I can give you the code if you’re having trouble with it.

I’ve tried something similar before but that was a whole other can of worms detecting the mouse movement while keeping everything server-sided. Thanks for the suggestion though!

Wouldn’t it stay server sided with a server script and a HingeConstraint attached to an Anchored Part?
If you want to see it in action you can try my Steampunk place and in the observatory house living room or bedroom there are fan and light switches that control the fan speed and light brightness.
Come to think of it, I remember using the angle of the DragDetector as well. I’ll send the script later tonight.

Honestly I don’t even remember with the vast mix of solutions I tried. If you do end up sending the script, I’ll take a peek and see if there is anything regarding angles that I may be able to use.

Ah, right, the Hinge is Anchored, the Lever is Anchored, and all the other Parts of the Lever are unanchored and welded to it. There is no HingeConstraint involved.

The script picks up the DragDetector.DragFrame angle of the Hinge (the green part with the mouse on it) and calculates the Brightness of each of the Lights in a folder in the model according to the ratio of the angle to the Brightness I want it set at.

1 Like

Thank you so much! I knew I was close and this was the final piece that fit the puzzle! I was able to properly get the angle this time, translate it into delta and then translate it into a final value.

I’m going to try to post a video of it working in this comment and also place the code in here for anyone who visits here in the future and has a similar problem.

local RUNSERVICE = game:GetService("RunService")
local dragDetector = script.Parent.DragDetector

local lastAngle = 90
local deltaAngle = 0
local totalValue = 0


local function updateDelta(totalTime, deltaTime)
	deltaAngle *= 0.75 --Decay delta (can be anything really)

	--I'm setting my value here by adding the deltaAngle to it. 
	--I also multiply by the delta time to ensure its consistent to the second.
	--The 100 is optional but you'll probably want it to counter the deltaTime a little bit.
	totalValue += deltaAngle * deltaTime * 100 
	
end



local function updateFrame()
	local rX, rY, rZ = dragDetector.DragFrame:ToOrientation()
	local currentAngle = math.deg(rY)

	local difference = (lastAngle - currentAngle) --Essentially the delta
	if difference > 180 then difference -= 360 end --Prevents wrapping
	if difference < -180 then difference += 360 end --Prevents wrapping
	
	deltaAngle = difference
	lastAngle = currentAngle
end


dragDetector.DragContinue:Connect(updateFrame) --This fires after the dragframe resets so I think that prevents the weird jumping
--dragDetector:GetPropertyChangedSignal("DragFrame"):Connect(updateFrame) --This will cause jumps due to the dragframe resetting
RUNSERVICE.Stepped:Connect(updateDelta)
1 Like

Yeah, same issue. I was trying to figure out exactly how to get the DragFrame information but after a few trys with the Studio Assistant and help from the forums it finally worked out.

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