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

Part rotation affects part position?

This kinda expands off of my last post. So basically I have this script that automatically sets the Position of a part and its orientation after doing some calculations, but for some reason setting the Rotation of the part would also deal collateral damage and mess up the Position-al component.

#Version 1 (commented) ##Code:


local runService = game:GetService("RunService")

local parachute = script.Parent
local base = parachute:WaitForChild("Base").Value
local dragBF = base:WaitForChild("Drag")

local resistanceCoefficient = 10
local parachuteDistance = 10

runService.Heartbeat:Connect(function(step)
	
	-- Compute the parachute's drag force
	local drag = -base.Velocity * resistanceCoefficient
	
	-- Compute position and rotation of parachute
	local chutePos = base.Position + base.Velocity.Unit * parachuteDistance
	local d = base.Position - parachute.Position		-- Delta X Y Z
	local hXY = math.sqrt(d.X^2 + d.Y^2)				-- Hypotenuse of triangle with dX and dY as legs
	local hXZ = math.sqrt(d.X^2 + d.Z^2) 
	local hYZ = math.sqrt(d.Y^2 + d.Z^2)
	
	local rotZ = math.asin(d.X/hXY)
	local rotY = math.asin(d.Z/hXZ)
	local rotX = math.asin(d.Y/hYZ)
	
	parachute.Position = chutePos
	--parachute.Rotation = Vector3.new(rotX, rotY, rotZ) 	-- rotation commented out
	
	print(tostring(chutePos))
	print(tostring(parachute.CFrame.p).." "..tostring(parachute.Rotation))
	
	dragBF.Force = drag
end)

BTW I did try setting the rotation by multiplying by CFrame.Angles but that didn't yield any better results

##Output:

0, 54.5970306, 0
0, 54.5970306, 0 0, 0, 0
0, 54.7103844, 0
0, 54.7103844, 0 0, 0, 0
0, 54.7820587, 0
0, 54.7820587, 0 0, 0, 0
...

#Version 2 (uncommented) ##Code:

local runService = game:GetService("RunService")

local parachute = script.Parent
local base = parachute:WaitForChild("Base").Value
local dragBF = base:WaitForChild("Drag")

local resistanceCoefficient = 10
local parachuteDistance = 10

runService.Heartbeat:Connect(function(step)
	
	-- Compute the parachute's drag force
	local drag = -base.Velocity * resistanceCoefficient
	
	-- Compute position and rotation of parachute
	local chutePos = base.Position + base.Velocity.Unit * parachuteDistance
	local d = base.Position - parachute.Position		-- Delta X Y Z
	local hXY = math.sqrt(d.X^2 + d.Y^2)				-- Hypotenuse of triangle with dX and dY as legs
	local hXZ = math.sqrt(d.X^2 + d.Z^2) 
	local hYZ = math.sqrt(d.Y^2 + d.Z^2)
	
	local rotZ = math.asin(d.X/hXY)
	local rotY = math.asin(d.Z/hXZ)
	local rotX = math.asin(d.Y/hYZ)
	
	parachute.CFrame = CFrame.new(chutePos)
	parachute.Rotation = Vector3.new(rotX, rotY, rotZ)
	
	print(tostring(chutePos))
	print(tostring(parachute.CFrame.p).." "..tostring(parachute.Rotation))
	
	dragBF.Force = drag
end)

##Output:

0, 54.5970306, 0
0, -3.40282347e+38, 0 0, 0, 0
0, 54.6886597, 0
0, -3.40282347e+38, 0 0, 0, 0
0, 54.7569809, 0
0, -3.40282347e+38, 0 0, 0, 0
...

So what's happening here? How is the Rotation of the part adversely affecting the Position??

1 answer

Set CFrame instead of Rotation. To do this, set the part's CFrame to it multiplied by CFrame.Angles(math.rad(x), math.rad(y), math.rad(z))

So if you want to rotate a part, let's say 90 degrees, on x axis, you would do it like this:

part.CFrame = part.CFrame * CFrame.Angles(math.rad(90), 0, 0) --you dont need to rad(0)

1 comment

0

Read the bolded part of my question: BTW I did try setting the rotation by multiplying by CFrame.Angles but that didn't yield any better results, but I'll try again to see if anything changed

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