Vector3: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mr Doom Bringer
No edit summary
>Mr Doom Bringer
No edit summary
Line 1: Line 1:
Vector3 handles inputs that need three values. It's not a hard concept once you get passed that.
A Vector3 is a number that holds three values inside it. This doesn't have to be a 3D position, it could be a size as well


Vector3 has three values, an X ordinate, Y ordinate and Z ordinate. It represents a position in 3D space, just as a coordinate plane that you use in school, and can be in anything that needs to have a set of three values in order to function. (For example an object [[Position (Property)|position]], [[Size (Property)|size]], et cetera).  
{{LeftNoticeStart}}
Vector3 has three values, an X ordinate, Y ordinate and Z ordinate. They're kind of like those coordinates you use in school on graphs. I'm sure you've seen something like


(1, 5)


All [[Property|properties] are read-only. However, they can be changed via [[Script|scripts]].
somewhere before. This means that on a graph you go to the right 1, and then up 5. That's because a coordinate uses an X and a Y value. It looks like this
{| border="1"
 
! Property !! Type !! Description
(x, y)
 
Now you have a [[Vector2]]. We need a '''Vector3'''. So the difference is we add another value, a '''Z''' value.
 
(x, y, z)
 
That's really all there is to it. These numbers can be used for the [[Position]] of things, the [[Size]] of things, or anything else that needs 3 numbers to work. When people talk about the values inside a Vector3,
*X is horizontal, or width.
*Y is vertical, or height
*Z is depth
|}
 
==Using Vectors3s==
 
===Moving things around===
 
Open up a new place with a part. [[Basic Test Map|Click here for a quick guide on how to set up a script testing Place]]
 
In the Command Line, type in this bit here and hit enter:
{{CodeExample}}game.Workspace.Part.Position = Vector3.new(0, 50, 0)
|}
 
You should see that the brick moved up a good distance, you may need to move the camera to see it. What you just did is change where the brick is, by changing it's [[Position (Property|Position]]. As you should know from the [[basic scripting guide]], you changed the Position by using the equal sign. You set Part.Position to a new location by using the Vector3.new ''constructor.'' You ''constructed'' a new Vector3 using 3 different values. This told the Lua engine to set the brick's Position to 0, 50, 0, making the brick move to that position.
 
{|style="text-align:center; -webkit-border-radius: 4px; -moz-border-radius: 4px; height: 100%; background-color: #ffdddd; border-top: solid 2px #ff0000; border-left: solid 2px #ff0000; border-bottom: solid 2px #aa0000; border-right: solid 2px #aa0000; margin: 6px; margin-right: 10px; margin-left: 10px; clear: none; padding: 2px;"
|-style=" margin:0; background:#b32e1c; font-weight:bold; color:#FFFFFF; padding:0.2em 0.4em;"
|    Property you're changing              ||      Set To    || Value you want to set it to
|-style=" margin:0; background:#EEEEEE; padding:0.2em 0.4em;"
|game.Workspace.Part.Position || = ||Vector3.new(0, 50, 0)
|-
|-
| <code>x</code> || number ||| the x-coordinate
|   The Position of "Part" ||               || Set to this
|-
| <code>y</code> || number ||| the y-coordinate
|-
| <code>z</code> || number ||| the z-coordinate
|-
| <code>unit</code> || Vector3 ||| a normalized copy of the vector
|-
| <code>magnitude</code>|| number ||| the length of the vector
|}
|}


{| border="1"
{{LeftNoticeStart}}
! Member Function !! Description
Moving things around with Vector3s comes with built-in collision detection. Lets say you have a huge brick, and you try to move another brick inside of it. Instead of appearing inside of the solid brick, the second brick will pop up on top of the large one right above where it's trying to get to. If you don't want this to happen, you can move objects around without this using [[CFrames]]
|-
| <code>Vector3 lerp(Vector3 goal, number alpha)</code> || returns a Vector3 lerped between this Vector3 and the goal. alpha should be between 0 and 1
|}
|}




==Vector3==
Vector3 is three values. An X (Horizontal), a Y (Vertical), and a Z (Depth). Learning Vector3 is the first step to learning how to move a brick and/or a character.


Example:
==[[Constructors]]


A brick is on the position line of 0,10,0. It has three vectors (x=0, y=10, z=0).  Now let's convert it to Vector3.
{|border="1" cellspacing="5"  style=" -webkit-border-radius: 4px; -moz-border-radius: 4px; height: 100%; background-color: #FFFFFF; border-top: dashed 2px #ff0000; border-left: dashed 2px #ff0000; border-bottom: dashed 2px #aa0000; border-right: dashed 2px #aa0000; margin: 6px; margin-right: 10px; margin-left: 10px; clear: none; padding: 2px;"
|-
| Vector3.new('''x''', '''y''', '''z''') || Creates a new Vector3 using ordinates '''x''', '''y''', '''z'''.
|}


