Roblox Lua C Tutorial

Decided to call this tutorial series Intermediate instead of advanced - I think that was a good call. Lua is a simple, yet powerful scripting language. It is used by many games, including ROBLOX. The ROBLOX specific version of Lua, RBX.Lua, is a great tool to take your games to the next level. This website was created because there are few up-to-date RBX.Lua tutorials. While the ROBLOX wiki is a great reference, it offers few quality tutorials.

Easy Written By marioman1932

This tutorial will tell you about CFrame, and what it is.

Lua Scripting Starter Guide Explained by @DarkSinisterPVP With Help From @Supersaiyan122 Note: I will be updating this post in several weeks from now. When I posted this tutorial, I did not expect that this many people would be learning from it. The update should add much more information, advice, fixes of mistakes I’ve made describing things, overall a huge general improvement! Roblox Lua Tutorials. Everything you need to kickstart your game development career, or scratch up on your skills. Med By higio123 Getting Player Ping. How to get a player's ping using a RemoteFunction Med By idonthackbutiflame How to optimize your game.

CFrame is a hidden property that uses only Position, and Orientation (aka Rotation) which isprobably why it's hidden.

Lets start with changing position with CFrame. It's just like how you would with theposition property, but instead of Vector3.new() it's CFrame.new()

Moving on, we have orientation. This is how you change orientation with CFrame.

The reason we use math.rad() is because you have to change the rotation in Radians, notDegrees. math.rad() takes a degree value, and returns the radian conversion of it.There's a way you can change Position, and Orientation with CFrame at the same time!here's how you do it.

CFrame.new() has a bunch of different arguments:CFrame.new(x,y,z,r00,r01,r02)there are other ones, but these are the ones we need to know.

And there you have it. The CFrame Basics. I will be make a part 2 to show some otherstuff about CFrame.

Easy Written By marioman1932

See marioman1932's profile on Roblox

Please enable JavaScript to view the comments powered by Disqus.

Hard Written By Halalaluyafail3

How CFrames are represented, created, and manipulated.

Lua

If you haven't worked with CFrames before, I recommened reading these articles. They should be a good place to start, and easier to understand than what I have put here.

CFrame datatype wiki page

Understanding CFrames article

CFrames are represented as a 3x4 matrix. (3x3 rotation matrix and a vector combined)

Whenever a 4x4 matrix is needed, the last row is presumed to be 0,0,0,1

The 3x3 rotation matrix should be orthonormal. (columns have a magnitude of 1, and each column is orthogonal to the others)

The x,y,z part (fourth column) of the CFrame represents the position, and the other components represent the rotation.

The first column [r00, r10, r20] represents the direction right relative to the orientation of the CFrame. (.RightVector)The second column [r01, r11, r21] represents the direction up relative to the orientation of the CFrame. (.UpVector)The third column [r02, r12, r22] represents the direction forward relative to the orientation of the CFrame. (-.LookVector)

For each column, roblox has a property to describe it. (.RightVector, .UpVector, .LookVector, .Position (these are all mentioned later))For .LookVector, it is actually [-r02,-r12,-r22], so it effectively describes how to CFrame is oriented backwards.

Tutorial

When you visualize it, it makes more sense. In this picture the red arrow is for the right direction, the blue arrow is for look direction, the green arrow is for up direction, and the grey cube is for position.


The next sections will be about CFrame methods and properties, as well as methods to create a CFrame.

/wii-theme-song-mp3-download/. Most of the math used isn't very obvious, so there will be links to articles which will give a better explanation.

There are many methods that create a CFrame.

CFrame.new()

Creates a CFrame which when you add the 4th row, becomes the identity matrix.

Position is 0,0,0Rotation is the identity matrix

CFrame.new(number x,number y,number z)

Creates a CFrame with position x,y,z, rotation stays as the identity matrix.

CFrame.new(Vector3 vector)

Creates a CFrame with the position being the components of vector.

CFrame.new(Vector3 position,Vector3 lookAt)

Creates a CFrame with position being the position argument, and the rotation being how to should be oriented to look at the lookAt argument. There will be no roll, so if this is desired, an alternative solution must be used.

I won't go over how each element is calculated, but for finding the direct look vector.

This won't be accurate if the positions are the same, so in that case you will want to use the identity matrix.

Unlike what the documentation says, this function is most likely not deprecated. However, it is probably discouraged due not being able to specify roll. (which i mentioned above)

CFrame.new(number x,number y,number z,number r00,number r01,number r02,number r10,number r11,number r12,number r20,number r21,number r22)

Creates a CFrame with all provided arguments.

CFrame.new(number x,number y,number z,number qX,number qY,number qZ,number qW)

I'm not sure how roblox implemented this, but it creates a CFrame from position x,y,z and quaternion qX,qY,qZ,qW

Here is my implementation. (x,y,z,qW,qX,qY,qZ)

If you're interested in why this works:

More about quaternions:

CFrame.fromMatrix(Vector3 pos,Vector3 vX,Vector3 vY [,Vector3 vZ])

The default for vZ is vX:Cross(vY).Unit

Creates a new CFrame with position pos, rightVector vX, upVector vY, and lookVector vZ.

CFrame.fromEulerAnglesXYZ(number x,number y,number z) or CFrame.Angles(number x,number y,number z)

