|
|
Line 1: |
Line 1: |
− | The SVector struct contains a simple 3D vector of X, Y and Z floats
| + | This page has moved to: https://docs.sine.space/v/scripting/client-scripting/types/svector |
− | | + | |
− | ==Fields==
| + | |
− | {{ScriptFunction|float|X|{ get; set; }|X axis (red axis)|5=
| + | |
− | local obj = Space.Host.ExecutingObject;<br>
| + | |
− | local originalPos = obj.LocalPosition;<br>
| + | |
− | obj.SubscribeToEvents();<br><br>
| + | |
− | local onStartMethod = function()<br>
| + | |
− | Space.Log(originalPos.'''x''');<br>
| + | |
− | ''-- prints the X axis position of this object as a float''<br><br>
| + | |
− | originalPos.'''x''' = 100.0;<br>
| + | |
− | ''-- assigns 100.0 value to the X axis position of this object''<br><br>
| + | |
− | obj.LocalPosition = originalPos;<br>
| + | |
− | ''-- sets the the new position''<br>
| + | |
− | end<br><br>
| + | |
− | obj.OnStart(onStartMethod);}}
| + | |
− | | + | |
− | {{ScriptFunction|float|Y|{ get; set; }|Y axis (green axis)|5=
| + | |
− | local obj = Space.Host.ExecutingObject;<br>
| + | |
− | local originalPos = obj.LocalPosition;<br>
| + | |
− | obj.SubscribeToEvents();<br><br>
| + | |
− | local onStartMethod = function()<br>
| + | |
− | Space.Log(originalPos.'''y''');<br>
| + | |
− | ''-- prints the Y axis position of this object as a float''<br><br>
| + | |
− | originalPos.'''y''' = 100.0;<br>
| + | |
− | ''-- assigns 100.0 value to the Y axis position of this object''<br><br>
| + | |
− | obj.LocalPosition = originalPos;<br>
| + | |
− | ''-- sets the the new position''<br>
| + | |
− | end<br><br>
| + | |
− | obj.OnStart(onStartMethod);}}
| + | |
− | | + | |
− | {{ScriptFunction|float|Z|{ get; set; }|Z axis (blue axis)|5=
| + | |
− | local obj = Space.Host.ExecutingObject;<br>
| + | |
− | local originalPos = obj.LocalPosition;<br>
| + | |
− | obj.SubscribeToEvents();<br><br>
| + | |
− | local onStartMethod = function()<br>
| + | |
− | Space.Log(originalPos.'''z''');<br>
| + | |
− | ''-- prints the Z axis position of this object as a float''<br><br>
| + | |
− | originalPos.'''z''' = 100.0;<br>
| + | |
− | ''-- assigns 100.0 value to the Z axis position of this object''<br><br>
| + | |
− | obj.LocalPosition = originalPos;<br>
| + | |
− | ''-- sets the the new position''<br>
| + | |
− | end<br><br>
| + | |
− | obj.OnStart(onStartMethod);}}
| + | |
− | | + | |
− | ==Constructors==
| + | |
− | {{ScriptFunction|SVector|__new|(float x, float y, float z)|Initialises vector from three floats|5= local newVector = '''Vector.New'''(0, 1, 0);<br>''-- creates a new vector with value [0, 1, 0]''}}
| + | |
− | | + | |
− | ==Members==
| + | |
− | Note: Add/Scale/Divide are also implemented as operators (e.g. A + B, A += B)
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Add|(SVector other);|Adds another vector to this, and returns the result|5=
| + | |
− | local vector = Vector.New(0, 10, 0);<br>
| + | |
− | local vectorOther = Vector.New(40, 50, 6);<br>
| + | |
− | Space.Log(vector.'''Add'''(vectorOther));<br>
| + | |
− | ''-- prints [40, 60, 6]''<br><br>
| + | |
− | Space.Log(vector '''+''' vectorOther);<br>
| + | |
− | ''-- prints [40, 60, 6]''}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Add|(float other);|Adds a float to each axis of this, and returns the result|5=
| + | |
− | local vectorB = Vector.New(0, 10, 0);<br>
| + | |
− | Space.Log(vectorB.'''Add'''(5));<br>
| + | |
− | ''-- prints [5, 15, 5]''}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Scale|(SVector other);|Multiplies another vector to this, and returns the result|5=
| + | |
− | local vector = Vector.New(0, 10, 0);<br>
| + | |
− | local vectorOther = Vector.New(2, 5, 4);<br>
| + | |
− | Space.Log(vector.'''Scale'''(vectorOther));<br>
| + | |
− | ''-- prints [0, 50, 0]''<br><br>
| + | |
− | Space.Log(vector '''*''' vectorOther);<br>
| + | |
− | ''-- prints [0, 50, 0]''}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Scale|(float other);|Multiplies a float to each axis of this, and returns the result|5=
| + | |
− | local vector = Vector.New(0, 10, 0);<br>
| + | |
− | Space.Log(vector.'''Scale'''(5));<br>
| + | |
− | ''-- prints [0, 50, 0]''<br><br>
| + | |
− | Space.Log(vector '''*''' 5);<br>
| + | |
− | ''-- prints [0, 50, 0]''}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Divide|(SVector other);|Divides another vector to this, and returns the result|5=
| + | |
− | local vector = Vector.New(10, 10, 10);<br>
| + | |
− | local vectorOther = Vector.New(2, 2, 2);<br>
| + | |
− | Space.Log(vector.'''Divide'''(vectorOther));<br>
| + | |
− | ''-- prints [5, 5, 5]''<br><br>
| + | |
− | Space.Log(vector '''/''' vectorOther);<br>
| + | |
− | ''-- prints [5, 5, 5]''}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Divide|(float other);|Divides a float to each axis of this, and returns the result|5=
| + | |
− | local vector = Vector.New(10, 10, 10);<br>
| + | |
− | Space.Log(vector.'''Divide'''(2));<br>
| + | |
− | ''-- prints [5, 5, 5]''<br><br>
| + | |
− | Space.Log(vector '''/''' 2);<br>
| + | |
− | ''-- prints [5, 5, 5]''}}
| + | |
− | | + | |
− | {{ScriptFunction|float|Magnitude|{ get ; }|Returns the magnitude of this vector|5=local vector = Vector.New(1.0, 5.0, 0.0);<br>Space.Log('''vector.Magnitude''');<br>''-- prints 5.0990195274353''}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Normalised|{ get ; }|Returns the normalised version of this vector|5=local vector = Vector.New(0.0, 10.0, 0.0);<br>Space.Log('''vector.Normalised''');<br>''-- prints [0, 1, 0]''}}
| + | |
− | | + | |
− | {{ScriptFunction|float|Distance|(SVector other);|Returns the distance between this vector and other in meters|5=
| + | |
− | local vector = Vector.New(3, 3, 3.5);<br>local vectorOther = Vector.New(2, 2, 2);<br>Space.Log('''vector.Distance'''(vectorOther));<br>''-- prints 2.06155276298523''|6=<pre>--the below script will change the objects color to green if you are near the object
| + | |
− | --and change color to red if you are far from the object
| + | |
− | | + | |
− | thisGameObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | function OnUpdate()
| + | |
− | positionAvatar = Space.Scene.PlayerAvatar.GameObject.WorldPosition
| + | |
− | positionObject = thisGameObject.WorldPosition
| + | |
− |
| + | |
− | if positionAvatar.Distance(positionObject) < 5 then
| + | |
− | thisGameObject.Renderer.Material.SetColor("_Color",0,1,0,1)
| + | |
− | else
| + | |
− | thisGameObject.Renderer.Material.SetColor("_Color",1,0,0,1)
| + | |
− | end
| + | |
− |
| + | |
− | end
| + | |
− | | + | |
− | thisGameObject.OnUpdate(OnUpdate)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|float|SquareDistance|(SVector other);|Returns the square of the distance between this vector and other in meters, considerably faster than distance()|5=
| + | |
− | local vector = Vector.New(3, 3, 3.5);<br>local vectorOther = Vector.New(2, 2, 2);<br>Space.Log('''vector.SquareDistance'''(vectorOther));<br>''-- prints 4.25''|6=<pre>--the below script will change the objects color to green if you are near the object
| + | |
− | --and change color to red if you are far from the object
| + | |
− | | + | |
− | thisGameObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | function OnUpdate()
| + | |
− | positionAvatar = Space.Scene.PlayerAvatar.GameObject.WorldPosition
| + | |
− | positionObject = thisGameObject.WorldPosition
| + | |
− |
| + | |
− | if positionAvatar.SquareDistance(positionObject) < 25 then
| + | |
− | thisGameObject.Renderer.Material.SetColor("_Color",0,1,0,1)
| + | |
− | else
| + | |
− | thisGameObject.Renderer.Material.SetColor("_Color",1,0,0,1)
| + | |
− | end
| + | |
− |
| + | |
− | end
| + | |
− | | + | |
− | thisGameObject.OnUpdate(OnUpdate)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|bool|InRange|(SVector other, float range);|Returns if other is within range meters of this vector, inclusive|5=
| + | |
− | local vector = Vector.New(370, 30, 0);<br>
| + | |
− | local vectorOther = Vector.New(372, 30, 0);<br>
| + | |
− | Space.Log(vector.'''InRange'''(vectorOther, 2.0));<br>
| + | |
− | ''-- prints True''<br><br>
| + | |
− | local vector2 = Vector.New(80, 30, 20);<br>
| + | |
− | local vectorOther2 = Vector.New(1, 15, 25);<br>
| + | |
− | Space.Log(vector2.'''InRange'''(vectorOther2, 1.0));<br>
| + | |
− | ''-- prints False''|6=
| + | |
− | <pre>--the below script will change the objects color to green if you are near the object
| + | |
− | --and change color to red if you are far from the object
| + | |
− | --(example: motion sensor lights)
| + | |
− | | + | |
− | thisGameObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | function OnUpdate()
| + | |
− | positionAvatar = Space.Scene.PlayerAvatar.GameObject.WorldPosition
| + | |
− | positionObject = thisGameObject.WorldPosition
| + | |
− |
| + | |
− | if positionAvatar.InRange(positionObject, 5.0) then
| + | |
− | thisGameObject.Renderer.Material.SetColor("_Color",0,1,0,1)
| + | |
− | else
| + | |
− | thisGameObject.Renderer.Material.SetColor("_Color",1,0,0,1)
| + | |
− | end
| + | |
− |
| + | |
− | end
| + | |
− | | + | |
− | thisGameObject.SubscribeToEvents()
| + | |
− | thisGameObject.OnUpdate(OnUpdate)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Cross|(SVector other);|Returns the cross product of this vector and other|5=local vector = Vector.New(0, 1, 0);<br>local vectorOther = Vector.New(1, 0, 0);<br>Space.Log(vector.'''Cross'''(vectorOther));<br>
| + | |
− | ''-- prints [0, 0, -1]''}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Lerp|(SVector other, float t);|Linear interpolates between this and other based on factor t (0..1)|5=
| + | |
− | ''-- Lua Translation of Unity C# Documentation''<br><br>
| + | |
− | ''--[["StartMarker" and "EndMaker" are game objects set up as endpoints.''<br>
| + | |
− | ''These objects are attached to the script in the Object Reference section.''<br>
| + | |
− | ''The name fields in that section should match these names for the example. --]]''<br>
| + | |
− | local startMarker = Space.Host.GetReference("StartMarker");<br>
| + | |
− | local endMarker = Space.Host.GetReference("EndMarker");<br>
| + | |
− | local ball = Space.Host.ExecutingObject;<br>
| + | |
− | local speed = 1.0;<br>
| + | |
− | local startTime;<br>
| + | |
− | local journeyLength;<br>
| + | |
− | ball.SubscribeToEvents();<br><br>
| + | |
− | local onStartMethod = function()<br>
| + | |
− | startTime = Space.Time;<br>
| + | |
− | journeyLength = startMarker.LocalPosition.Distance(endMarker.LocalPosition);<br>
| + | |
− | end<br><br>
| + | |
− | ''-- This ball object will move from the start endpoint object to the end endpoint.''<br>
| + | |
− | local moveBall = function()<br>
| + | |
− | local distCovered = (Space.Time - startTime) * speed;<br>
| + | |
− | local fracJourney = distCovered / journeyLength;<br>
| + | |
− | ball.LocalPosition = startMarker.LocalPosition.'''Lerp'''(endMarker.LocalPosition, fracJourney);<br>
| + | |
− | end<br><br>
| + | |
− | ball.OnStart(onStartMethod);<br>
| + | |
− | ball.OnUpdate(moveBall);
| + | |
− | }}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Slerp|(SVector other, float t);|Spherically linear interpolates between this and other based on factor t (0..1)|5=
| + | |
− | ''-- Lua Translation of Unity C# Documentation''<br><br>
| + | |
− | ''--[["Sunrise" and "Sunset" are game objects set up as endpoints.''<br>
| + | |
− | ''These objects are attached to the script in the Object Reference section.''<br>
| + | |
− | ''The name fields in that section should match these names for the example. --]]''<br>
| + | |
− | local sunrise = Space.Host.GetReference("Sunrise");<br>
| + | |
− | local sunset = Space.Host.GetReference("Sunset");<br><br>
| + | |
− | local ball = Space.Host.ExecutingObject;<br>
| + | |
− | local startTime;<br>
| + | |
− | local journeyTime = 1.0;<br>
| + | |
− | ball.SubscribeToEvents();<br><br>
| + | |
− | local onStartMethod = function()<br>
| + | |
− | startTime = Space.Time;<br>
| + | |
− | end<br><br>
| + | |
− | ''-- This ball object will move from the start endpoint object to the end endpoint.''<br>
| + | |
− | local moveBall = function()<br>
| + | |
− | local center = (sunrise.LocalPosition + sunset.LocalPosition) * 0.5;<br>
| + | |
− | center = center - Vector.Up;<br>
| + | |
− | local riseRelCenter = sunrise.LocalPosition - center;<br>
| + | |
− | local setRelCenter = sunset.LocalPosition - center;<br>
| + | |
− | local fracComplete = (Space.Time - startTime) / journeyTime;<br><br>
| + | |
− | ball.LocalPosition = riseRelCenter.'''Slerp'''(setRelCenter, fracComplete);<br>
| + | |
− | ball.LocalPosition = ball.LocalPosition + center;<br>
| + | |
− | end<br><br>
| + | |
− | ball.OnStart(onStartMethod);<br>
| + | |
− | ball.OnUpdate(moveBall);}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|MoveTowards|(SVector other, float maxDistance);|Moves this vector closer to other by a maximum of maxDistance units|5=
| + | |
− | ''-- Lua Translation of Unity C# Documentation''<br><br>
| + | |
− | ''--[["Target" is a game object set up as a target position.''<br>
| + | |
− | ''This object is attached to the script in the Object Reference section.''<br>
| + | |
− | ''The name field in that section should match this name for the example. --]]''<br>
| + | |
− | local target = Space.Host.GetReference("Target");<br><br>
| + | |
− | local ball = Space.Host.ExecutingObject;<br>
| + | |
− | local speed = 1.0;<br>
| + | |
− | ball.SubscribeToEvents();<br><br>
| + | |
− | local onStartMethod = function()<br>
| + | |
− | startTime = Space.Time;<br>
| + | |
− | end<br><br>
| + | |
− | ''-- This ball object will move towards the target. Negative values for''<br>
| + | |
− | ''-- the maxDistance parameter will push the ball away from the target.''<br>
| + | |
− | local moveBall = function()<br>
| + | |
− | local step = speed * Space.DeltaTime;<br>
| + | |
− | ball.LocalPosition = ball.LocalPosition.'''MoveTowards'''(target.LocalPosition, step);<br>
| + | |
− | end<br><br>
| + | |
− | ball.OnStart(onStartMethod);<br>
| + | |
− | ball.OnUpdate(moveBall);<br><br>
| + | |
− | ''-- NOTE: If you use MoveTowards, the movement will not overshoot the target.''}}
| + | |
− | | + | |
− | {{ScriptFunction|float|Dot|(SVector other);|Returns the dot product between this and other (note - normalise your vectors first!|5=
| + | |
− | local vectorA = Vector.New(0, 1, 0);<br>
| + | |
− | local vectorOtherA = Vector.New(1, 0, 0);<br>
| + | |
− | Space.Log(vectorA.'''Dot'''(vectorOtherA));<br>
| + | |
− | ''-- prints 0''<br><br>
| + | |
− | local vectorB = Vector.New(0, 1, 0);<br>
| + | |
− | local vectorOtherB = Vector.New(0, 1, 0);<br>
| + | |
− | Space.Log(vectorB.'''Dot'''(vectorOtherB));<br>
| + | |
− | ''-- prints 1''<br><br>
| + | |
− | local vectorC = Vector.New(0, 1, 0);<br>
| + | |
− | local vectorOtherC = Vector.New(0, -1, 0);<br>
| + | |
− | Space.Log(vectorC.'''Dot'''(vectorOtherC));<br>
| + | |
− | ''-- prints -1''}}
| + | |
− | | + | |
− | ==Static Members==
| + | |
− | {{ScriptFunction|SVector|Up|{ get; }|Equivalent of new SVector(0,1,0)|5=Space.Log('''Vector.Up''');<br>''-- prints [0, 1, 0]''|6=<pre>--this script will make clicking this object move 2 units up on the world Y axis
| + | |
− | thisObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | OnClick = function()
| + | |
− |
| + | |
− | thisObject.WorldPosition = thisObject.WorldPosition + (Vector.Up * 2)
| + | |
− |
| + | |
− | end
| + | |
− | | + | |
− | | + | |
− | thisObject.AddClickable()
| + | |
− | thisObject.Clickable.OnClick(OnClick)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Down|{ get; }|Equivalent of new SVector(0,-1,0)|5=Space.Log('''Vector.Down''');<br>''-- prints [0, -1, 0]''|6=<pre>--this script will make clicking this object move 2 units down on the world Y axis
| + | |
− | thisObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | OnClick = function()
| + | |
− |
| + | |
− | thisObject.WorldPosition = thisObject.WorldPosition + (Vector.Down * 2)
| + | |
− |
| + | |
− | end
| + | |
− | | + | |
− | | + | |
− | thisObject.AddClickable()
| + | |
− | thisObject.Clickable.OnClick(OnClick)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Left|{ get; }|Equivalent of new SVector(-1,0,0)|5=Space.Log('''Vector.Left''');<br>''-- prints [-1, 0, 0]''|6=<pre>--this script will make clicking this object move 2 units left on the world X axis
| + | |
− | thisObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | OnClick = function()
| + | |
− |
| + | |
− | thisObject.WorldPosition = thisObject.WorldPosition + (Vector.Left * 2)
| + | |
− |
| + | |
− | end
| + | |
− | | + | |
− | | + | |
− | thisObject.AddClickable()
| + | |
− | thisObject.Clickable.OnClick(OnClick)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Right|{ get; }|Equivalent of new SVector(1,0,0)|5=Space.Log('''Vector.Right''');<br>''-- prints [1, 0, 0]''|6=<pre>--this script will make clicking this object move 2 units right on the world X axis
| + | |
− | thisObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | OnClick = function()
| + | |
− |
| + | |
− | thisObject.WorldPosition = thisObject.WorldPosition + (Vector.Right * 2)
| + | |
− |
| + | |
− | end
| + | |
− | | + | |
− | | + | |
− | thisObject.AddClickable()
| + | |
− | thisObject.Clickable.OnClick(OnClick)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Forward|{ get; }|Equivalent of new SVector(0,0,1)|5=Space.Log('''Vector.Forward''');<br>''-- prints [0, 0, 1'']|6=<pre>--this script will make clicking this object move 2 units Forward on the world Z axis
| + | |
− | thisObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | OnClick = function()
| + | |
− |
| + | |
− | thisObject.WorldPosition = thisObject.WorldPosition + (Vector.Forward * 2)
| + | |
− |
| + | |
− | end
| + | |
− | | + | |
− | | + | |
− | thisObject.AddClickable()
| + | |
− | thisObject.Clickable.OnClick(OnClick)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Back|{ get; }|Equivalent of new SVector(0,0,-1)|5=Space.Log('''Vector.Back''');<br>''-- prints [0, 0, -1'']|6=<pre>--this script will make clicking this object move 2 units Back on the world Z axis
| + | |
− | thisObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | OnClick = function()
| + | |
− |
| + | |
− | thisObject.WorldPosition = thisObject.WorldPosition + (Vector.Back * 2)
| + | |
− |
| + | |
− | end
| + | |
− | | + | |
− | | + | |
− | thisObject.AddClickable()
| + | |
− | thisObject.Clickable.OnClick(OnClick)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|Zero|{ get; }|Equivalent of new SVector(0,0,0)|5=Space.Log('''Vector.Zero''');<br>''-- prints [0, 0, 0]''|6=<pre>--clicking this object move it to the center of the region <0,0,0>
| + | |
− | thisObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | OnClick = function()
| + | |
− | | + | |
− | thisObject.WorldPosition = Vector.Zero
| + | |
− | | + | |
− | end
| + | |
− | | + | |
− | | + | |
− | thisObject.AddClickable()
| + | |
− | thisObject.Clickable.OnClick(OnClick)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|One|{ get; }|Equivalent of new SVector(1,1,1)|5=Space.Log('''Vector.One''');<br>''-- prints [1, 1, 1]''|6=<pre>--clicking this object move it to the <1,1,1> coordinates of the region.
| + | |
− | thisObject = Space.Host.ExecutingObject
| + | |
− | | + | |
− | OnClick = function()
| + | |
− | | + | |
− | thisObject.WorldPosition = Vector.One
| + | |
− | | + | |
− | end
| + | |
− | | + | |
− | | + | |
− | thisObject.AddClickable()
| + | |
− | thisObject.Clickable.OnClick(OnClick)</pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|MinValue|{ get; }|Contains the largest possible negative vector|5=Space.Log('''Vector.MinValue''');<br>''-- prints [-3.402823E+38, -3.402823E+38, -3.402823E+38]''|6=<pre></pre>}}
| + | |
− | | + | |
− | {{ScriptFunction|SVector|MaxValue|{ get; }|Contains the largest possible vector|5=Space.Log('''Vector.MaxValue''');<br>
| + | |
− | ''-- prints [3.402823E+38, 3.402823E+38, 3.402823E+38]''6=<pre></pre>}}
| + | |
− | | + | |
− | {{Scripting Navbox}}
| + | |