<pre>
{|border="1" cellspacing="5"  style=" -webkit-border-radius: 4px; -moz-border-radius: 4px; height: 100%; background-color: #FFFFFF; border-top: dashed 2px #ff0000; border-left: dashed 2px #ff0000; border-bottom: dashed 2px #aa0000; border-right: dashed 2px #aa0000; margin: 6px; margin-right: 10px; margin-left: 10px; clear: none; padding: 2px;"
pos = Vector3.new(0,10,0)
! Member Function !! Description
</pre>
|-
| ''Vector3'':lerp(Vector3 '''goal''', number '''alpha''') || Returns a Vector3 lerped between this Vector3 and the '''goal'''. '''Alpha''' should be between 0 and 1
|-
| ''Vector3'':pointToWorldSpace(Vector3) || returns a Vector3 transformed from Object to World coordinates. Also works with tuples
|-
| ''Vector3'':pointToObjectSpace(Vector3) || returns a Vector3 transformed from World to Object coordinates. Also works with tuples
|-
| ''Vector3'':vectorToWorldSpace(Vector3) || returns a Vector3 rotated from Object to World coordinates. Also works with tuples
|-
| ''Vector3'':vectorToObjectSpace(Vector3) || returns a Vector3 rotated from World to Object coordinates. Also works with tuples
|}


You don't need to put it in a variable, it is just that a variable is much easier to use.  I will teach you about "Vector3.new" later on.  You see the term "Vector3.new", then 3 numbers seperated by commas inside two parentheses.  The "0" is the x vector, the "10" is the y vector, and the other "0" is the z vector (Vector3.new(x, y, z).


All of these properties are Read Only (you can't just set them Vector3.x = 5, it doesn't work) but you can create new


===Creating New Vectors===
{|border="1" cellspacing="5"  style=" -webkit-border-radius: 4px; -moz-border-radius: 4px; height: 100%; background-color: #FFFFFF; border-top: dashed 2px #ff0000; border-left: dashed 2px #ff0000; border-bottom: dashed 2px #aa0000; border-right: dashed 2px #aa0000; margin: 6px; margin-right: 10px; margin-left: 10px; clear: none; padding: 2px;"
So now you want to teleport bricks?  Here, we'll talk about "Vector3.new". When changing an object's position, you either use [[CFrame]] or "Vector3.new".
! Property !! Type !! Description
|-
| Vector3.'''x''' || [[Number]] || The x-coordinate
|-
| Vector3.'''y''' || [[Number]] || The y-coordinate
|-
| Vector3.'''z''' || [[Number]] || The z-coordinate
|-
| Vector3.'''unit''' || [[Vector3]] || A normalized copy of the vector
|-
| Vector3.'''magnitude'''|| [[Number]] || The length of the vector
|}


<pre>
brick = game.Workspace.Part
brick.Position = Vector3.new(0,10,0) --Sets the part's x, y, and z coordinates.
</pre>


In that example, we have Vector3.new(0,10,0), where X=0, Y=10, and Z=0.


== See Also ==
== See Also ==

Revision as of 22:25, 23 March 2010

A Vector3 is a number that holds three values inside it. This doesn't have to be a 3D position, it could be a size as well

Vector3 has three values, an X ordinate, Y ordinate and Z ordinate. They're kind of like those coordinates you use in school on graphs. I'm sure you've seen something like

(1, 5)

somewhere before. This means that on a graph you go to the right 1, and then up 5. That's because a coordinate uses an X and a Y value. It looks like this

(x, y)

Now you have a Vector2. We need a Vector3. So the difference is we add another value, a Z value.

(x, y, z)

That's really all there is to it. These numbers can be used for the Position of things, the Size of things, or anything else that needs 3 numbers to work. When people talk about the values inside a Vector3,

  • X is horizontal, or width.
  • Y is vertical, or height
  • Z is depth

Using Vectors3s

Moving things around

Open up a new place with a part. Click here for a quick guide on how to set up a script testing Place

In the Command Line, type in this bit here and hit enter: Template:CodeExamplegame.Workspace.Part.Position = Vector3.new(0, 50, 0) |}

You should see that the brick moved up a good distance, you may need to move the camera to see it. What you just did is change where the brick is, by changing it's Position. As you should know from the basic scripting guide, you changed the Position by using the equal sign. You set Part.Position to a new location by using the Vector3.new constructor. You constructed a new Vector3 using 3 different values. This told the Lua engine to set the brick's Position to 0, 50, 0, making the brick move to that position.

Property you're changing Set To Value you want to set it to
game.Workspace.Part.Position = Vector3.new(0, 50, 0)
The Position of "Part" Set to this

Moving things around with Vector3s comes with built-in collision detection. Lets say you have a huge brick, and you try to move another brick inside of it. Instead of appearing inside of the solid brick, the second brick will pop up on top of the large one right above where it's trying to get to. If you don't want this to happen, you can move objects around without this using CFrames


==Constructors

Vector3.new(x, y, z) Creates a new Vector3 using ordinates x, y, z.
Member Function Description
Vector3:lerp(Vector3 goal, number alpha) Returns a Vector3 lerped between this Vector3 and the goal. Alpha should be between 0 and 1
Vector3:pointToWorldSpace(Vector3) returns a Vector3 transformed from Object to World coordinates. Also works with tuples
Vector3:pointToObjectSpace(Vector3) returns a Vector3 transformed from World to Object coordinates. Also works with tuples
Vector3:vectorToWorldSpace(Vector3) returns a Vector3 rotated from Object to World coordinates. Also works with tuples
Vector3:vectorToObjectSpace(Vector3) returns a Vector3 rotated from World to Object coordinates. Also works with tuples


All of these properties are Read Only (you can't just set them Vector3.x = 5, it doesn't work) but you can create new

Property Type Description
Vector3.x Number The x-coordinate
Vector3.y Number The y-coordinate
Vector3.z Number The z-coordinate
Vector3.unit Vector3 A normalized copy of the vector
Vector3.magnitude Number The length of the vector


See Also