generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Setting an orbit.

Previous Thread :: Next Thread 
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
09 Sep 2013 11:03 PM
If you've read all of this, and you just want the code, scroll to the bottom.

I don't particularly like making tutorials like this, however, these are all common questions:

"How do I make a part go around another?"
"How do I set a part into an orbit?"
"How can I generate a circle?"

The answer is much simpler than it seems.
In order to orbit around a fixed axis, we have to understand what a circle is. How can we define the points on a circle? Well, one might say we can use this formula:
r^2 = x^2 + y^2
However, this only gives us four points.
Trigonometry is a very fascinating subject, and it allows us to do much more than we could imagine. See, the unit circle is a circle with a radius of 1. Meaning, if we take a circle, and split it in half, the line that split the circle would have the measure of the diameter of the circle, or the full length of the circle. If we take that line and bisect it right in the middle again (just as we did with the circle, except with a line), the length of one of the resulting segments is one half of the diameter.
Alright. So, assuming you understand that and know what the basic trigonometric functions are (sine, cosine and tangent), we can know devise a formula to plot all points from 0 to 2pi radians (0 to 360 degrees). Here's a quick refresher:

sine(angle) = opposite/hypotenuse
cosine(angle) = adjacent/hypotenuse
tangent(angle) = opposite/adjacent

The beauty of it is that, when we apply the unit circle, the hypotenuse will ALWAYS have a length of 1. Why? Because the hypotenuse is actually just a vector in standard position that reaches from (0, 0) to any point it can get in the angle it is directed, in order to become the radius of the circle. For a layman, that simply means the hypotenuse = opposite.
What does this mean? Well, this means:

sine(angle) = opposite
cosine(angle) = adjacent

