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 » Scripters
Home Search
 

Re: --== The Big CFrame Thread ==--

Previous Thread :: Next Thread 
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
18 Mar 2015 08:30 PM
Elementary, my dear Watson.

So, some of you guys (I'd say "some of you all," but the fact that there are no girls [all who say so are liars] allows this) don't exactly understand CFrame as much as you might like. Some of you think you know it and try to explain it wrong (I was interrogating a renowned scripting helper yesterday on a certain cloud site and was highly disappointed).

I'm not gonna teach the math. Nobody cares about why it works, but just what it can do for you!

Use this as a reference thread if you want, a question thread, or a useless thread!

-----------------------


== The Rotation Matrix ==


Ah yes! Those crazy nine numbers that literally make about as much sense as the chemical name for the protein "Titin." For those of you that don't know, proteins are named by their individual molecules, and Titin is a rather large protein 0_0

So, yes you may know the matrix houses rotation. But what _really_ is it and how can you use it!?!??!?!?!?

You ever heard of lookVector? CFrame.lookVector? If you haven't, and you're reading this thread, please consider looking it up and understanding what it does.

So, let's print CFrame.new() (default rotation matrix) and look at it.

> print(CFrame.new())
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1

Whoa. What is that! If you don't know, the first three numbers (0,0,0) are the position. What are the last nine actually?

Well, let's learn that ROBLOX's rotation matrix uses a column format. Meaning, it stores its rotation vectors in a vertical form, not horizontal. So let's rewrite the nine numbers to reflect this:

1 0 0
0 1 0
0 0 1

Wanna learn something pretty cool? Remember how I just (literally, just) wrote that the rotation matrix stores vectors in columns? Each column is a vector3 direction.

Direction for what? Axis direction. To be even _more_ simple, facial directions!

The first column (1,0,0) is the direction the RIGHT surface is facing. The second column (0,1,0) is the direction the TOP surface is facing.

However, the last column is not the front surface, it is the BACK surface (0,0,-1). Do this:

> print(CFrame.new().lookVector)
0, 0, -1

Notice how the lookVector (direction the front surface is facing) is 0,0,-1? Like I said, the rotation matrix columns goes RIGHT, TOP, BACK.

Hey! Congratulations! You know what the rotation matrix is now!


== Using :vectorToWorldSpace to Get Facial Directions ==


Many of you might see me use :vectorToWorldSpace to obtain the facial direction vectors. Again, I'm not going to explain the backgrounds of how these things work too much, just how you can use them.

A brief explanation of :vectorToWorldSpace is as follows (take all my explanations as layman's terms and not technically correct by the standards of many coders and other hard-headed people of the universe):

:vectorToWorldSpace will first use a CFrame and then take a Vector3. What does it do? It basically, first, removes the position from the CFrame you give it. Let's say you give it this CFrame:

2, -3, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1

Remember, the first three numbers are position. So, this method will remove the position:

0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1

However, it does not touch the rotation. This means you get a nice CFrame that has, for example, the rotation of the part you gave it BUT NOT THE POSITION.

Using this, we can combine it with the Vector3 you give :vectorToWorldSpace.

It will then return a Vector3 in relation to that rotation. So if we did:

Part.CFrame:vectorToWorldSpace(Vector3.new(1,0,0))

What could you expect to get from that? You'd get the direction of the right surface! Yay!

local rightVector = Part.CFrame:vectorToWorldSpace(Vector3.new(1,0,0))
local leftVector = Part.CFrame:vectorToWorldSpace(Vector3.new(-1,0,0))
local upVector = Part.CFrame:vectorToWorldSpace(Vector3.new(0,1,0))
local downVector = Part.CFrame:vectorToWorldSpace(Vector3.new(0,-1,0))


== Making Other Surface Face a Direction ==


So. We like how we can use CFrame.new(Pos, LookAt) to make a CFrame point at 'LookAt.' But let's imagine you want to make the top surface, or the right surface, point at 'LookAt' instead of the front surface.

Well, now that we know the CFrame rotation matrix is just a set of, basically, directions for each surface. This should be no problem!

We're going to make use the :Cross(). It's a method in the Vector3 library. What does it do? It takes two Vector3s and returns another Vector3 which is perpendicular to BOTH of the given ones. This means it is 90 degrees rotated from both of the given Vector3s. Right-angled. This is exactly what we need.

We'll start off using the world-space 'up' direction. Which is 0,1,0. We'll cross this with our direction to look at. Now that we do this, we have two of our needed 3 axes. Since the world-space up direction is the real direction the top surface of the part will be facing, we'll cross the two vectors we have that work (the 'look at' direction and the ZAxis).

function PointRightSurfaceAt(Pos, LookAt)
local Up = Vector3.new(0,1,0);
local DirectionToLookAt = (LookAt-Pos).unit;

if (DirectionToLookAt.Y > 0.98) then
-- This means the direction to look at is the same as the 'Up'
-- This won't work. Let's just change the 'Up' to something else
Up = Vector3.new(1,0,0);
end

local ZAxis = DirectionToLookAt:Cross(Up)
local Up = ZAxis:Cross(DirectionToLookAt);
return CFrame.new(
Pos.X,Pos.Y,Pos.Z,
DirectionToLookAt.X,Up.X,ZAxis.X,
DirectionToLookAt.Y,Up.Y,ZAxis.Y,
DirectionToLookAt.Z,Up.Z,ZAxis.Z
);
end

And that will give us a CFrame which has the right surface pointing at our 'LookAt' variable! Yay!

To make it work with other axes, don't get confused with the variable names. Just put the "DirectionToLookAt" in any of the columns you want. Just shift the other ones over one. Here's code to make the top surface point at 'LookAt'

function PointRightSurfaceAt(Pos, LookAt)
local Up = Vector3.new(0,1,0);
local DirectionToLookAt = (LookAt-Pos).unit;

if (DirectionToLookAt.Y > 0.98) then
-- This means the direction to look at is the same as the 'Up'
-- This won't work. Let's just change the 'Up' to something else
Up = Vector3.new(1,0,0);
end

local ZAxis = DirectionToLookAt:Cross(Up)
local Up = ZAxis:Cross(DirectionToLookAt);
return CFrame.new(
Pos.X,Pos.Y,Pos.Z,
ZAxis.X,DirectionToLookAt.X,Up.X,
ZAxis.Y,DirectionToLookAt.Y,Up.Y,
ZAxis.Z,DirectionToLookAt.Z,Up.Z
);
end

See? All I did was shift the variables over. I even left the variable names the same (lazy me)!


== USE AXIS ANGLES NOT EULER ANGLES ==


Alright guys, real talk here. I know we're all used to Euler's rotation angles. They may seem simple and easy to us, because we've grown up on them, but they're very bad in programming. It's good to use on the user end, but game developers should know better (come on guys).

First off, why so much hate?

1) Mathematically wise, they're complicated. Have you seen the page on the Wiki about getting Euler angles from a matrix? Geezzzz
2) They have gimbal lock (Okay, we all know this one).
3) They have "infinite" possibilities for every rotation.

