[Post note after solution]: every time I mention “offset angle” in here, what I mean in terms of an actual satellite is their inclination angle. Didn’t know that’s what it was called until I researched a little.
I have a satellite that can revolve around a planet on it’s X and Z axes. That code works fine, here is a dumbed down version of what that looks like:
local circle = 2*math.pi
local sin = math.sin
local cos = math.cos
local radius = 5
local origin = Vector3.new(0,10,0) -- example, origin in my code is the Planet's position
for i = 0, circle, circle/120 do
local offset = Vector3.new(cos(i), 0, sin(i)) * radius
satellite.CFrame = CFrame.new(origin + offset, origin)
task.wait()
end
If you follow the path that the satellite takes and draw it (red line), what I want to achieve is to be able to ‘tilt’ it on that Y axis (like the blue line shows).
I assume somewhere in this math there should be a * CFrame.Angles(0, offsetAngle, 0), but I’ve tried various spots and it doesn’t seem to work. Alternatively, I’ve tried just doing sin(math.rad(offsetAngle)) for the Y component of the Vector3, but that doesn’t get me the result I’m looking for.
Bonus points to anyone who can solve it and explain why their solution works.
I thought about the problem a little harder, even drawing unit circles on the X,Y, X,Z and X,Y,Z planes and realized why my attempt of doing sin(math.rad(offsetAngle)) was giving me the result it was. As i approached the distance of a circle (in radians), the Y position was static because I wasn’t involving i at all in the Y positions calculation.
The solution (at least seems to be): sin(i) * sin(rad(offsetAngle)) which makes sense in hindsight. sin(i) is just giving you the Y value of the path at any point (between -1 and 1). But if I didn’t include my offsetAngle at all, it would go all the way up. Multiplying sin(i) to sin(rad(offsetAngle)) gives you the Y value and sort of clamps it, if that’s the correct way to describe it.
The final code (using the example code I referenced above, not the code I have in my file):
local circle = 2*math.pi
local sin = math.sin
local cos = math.cos
local rad = math.rad
local offsetAngle = 15
local radius = 5
local origin = Vector3.new(0,10,0) -- example, origin in my code is the Planet's position
for i = 0, circle, circle/120 do
local offset = Vector3.new(cos(i), sin(i) * sin(rad(offsetAngle)), sin(i)) * radius
satellite.CFrame = CFrame.new(origin + offset, origin)
task.wait()
end