Vector3: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mr Doom Bringer
No edit summary
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">"
 
(34 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{CatUp|Data Types}} {{CatUp|Scripting}}
{{Map|Scripting|Data Types}}
__TOC__
A {{type|Vector3}} is a {{type|userdata}} that holds three values inside it. This doesn't have to be a 3D position, it could be a size as well.


==Vector3==
The three values are an X ordinate, a Y ordinate, and a 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 {{`|(x, y)}}, sometimes written as {{Vector2|x|y}}.


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
This is a 2D vector, or in Roblox, a {{type|Vector2}}. We need a 3D vector, or {{type|Vector3}}. So we add another value, a '''Z''' value. This gives us {{`|(x, y, z)}}, which can also be written as {{Vector3|x|y|z}}


{{LeftNoticeStart}}
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 {{type|Vector3}},
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
*X is sideways component, or width.
*Y is the vertical component, or height.
*Z is the forward component, or depth.


(1, 5)
==Using Vector3s==
 
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===
===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]]
Open up a new place with a part.


In the Command Line, type in this bit here and hit enter:
In the [[Command Bar|command bar]], type in this bit here and hit enter:
{{CodeExample}}game.Workspace.Part.Position = Vector3.new(0, 50, 0)
<syntaxhighlight lang="lua">
|}
Workspace.Part.Position = Vector3.new(0, 50, 0)
</syntaxhighlight>


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.
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 changed where the brick is, by changing it's [[Position]]. As you should know from the [[Absolute beginner's guide to scripting #Conditions | absolute beginner's guide to scripting]], 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 {{type|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="text-align:center; {{border-radius|4px}}; 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;"
|-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
|    Property you're changing              ||      Set To    || Value you want to set it to
|-style=" margin:0; background:#EEEEEE; padding:0.2em 0.4em;"
|-style=" margin:0; background:#EEEEEE; padding:0.2em 0.4em;"
|game.Workspace.Part.Position || = ||Vector3.new(0, 50, 0)
|Workspace.Part.Position || = ||Vector3.new(0, 50, 0)
|-
|-
|    The Position of "Part" ||                ||  Set to this
|    The Position of "Part" ||                ||  Set to this
|}
|}


{{LeftNoticeStart}}
{{EmphasisBox|
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 [[CFrame]]s.
Moving things around with the [[Position]] property comes with built-in collision detection. Let's 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 with the [[CFrame]] property instead.
|}
}}
 
 


==[[Constructors]]==
==Constructors==


{|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;"
{| class="wikitable"
! Constructor !! Description
! Constructor !! Description
|-
|-
| Vector3.new('''x''', '''y''', '''z''') || Creates a new Vector3 using ordinates '''x''', '''y''', '''z'''.
| Vector3.new(<var>x</var>, <var>y</var>, <var>z</var>) || Creates a new {{type|Vector3}} using coordinates <var>x</var>, <var>y</var>, <var>z</var>.
|-
| Vector3.FromNormalId({{type|NormalId|NormalId (Enum)}} <var>normalId</var>) || Creates a unit {{type|Vector3}} in a particular facing direction.
|-
| Vector3.FromAxis({{type|Axis|Axis (Enum)}} <var>axis</var>) || Creates a unit {{type|Vector3}} for a particular Axis.
|}
|}


{|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;"
== Methods ==
{| class="wikitable"
! Member Function !! Description
! 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:Lerp({{type|Vector3}} <var>goal</var>, {{type|number}} <var>alpha</var>) || Returns a {{type|Vector3}} lerped between this {{type|Vector3}} and the <var>goal</var>. <var>alpha</var> should be between 0 and 1.
|-
|-
| ''Vector3'':pointToWorldSpace(Vector3) || returns a Vector3 transformed from Object to World coordinates. Also works with tuples
| Vector3:Dot(Vector3) || Returns the vector dot product of the two vectors
|-
|-
| ''Vector3'':pointToObjectSpace(Vector3) || returns a Vector3 transformed from World to Object coordinates. Also works with tuples
| Vector3:Cross(Vector3) || Returns the vector cross product of the two vectors
|}
 
== Properties ==
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 vectors with such changes, or apply an operation, seen in the next section.
 
{| class="wikitable"
! Property !! Type !! Description
|-
|-
| ''Vector3'':vectorToWorldSpace(Vector3) || returns a Vector3 rotated from Object to World coordinates. Also works with tuples
| Vector3.'''x''' || {{type|number}} || The x-coordinate
|-
|-
| ''Vector3'':vectorToObjectSpace(Vector3) || returns a Vector3 rotated from World to Object coordinates. Also works with tuples
| Vector3.'''y''' || {{type|number}} || The y-coordinate
|-
| Vector3.'''z''' || {{type|number}} || The z-coordinate
|-
| Vector3.'''unit''' || {{type|Vector3}} || A normalized copy of the vector
|-
| Vector3.'''magnitude'''|| {{type|number}} || The length of the vector
|}
|}