#3/#2 is why some of you may experience weird rotation "flips" or odd happenings when dealing with camera manipulation. Why even use Euler's angles for camera manipulation when axis angles are so much simpler.

Also, many of you believe quaternions to be the only way to interpolate.... WRONG. Axis angles are not only faster, but are simpler to comprehend. Quaternions spiral on three planes of numbers, and one complex plane of numbers.

The move to axis angles from Euler angles may seem difficult for some of you. How do you know what axis to rotate on? Well, guys, I have good news! Now that you know about the rotation matrix, YOU CAN EASILY USE AXIS ANGLES INSTEAD!

Let's say you wanted to rotate something on the Y axis of Euler rotation (make it spin around).

Do not use this crappy function:

CFrame.Angles(0,1.57,0);

to change the Y axis. Use axis angles to change the Y axis! First, how do we get the Y axis of the part? Easy!

local YAxis = Part.CFrame:vectorToWorldSpace(Vector3.new(0,1,0))
Part.CFrame = Part.CFrame * CFrame.fromAxisAngle(YAxis, 1.57);

You just saved the networking from having to convert your crappy Euler angles into a matrix using a range of messy trig functions!

Using what's been explained above, you should have no problem realizing how to use axis angles now.

Any questions? Concerns? Hate my post? Hate me? Let me know! (Those last two... I could do without).
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
18 Mar 2015 09:23 PM
Bump
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
18 Mar 2015 09:24 PM
Added to bookmarks for quick insertion into threads.

