Hello developers!
I am writing a script in which I need to convert the Radius
of a circle and a given Angle
into x
and y
coordinates.
For example, if I R = 5 and A = 90, how can I find X and Y?
Thank you!
Hello developers!
I am writing a script in which I need to convert the Radius
of a circle and a given Angle
into x
and y
coordinates.
For example, if I R = 5 and A = 90, how can I find X and Y?
Thank you!
Do you need this as a 2D vector or a 3D vector?
After some further looking into, are you sure you don’t know either X or Y already? What is it being used for?
2D… because I need X
and Y
not X
, Y
, and Z
.
I’m not a mathematician, but I don’t think you have enough information given to find that out.
A point consisting of an X and Y value can be found anywhere in the quadrant that you have, technically. So just being given a radius and angle, I don’t think you could narrow that down to just a single point. (imk)
I thought the same but after doing a bit of searching, (haven’t done math, especially trig, in awhile), you can apply the Law of sines to go from just a hypotenuse and angle to finding the other sides. Whether and how you’ll be able to map this to a 2D circle, I don’t know. Still looking into it.
How do those pet systems do it, where they have pets evenly separated throughout a circle?
My logic was that each angle is 360/NumberOfPets, and this is how to get the coordinates of each pet.
(I am not making a pet system or anything 3d, this is an example)
Ah! Thanks for reminding me, my friend helped me figure the math out for this exact thing awhile ago. Not a bad suggestion actually. I’m gonna look into that code now.
Thanks, I appreciate it!
local Center = workspace.Center
local Radius = 5
function getXAndZPositions(Angle, Radius)
local x = math.cos(math.rad(Angle)) * Radius
local z = math.sin(math.rad(Angle)) * Radius
return x, z
end
local function NewPart(Angle)
local PartVisual = Instance.new("Part", workspace)
PartVisual.Anchored = true
local X, Z = getXAndZPositions(Radius, Angle)
PartVisual.Position = Vector3.new(X, Center.Position.Y, Z)
PartVisual.Name = Angle
end
NewPart(0)
NewPart(45)
NewPart(90)
NewPart(180)
NewPart(270)
This is the code I have been using to test. Here is the result I am getting:
(Part in red is the center part)
What am I doing wrong?
I believe the issue with your implementation is the way you’re using the Angle
. From the Angle
and the Radius
, we have to find the X and Z values (the sides) of the triangle. (From my understanding of this video).
Trying to re-write your function right now with the knowledge I gained.
I figured it out this was actually a pretty fun learning experience and makes me want to get right back into math (I was good in high school I swear).
local radius = 10
local function getXAndZPositions(r, angle)
local x = math.sin(math.rad(angle)) * r
local z = math.cos(math.rad(angle)) * r
return x, z
end
local function newPart(angle)
local p = Instance.new("Part")
p.Anchored = true
p.Size = Vector3.new(2, 2, 2)
p.Name = "Testing Maths ["..angle.."]"
local x, z = getXAndZPositions(radius, angle)
p.Position = workspace.Origin.Position + Vector3.new(x, 0, z)
p.Parent = workspace
end
newPart(45)
newPart(90)
newPart(135)
newPart(180)
You had the angle part correct, your entire getXAndZPositions function was right. It was just the way you implemented it. Add it to the origin part (the centre)'s Position instead.
Yes you could if you know where exactly the angle is placed.
In the case of OP’s drawing I’m assuming the “starting point” is the black radius line, so the coords on the circle are x + r, y. In case of the red line the coords are x, y + r, and you can get the coords for any angle the way someone posted above in their getXAndZPositions
function.
You can think of it like of drawing a line at a specific angle and then finding the point of intersection with the circle.
For example if you go to demos and input the following 2 equations:
x^2 + y^2 = 10^2
and
y = 4/3x
you can see it intersects the circle at (6, 8)
this is because the radius of the circle is 10
, and the angle is arctan(4/3)
, which’s cosine is 0.6
, and 10 * 0.6 = 6
You can also correctly guess that the sine of arctan(4/3)
is 0.8
Quick question since I’m horrible at math lmao
Is arctan the same as math.atan in lua and same with cosine and math.cos?
I’m unsure of the difference between math.atan
and math.atan2
, but in case of sine and cosine yes they’re math.sin
and math.cos
.
Note that they take radians, not angles.