== Operators ==


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
{| class="wikitable"
 
! Operator !! Description
{|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;"
|-
! Property !! Type !! Description
| {{type|Vector3}} + {{type|Vector3}} || returns Vector3 translated (slid) by Vector3
|-
| {{type|Vector3}} - {{type|Vector3}} || returns Vector3 translated (slid) by -Vector3 (also gives relative position of one to the other)
|-
| {{type|number}} * {{type|Vector3}}  || returns Vector3 with each component multiplied by number
|-
|-
| Vector3.'''x''' || [[Number]] || The x-coordinate
| {{type|Vector3}} * {{type|number}}  || returns Vector3 with each component multiplied by number
|-
|-
| Vector3.'''y''' || [[Number]] || The y-coordinate
| {{type|number}} / {{type|Vector3}}  || returns Vector3 with Number divided by each component
|-
|-
| Vector3.'''z''' || [[Number]] || The z-coordinate
| {{type|Vector3}} / {{type|number}}  || returns Vector3 with each component divided by number
|-
|-
| Vector3.'''unit''' || [[Vector3]] || A normalized copy of the vector
| {{type|Vector3}} * {{type|Vector3}} || returns Vector3 with each component multiplied by corresponding component
|-
|-
| Vector3.'''magnitude'''|| [[Number]] || The length of the vector
| {{type|Vector3}} / {{type|Vector3}} || returns Vector3 with each component divided by corresponding component
|}
|}
See [[CFrame]] for additional {{type|Vector3}} operators.


== Advanced insight ==


In many game engines and graphics engines, a {{type|Vector3}} value of {{Vector3|0|0|1}} would be pointed straight up. This means that the Z axis is also pointed straight up. This is not the case in ROBLOX, where {{Vector3|0|1|0}} is pointed up, that is, the Y axis.


== See Also ==
== See also ==


*[[CFrame]]
*[[CFrame]]
*[[Part]]
*[[Part]]
*[[Properties]]
*[[Properties]]
*[[Vector2]]
[[Category:Data types]]

Latest revision as of 04:43, 27 April 2023

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

The three values are an X ordinate, a Y ordinate, and a 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 (x, y), sometimes written as

x
y

. This is a 2D vector, or in Roblox, a Vector2. We need a 3D vector, or Vector3. So we add another value, a Z value. This gives us (x, y, z), which can also be written as

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 sideways component, or width.
  • Y is the vertical component, or height.
  • Z is the forward component, or depth.

Using Vector3s

Moving things around

Open up a new place with a part.

In the command bar, type in this bit here and hit enter:

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 changed where the brick is, by changing it's Position. As you should know from the absolute beginner's guide to scripting, 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
Workspace.Part.Position = Vector3.new(0, 50, 0)
The Position of "Part" Set to this

Moving things around with the Position property comes with built-in collision detection. Let's 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 with the CFrame property instead.

Constructors

Constructor Description
Vector3.new(x, y, z) Creates a new Vector3 using coordinates x, y, z.
Vector3.FromNormalId(NormalId (Enum) normalId) Creates a unit Vector3 in a particular facing direction.
Vector3.FromAxis(Axis (Enum) axis) Creates a unit Vector3 for a particular Axis.

Methods

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:Dot(Vector3) Returns the vector dot product of the two vectors
Vector3:Cross(Vector3) Returns the vector cross product of the two vectors

Properties

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 vectors with such changes, or apply an operation, seen in the next section.

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

Operators

Operator Description
Vector3 + Vector3 returns Vector3 translated (slid) by Vector3
Vector3 - Vector3 returns Vector3 translated (slid) by -Vector3 (also gives relative position of one to the other)
number * Vector3 returns Vector3 with each component multiplied by number
Vector3 * number returns Vector3 with each component multiplied by number
number / Vector3 returns Vector3 with Number divided by each component
Vector3 / number returns Vector3 with each component divided by number
Vector3 * Vector3 returns Vector3 with each component multiplied by corresponding component
Vector3 / Vector3 returns Vector3 with each component divided by corresponding component

See CFrame for additional Vector3 operators.

Advanced insight

In many game engines and graphics engines, a Vector3 value of

0
0
1

would be pointed straight up. This means that the Z axis is also pointed straight up. This is not the case in ROBLOX, where

0
1
0

is pointed up, that is, the Y axis.

See also