Oh wait, I also have this, which is an amazingly written and diagrammed article, IMO:
http://wiki.roblox.com/index.php?title=Joint#Programmatic_Creation
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
18 Mar 2015 09:26 PM
OOOOO

Much better idea. I'll put this on my Wiki page.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
18 Mar 2015 09:27 PM
Give me a link to that when you're finished. Also, do you have any cool pages you've seen that explain stuff? I want to compile a huge list of info.
Report Abuse
AbstractMadness is not online. AbstractMadness
Joined: 22 Dec 2014
Total Posts: 20425
18 Mar 2015 09:53 PM
.
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
19 Mar 2015 07:55 AM
Bump
Report Abuse
warspykingAlt is not online. warspykingAlt
Joined: 16 Mar 2015
Total Posts: 37
19 Mar 2015 08:04 AM
Ahem, eLunate is a girl...
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
19 Mar 2015 08:13 AM
Doing a search for "eLunate" on this thread shows that she was never referenced anywhere.
Report Abuse
warspykingAlt is not online. warspykingAlt
Joined: 16 Mar 2015
Total Posts: 37
19 Mar 2015 08:15 AM
"So, some of you guys (I'd say "some of you all," but the fact that there are no girls [all who say so are liars] allows this)"
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
19 Mar 2015 08:16 AM
eLunate is not a guy. :P
Report Abuse
blooo127127 is not online. blooo127127
Joined: 22 Jul 2010
Total Posts: 403
19 Mar 2015 08:18 AM
OP get's a cookie
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
19 Mar 2015 05:10 PM
"Ahem, eLunate is a girl..."

Pfft.
Report Abuse
devTree is not online. devTree
Joined: 26 Dec 2012
Total Posts: 2596
19 Mar 2015 05:22 PM
wat

give me ur roblox wiki page pl0x
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
19 Mar 2015 05:34 PM
@devTree

http://wiki.roblox.com/index.php?title=User:MrNicNac/CFrame_Cheat_Sheet
Report Abuse
ChiefDelta is not online. ChiefDelta
Joined: 05 Nov 2010
Total Posts: 13071
19 Mar 2015 05:37 PM
elunate's a girl

and hey this might be as good as stravant's thread havent read this
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
19 Mar 2015 05:39 PM
"elunate's a girl"

Sorry, but I just don't believe people by word :P It's just my personal opinion.
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
19 Mar 2015 05:40 PM
"nd hey this might be as good as stravant's thread havent read this"

Stravant's thread is more math and understand the operations that matrices have. This is more of a "cheat sheet" for doing some things.
Report Abuse
ChiefDelta is not online. ChiefDelta
Joined: 05 Nov 2010
Total Posts: 13071
19 Mar 2015 05:42 PM
got any plans to make any other threads like these

this is quite nice
Report Abuse
DeviousDeviation is not online. DeviousDeviation
Joined: 28 Dec 2010
Total Posts: 9306
19 Mar 2015 05:43 PM
why have i never learned this

⬡
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
19 Mar 2015 05:45 PM
From eLunate's blurb:

Mrrr~!

I'm Skye, and totally the best cat ever.
I like to script stuff, and then I hate to script stuff.
Resident troll, mama cat and queen of Scripters yay me.


Mew =^•u•^=


---

"mama cat and queen of Scripters"


I script -~ chimmihc
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
19 Mar 2015 05:45 PM
"got any plans to make any other threads like these"

Yeah, I'm rounding up a bunch of other useful vector math techniques and maybe even a few smaller trig techniques for doing cooler things.
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
19 Mar 2015 05:46 PM
"chimmihc"

Again, I don't just take what people say so easily. People like to pretend to be girls for some attention. Mustyoshi and Emess are great examples of two guys who have done this. I'm not calling eLunate a liar, I'm just asserting my aggressive opinion.
Report Abuse
ChiefDelta is not online. ChiefDelta
Joined: 05 Nov 2010
Total Posts: 13071
19 Mar 2015 05:48 PM
uhh chimmic's a guy

i know what you mean drmath i C&G so yeah lots of guys pretend to be girls for money & ranks

but i've met real girls on roblox too (cam & mic so no lying)

if i had to bet i'd say elunate was a girl
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
19 Mar 2015 05:48 PM
@ChiefDelta

I hear ya man. It's just not a big deal to me :P
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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