Rotation around two planes?

So with the following script, i have it set up so i can rotate a sphere around another sphere about the x, y, or z planes:

local Center = script.Parent.Center
local Ball = script.Parent.Part

game:GetService("RunService").Heartbeat:Connect(function()
	local yPlane = Vector3.new(math.sin(tick()) * 4, 0, math.cos(tick()) * 4)
	local xPlane = Vector3.new(0, math.sin(tick()) * 4, math.cos(tick()) * 4)
	local zPlane = Vector3.new(math.sin(tick()) * 4, math.cos(tick()) * 4, 0)
	
	
	Ball.Position = Center.Position + yPlane
end)

Image from Gyazo
Image from Gyazo

But what if i wanted to rotate the sphere about two or more planes at the same time? simply adding them together doesnt seem to work, and i dont really know how to go about this.

1 Like

I seemed to have solved it by combining two of the planes (in this case x and y), multiplied the Z component by a square root of two, and then divided the resulting vector by a square root of two, like so:

local Center = script.Parent.Center
local Ball = script.Parent.Part

game:GetService("RunService").Heartbeat:Connect(function()
	local yPlane = Vector3.new(math.sin(tick()) * 4, 0, math.cos(tick()) * 4)
	local xPlane = Vector3.new(0, math.sin(tick()) * 4, math.cos(tick()) * 4)
	local zPlane = Vector3.new(math.sin(tick()) * 4, math.cos(tick()) * 4, 0)
	
	local testPlane = Vector3.new(math.sin(tick()) * 4, math.sin(tick()) * 4, math.cos(tick()) * 4 * math.sqrt(2)) * 1/math.sqrt(2)
	
	Ball.Position = Center.Position + testPlane
end)

Now there is a new problem. What if i want to rotate about both planes, but i want to rotate them independently of each other? (ie. 50 degrees about the x plane and 95 degrees about the y)

local Plane = Vector3.new(math.sin(X_angle) * 4, math.sin(Y_angle) * 4, math.cos( ??? ) * 4 * math.sqrt(2)) * 1/math.sqrt(2)

just add a value to the angle input. like

angle + math.rad(angle offset in degrees)

i dont think you quite understand the question. In the first example, The same variable (the tick function) was used in all three parameters of the calculation. It was essentially creating a new plane that was offset between the x and y planes. But now i am trying to not combine the two planes, but have them work in unison. I am not trying to make a new plane, but rather have it rotate along the x and y planes independently. For the x and y components, i can just use the sines of the x and y. But what of the Z component? what combination of x and y angles am i supposed to plug in here?

Im not sure what your trying to acomplish either. Can you send a example(image or vid) that i can see cause your wording is weird.

In the first post, you can see two gifs. In the first one, the sphere can be observed rotating about the y axis, and in the second one it is seen rotating about the x axis. What i want to achieve is to have them rotate about both axes independently at the same time.

Thats possible. Im not sure what you were doing with the sqrts.

Here is my simple script that made 2 balls orbit around 1 main ball.

local orbitPoint = script.Parent

local xy = workspace:WaitForChild("X-Y")

local xz = workspace:WaitForChild("X-Z")

local distance = 5 -- studs

game:GetService("RunService").Heartbeat:Connect(function()
	xy.Position = Vector3.new(orbitPoint.Position.X + math.sin(tick())*distance,orbitPoint.Position.Y+math.cos(tick())*distance,orbitPoint.Position.Z)
	
	xz.Position = Vector3.new(orbitPoint.Position.X+math.cos(tick())*distance,orbitPoint.Position.Y,orbitPoint.Position.Z + math.sin(tick())*distance)
end)

Im not trying to orbit two balls, im trying to orbit one ball about two different axes with two different angles for each axis

What do you mean?

A ball can’t be in two places at the same time.

the result wouldnt be two different locations, it would be one location derived from angles rotated about the two different axes

It’s fine if you dont quite know how to solve this, I was able to find another way to do what i was trying to do

Nah its just the fact your question makes no sense.

1 Like

The problem requires an in depth understanding of trigonometry. I understand that my language may not have been perfect, and i apologize for that; it can be tough for me to explain problems like this one sometimes

perhaps you want to look into quaternions. in fact, somebody made a tutorial about them specifically for roblox itself.

2 Likes

I’m also confused about what you’re trying to do. What do you mean:

You can’t rotate “about the x plane”.

Do you want an orbit, but want to be able to set the normal vector for the plane in which it orbits?

Or do you want the ball to rotate as it orbits?

Do you just want an eliptical orbit?

Or something else entirely?

Just guessing what you want, but you could just do something like this (and set the reference frame to whatever you wanted)

local Center = workspace.Sun
local Ball = workspace.Moon

local reference = CFrame.Angles(math.rad(45), 0, math.rad(45)) -- coordinate frame for orbiting (in XZ plane)
local radius = 4 -- studs
local angle = 0
local speed = 1 -- rad/sec

game:GetService("RunService").Heartbeat:Connect(function(dt)
	angle = angle + speed * dt
	Ball.Position = Center.Position + reference * Vector3.new(radius * math.cos(angle), 0, radius * math.sin(angle))
end)

He means rotation around two axes. That’s fairly straightforward with CFrames, but I’m not some genius at math. Here’s a crack at it, but it might not be what he wants.

local z, t = math.sin(a2), math.cos(a2)
local x, y = math.sin(a1)*t, math.cos(a1)*t

From here you can specify an origin and a radius to fit your video if you wish. You can swap or negate axes to get different directions of rotation.

x*=radius, y*=radius, z*=radius
x+=xPos, y+=yPos, z+=zPos

You mean like I just did here? Select the text you want to quote and a little button saying Quote will pop up. Just press that.
Or if you want to link to another other post, just press this on the post you want to embed. If you’re on mobile it might be buried behind a menu button in the same location.
image

1 Like

Sorry, i meant the x and y axes

The only way to accomplish what you want is by rotating each angle in YXZ order.
ball.Position = (CFrame.new(center.Position) * CFrame.fromEulerAnglesYXZ(yPlaneRotation, xPlaneRotation, zPlaneRotation) * CFrame.new(0, 0, -center.Size.Z/2)).Position

2 Likes