Thus, we can figure out the lengths of all three sides of the triangle simply with the angle.
Now, if we were to set up a platform on Roblox and put a CylinderMesh in it, it would most certainly not have a radius of 1 (unless you'd like a very small platform). Thus, multiplying the sine and cosine by the radius of the circle you'd like the part to travel around will give us the sides of our triangle.
"But wait!" you say. "What does finding the triangle have anything to do with it?" Obvious. The point at which the hypotenuse intersects with the circle is going to be equal to (in rectangular coordinates):
(r*cos(angle), r*sin(angle))
Where r is the radius.
Okay, so, this point will not change unless we change the angle. What does that call for? A for loop. Now, the answer is quite obvious.

r = 20.5 -- this is the radius of the circle we travel around
q = Workspace.Part -- part inserted
p = Workspace.Part.CFrame -- part is at the center of the circle we would like it to travel around (e.g., the head of a character)

for i=0, 360 do -- 0 to 2pi
q.CFrame = p + Vector3.new(r*math.sin(math.rad(i)), 0, r*math.cos(math.rad(i)))
-- the above line simply makes the CFrame of the part equal to itself PLUS the coordinates of the intersection between the circle and the radius (simple vector addition)
wait() -- wait 1/30 of a second
end
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
09 Sep 2013 11:05 PM
Excuse me.

"For a layman, that simply means the hypotenuse = opposite."
I meant to say:
"For a layman, that simply means the hypotenuse = radius."
Report Abuse
ScrewDeath is not online. ScrewDeath
Joined: 03 Jun 2012
Total Posts: 2700
09 Sep 2013 11:11 PM
Nice tutorial c:

Could you explain where you got this formula from:
"(r*cos(angle), r*sin(angle))
Where r is the radius."

I got lost at that part.
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
09 Sep 2013 11:17 PM
Yes. If you understand the three foremost functions of trig, you know that:

sin(theta) = opposite/hypotenuse
cos(theta) = adjacent/hypotenuse
tan(theta) = opposite/adjacent

Alright. So, we know that, in a unit circle, the radius = 1. We clarified that the radius = hypotenuse at all times. Therefore, the above functions simplify as shown:

sin(theta) = opposite/1 = opposite (a number divided by 1 is equal to that number)
cos(theta) = adjacent/1 = adjacent

Therefore, simply taking the sines and cosines of the angle will return the length of the opposite and adjacent sides on a unit circle. Notwithstanding, we steadfast must scale the function up. Why? Again, the radius is 1. If we want our circle to be larger, we must increase the radius. Multiplying the sine and cosine by the radius by make it larger.
See, if we wanted a very small circle, the following would work fine:

(cos(theta), sin(theta))

However, in my example, the circle is 20.5 in radius.
Report Abuse
Scriptural is not online. Scriptural
Joined: 06 Sep 2013
Total Posts: 2979
09 Sep 2013 11:22 PM
Your forum post are always very interesting.
Report Abuse
ScrewDeath is not online. ScrewDeath
Joined: 03 Jun 2012
Total Posts: 2700
09 Sep 2013 11:24 PM
Thanks.
-feels a lil' smarter
c:
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
09 Sep 2013 11:31 PM
@Scriptural, thank you!

@screwdeath, that is a good thing.
Report Abuse
Infocus is not online. Infocus
Joined: 28 Apr 2011
Total Posts: 8022
09 Sep 2013 11:35 PM
You're one of the few scripters that never fail to amuse me due to the high logic put into your posts.

Thank you ^^

I still don't get any math though, *sigh*, the perks of being a failure in math.
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
09 Sep 2013 11:47 PM
@infocus, if you need any help with math, feel free to PM me. Mathematics is my hobby.
Also, your modesty is appreciated.
Report Abuse
Mikesly is not online. Mikesly
Joined: 07 Sep 2007
Total Posts: 4593
10 Sep 2013 02:17 AM
This helps so much with my indie space game.
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
10 Sep 2013 06:46 AM
You are welcome.
Report Abuse
Infocus is not online. Infocus
Joined: 28 Apr 2011
Total Posts: 8022
11 Sep 2013 05:08 AM
That theory listed above is the Pythagorean theorem right?
Report Abuse
ilikepiehaha is not online. ilikepiehaha
Joined: 02 Aug 2011
Total Posts: 316
11 Sep 2013 07:25 AM
I got to the pythagorean theorem (A^2 + B^2 = C^2 maaaan) and then I lost all sense of any math ever.
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
11 Sep 2013 02:46 PM
It's sort of like the Pythagorean Theorem. I mentioned it because some people try to use it to orbit around a fixed point; it doesn't work. It returns four points, and is referred to as the equation of the circle:

a^2 + b^2 = r^2
Which also can be written as:
(x-h)^2 + (y-k)^2 = r^2
Where (h, k) is a point on the circle.
'r' is defined as the radius of the circle, also known as the distance between the midpoint on the interior of the circle and any point intersecting with the circle. For instance, let us say we have a radius of 1 (unit circle). There are technically an infinite amount of possibilities that satisfy this equation, but four stand out:

(1/sqrt(2))^2 + (1/sqrt(2))^2 = 1^2
-(1/sqrt(2))^2 + (1/sqrt(2))^2 = 1^2
(1/sqrt(2))^2 - (1/sqrt(2))^2 = 1^2
-(1/sqrt(2))^2 - (1/sqrt(2))^2 = 1^2

Now you're thinking, "OK, so why did he choose 1/sqrt(2)?" 1/sqrt(2) squared is equal to one half. Proof:
sqrt(2)^2 = sqrt(2) * sqrt(2) = 2
1/sqrt(2)^2 = 1/sqrt(2) * 1/sqrt(2) = (1*1)/(sqrt(2)) * (1*1)/(sqrt(2))
Alright, so it is 1/2. Now, 1/2 + 1/2 = 1^2 (1), which satisfies the equation. Also, a negative squared will just be a positive, thus returning the same results as if it were positive. So, the four main points (besides the orthogonal quadrilateral points) on the unit circle are:
(1/sqrt(2), 1/sqrt(2))
(-1/sqrt(2), 1/sqrt(2))
(1/sqrt(2), -1/sqrt(2))
(-1/sqrt(2), -1/sqrt(2))

Now, I said there are an infinite amount of possibilities. How is this possible? If r = 1, and a = b, plus their sum of squares = 1, then what other values are there? There are actually 1440 normalized conditions that have a sum of squares = 1, and an infinite amount of conditions if we were to measure angles in infinitely small amounts. The answer is (cos(angle), sin(angle)). Do you see?
cos^2(theta) + sin^2(theta) = 1

I demonstrated this in my code above. Look at the CFrames. See anything familiar?
Report Abuse
lupine is not online. lupine
Joined: 24 Jun 2008
Total Posts: 3561
12 Sep 2013 05:55 PM
Would there be anything wrong with doing

distance = 5
while wait() do
for i = 1, 360 do
Satellite.CFrame = Planet.CFrame * CFrame.Angles(0,math.rad(i),0) * CFrame.new(distance, 0, 0)
wait()
end
end

?
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
12 Sep 2013 06:02 PM
Yes.
Report Abuse
Infocus is not online. Infocus
Joined: 28 Apr 2011
Total Posts: 8022
12 Sep 2013 11:06 PM
I really dont get it. Maybe if I go in depth into the basics, I might be able to do it.
Report Abuse
Vrakner is not online. Vrakner
Joined: 19 Feb 2013
Total Posts: 323
13 Sep 2013 04:33 AM
I think I get it. It's mainly just the pythagorean theorem used for a circle, where x,y is the centerpoint of the circle, h,k is a point on the circle, and r is the hypotenuse or the radius.
Report Abuse
Infocus is not online. Infocus
Joined: 28 Apr 2011
Total Posts: 8022
13 Sep 2013 04:36 AM
R is most likely the hypotenuse.

I still dont get how
opp/hyp = sin

I dont get what the sine returns and what it can benefit us for roblox?

I read an algebra 2 book for 5 minutes and I found out what the 3 main trig functions were and where the hyp, adj, and opp are located on the right triangle.

I also know how to get the sin, tan, and cos

Thats about it
Report Abuse
Infocus is not online. Infocus
Joined: 28 Apr 2011
Total Posts: 8022
13 Sep 2013 04:37 AM
The formula for radius is (if I can remember correctly)

radius = volume/pi*2

idk
Report Abuse
Infocus is not online. Infocus
Joined: 28 Apr 2011
Total Posts: 8022
13 Sep 2013 04:39 AM
'r is most likely hypotenuse'

I think, because that was the thing missing qq
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
13 Sep 2013 06:28 AM
In a unit circle, r is ALWAYS equal to 1, which is equal to the hypotenuse. In fact, in any circle you define to hold triangles, the definition of the hypotenuse is the vector that has an angle of theta, and a size of the radius.
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
13 Sep 2013 06:44 AM
@Infocus:
"I dont get what the sine returns and what it can benefit us for roblox?"
There is a very simple answer for that.
See, sine is a proportion that always remains the same. If you give the angle of a vector, you can make a triangle out of it, measure the sides, and run the basic trig functions:

sin(angle) = opposite/hypotenuse
cos(angle) = adjacent/hypotenuse
tan(angle) = opposite/adjacent

The less important, but sometimes equally useful functions:

csc(angle) = hypotenuse/opposite
sec(angle) = hypotenuse/adjacent
ctn(angle) = adjacent/opposite

Given that I said r = hypotenuse = 1, we can rewrite the above functions.

sin(angle) = opposite (x/1 = x)
cos(angle) = adjacent
tan(angle) = sin(angle)/cos(angle)
csc(angle) = 1/opposite
sec(angle) = 1/adjacent
ctn(angle) = 1/tan(angle) (1/opposite/adjacent = adjacent/opposite)

This means, given the angle, we can determine the sides of any triangle we want. Not only that, it's pretty simple to find the points on the circle. We said the hypotenuse intersects with the circle, thus, sin(angle), which is the height of the triangle (opposite) will also be the height of the point wherein the hypotenuse and unit circle intersect.
cos(angle) will be the length of the triangle, so that will be the x-value of the point.
Therefore, we can retrieve any point we wish on a circle with sine, cosine and tangent.
Report Abuse
Vrakner is not online. Vrakner
Joined: 19 Feb 2013
Total Posts: 323
13 Sep 2013 11:05 AM
I just know

SOHCAHTOA.

Sine = o/h, Cos = a/h, Tangent = o/a
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
13 Sep 2013 03:50 PM
Okay.
I have not explained anything to you that is above that level.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image