Rotation is in radians.

Creates a new CFrame with position 0,0,0 and angles x,y,z

The angles are converted into a rotation matrix so they work with CFrames.

I'm not sure how roblox does it, so here is my implementation.

If you're interested in why this works:

CFrame.fromEulerAnglesYXZ(number y,number x,number z) or CFrame.fromOrientation(number y,number x,number z)

I'm not sure how roblox implemented this, but it should be pretty similar to the previously mentioned constructor, except angles are applied in Y,X,Z order instead of X,Y,Z

CFrame.fromAxisAngle(Vector3 v,number r)

Creates a CFrame with position 0,0,0 and angles defined by axis angle v,r

The axis angle is converted to a matrix to make it work with CFrames.

I'm not sure how roblox implemented it, but here is my implementation.

If you're interested in why this works

CFrame.Position [Vector3]

The position of the CFrame as a vector.

The elements of the vector are the r03,r13,r23 elements of the CFrame.

CFrame.X [number]

Represents the X part of the position vector.

Element - r03

CFrame.Y [number]

Represents the Y part of the position vector.

Element - r13

CFrame.Z [number]

Represents the Z part of the position vector.

Element - r23

CFrame.RightVector [Vector3]

Represents the 'right-ward' direction of the CFrame.

Elements - r00,r10,r20

CFrame.UpVector [Vector3]

Represents the 'up-ward' direction of the CFrame.

Elements - r01,r11,r21

CFrame.LookVector [Vector3]

Represents the negative 'forward' direction of the CFrame.

Elements - -r02,-r12,-r22

Helpful link:

CFrame+Vector3 [returns: CFrame]

Adds the elements of the vector to the position part of the CFrame.

CFrame-Vector3 [returns: CFrame]

Subtracts the elements of the vector from the position part of the CFrame.

CFrame*Vector3 [returns: Vector3]

Better explained by code

Returns a vector 'moved' relative to cf. (amount defined by v3)

The linked article at the beginning of this section does a good job of explaining what is being done.

CFrame*CFrame [returns: CFrame]

3x4 times 4x4 matrix multiplication.

If you haven't worked with matrices or matrix multiplication I recommend you read:

The resulting matrix will be a 3x4 matrix (what we need for a CFrame)

It is important to note, matrix multiplication isn't commutative. So a times b most likely won't equal b times a

Anyway, onto the matrix multiplication.

I will represent the first cframe as

and the second cframe as

result:

And then after removing the 0 and 1

Code for CFrame multiplication:

CFrame:GetComponents() [returns: 12 numbers]

Returns all 12 components of the CFrame in this order.

r03,r13,r23,r00,r01,r02,r10,r11,r12,r20,r21,r22

The first three are the position. (r03 = x,r13 = y,r23 = z)The fourth, seventh, and tenth are the RightVector.The fifth, eighth, and eleventh are the UpVector.the sixth, ninth, and twelfth are the 'forward' direction. (LookVector is negative 'forward' direction)

CFrame:Inverse() [returns: CFrame]

Returns the inverse of the CFrame.

Roblox relies on how the rotation matrix should be orthonormal, as such the inverse rotation is simply the transpose.

For position, it multiplies the column, and then takes the negative.

This is explained here.

CFrame:ToWorldSpace(CFrame cf) [returns: CFrame]

Equivalent to

CFrame:ToObjectSpace(CFrame cf) [returns: CFrame]

Equivalent to

Will often be useful when you need to solve for B in something like this.

where A is the first CFrame and C is the second CFrame.

B will be A:Inverse()*C because

CFrame:PointToWorldSpace(Vector3 v3) [returns: Vector3]

Roblox Lua C Tutorials

Equivalent to

CFrame:PointToObjectSpace(Vector3 v3) [returns: Vector3]

Equivalent to

CFrame:VectorToWorldSpace(Vector3 v3) [returns: Vector3]

Equivalent to

CFrame:VectorToObjectSpace(Vector3 v3) [returns: Vector3]

Equivalent to

CFrame:ToEulerAnglesXYZ() [returns: 3 numbers]

Converts the rotation matrix to euler angles.

Angles returned are in radians.

I'm not sure how roblox implemented this, but here are some pages about getting euler angles from a rotation matrix.

CFrame:ToEulerAnglesYXZ() or CFrame:ToOrientation() [returns: 3 numbers]

Converts the rotation matrix to euler angles.

Angles returned are in radians.

I'm not sure how roblox implemented this, see articles mentioned above.

CFrame:ToAxisAngle() [returns: Vector3, number]

Converts the rotation matrix to axis angle.

I'm not sure how roblox implemented this, but here is an article about converting matrix to axis angle.

Roblox Lua Tutorial

CFrame:Lerp(CFrame goal,number alpha) [returns: CFrame]

Linearly interpolates between CFrame and goal by amount alpha.

I have no idea how roblox implemented this, but here is my implementation.

I got this from modifying this module

made by stravant

How to think about CFrames (by AxisAngle)

I mentioned quaternions briefly, but here is another article about quaternions and rotation (by suremark)

Hard Written By Halalaluyafail3

Roblox Lua Programming Tutorial

See Halalaluyafail3's profile on Roblox

Lua C Scripts Roblox Pastebin

Please enable JavaScript to view the comments powered by Disqus.