Ashasekayi (Talk | contribs) |
Ashasekayi (Talk | contribs) |
||
Line 208: | Line 208: | ||
{{ScriptFunction|SVector|Right|{ get; }|Equivalent of new SVector(1,0,0)|5=Space.Log('''Vector.Right''');<br>''-- prints [1, 0, 0]''}} | {{ScriptFunction|SVector|Right|{ get; }|Equivalent of new SVector(1,0,0)|5=Space.Log('''Vector.Right''');<br>''-- prints [1, 0, 0]''}} | ||
− | {{ScriptFunction|SVector|Forward|{ get; }|Equivalent of new SVector(0,0,1)}} | + | {{ScriptFunction|SVector|Forward|{ get; }|Equivalent of new SVector(0,0,1)|5=Space.Log('''Vector.Forward''');<br>''-- prints [0, 0, 1'']}} |
− | {{ScriptFunction|SVector|Back|{ get; }|Equivalent of new SVector(0,0,-1)}} | + | |
− | {{ScriptFunction|SVector|Zero|{ get; }|Equivalent of new SVector(0,0,0)}} | + | {{ScriptFunction|SVector|Back|{ get; }|Equivalent of new SVector(0,0,-1)|5=Space.Log('''Vector.Back''');<br>''-- prints [0, 0, -1'']}} |
− | {{ScriptFunction|SVector|One|{ get; }|Equivalent of new SVector(1,1,1)}} | + | |
+ | {{ScriptFunction|SVector|Zero|{ get; }|Equivalent of new SVector(0,0,0)|5=Space.Log('''Vector.Zero''');<br>''-- prints [0, 0, 0]''}} | ||
+ | |||
+ | {{ScriptFunction|SVector|One|{ get; }|Equivalent of new SVector(1,1,1)|5=Space.Log('''Vector.One''');<br>-- prints [1, 1, 1]}} | ||
+ | |||
{{ScriptFunction|SVector|MinValue|{ get; }|Contains the largest possible negative vector}} | {{ScriptFunction|SVector|MinValue|{ get; }|Contains the largest possible negative vector}} | ||
{{ScriptFunction|SVector|MaxValue|{ get; }|Contains the largest possible vector}} | {{ScriptFunction|SVector|MaxValue|{ get; }|Contains the largest possible vector}} | ||
{{Scripting Navbox}} | {{Scripting Navbox}} |
The SVector struct contains a simple 3D vector of X, Y and Z floats
X axis (red axis)
local originalPos = obj.LocalPosition;
obj.SubscribeToEvents();
local onStartMethod = function()
Space.Log(originalPos.x);
-- prints the X axis position of this object as a float
originalPos.x = 100.0;
-- sets the X axis position of this object to 100.0
end
Y axis (green axis)
local originalPos = obj.LocalPosition;
obj.SubscribeToEvents();
local onStartMethod = function()
Space.Log(originalPos.y);
-- prints the Y axis position of this object as a float
originalPos.y = 100.0;
-- sets the Y axis position of this object to 100.0
end
Z axis (blue axis)
local originalPos = obj.LocalPosition;
obj.SubscribeToEvents();
local onStartMethod = function()
Space.Log(originalPos.z);
-- prints the Z axis position of this object as a float
originalPos.z = 100.0;
-- sets the Z axis position of this object to 100.0
end
Initialises vector from three floats
Note: Add/Scale/Divide are also implemented as operators (e.g. A + B, A += B)
Adds another vector to this, and returns the result
local vectorOther = Vector.New(40, 50, 6);
Space.Log(vector.Add(vectorOther));
-- prints [40, 60, 6]
Space.Log(vector + vectorOther);
Adds a float to each axis of this, and returns the result
Space.Log(vectorB.Add(5));
Multiplies another vector to this, and returns the result
local vectorOther = Vector.New(2, 5, 4);
Space.Log(vector.Scale(vectorOther));
-- prints [0, 50, 0]
Space.Log(vector * vectorOther);
Multiplies a float to each axis of this, and returns the result
Space.Log(vector.Scale(5));
-- prints [0, 50, 0]
Space.Log(vector * 5);
Divides another vector to this, and returns the result
local vectorOther = Vector.New(2, 2, 2);
Space.Log(vector.Divide(vectorOther));
-- prints [5, 5, 5]
Space.Log(vector / vectorOther);
Divides a float to each axis of this, and returns the result
Space.Log(vector.Divide(2));
-- prints [5, 5, 5]
Space.Log(vector / 2);
Returns the magnitude of this vector
Returns the normalised version of this vector
Returns the distance between this vector and other in meters
Returns the square of the distance between this vector and other in meters, considerably faster than distance()
Returns if other is within range meters of this vector, inclusive
local vectorOther = Vector.New(372, 30, 0);
Space.Log(vector.InRange(vectorOther, 2.0));
-- prints True
local vector2 = Vector.New(80, 30, 20);
local vectorOther2 = Vector.New(1, 15, 25);
Space.Log(vector2.InRange(vectorOther2, 1.0));
Returns the cross product of this vector and other
Linear interpolates between this and other based on factor t (0..1)
--[["StartMarker" and "EndMaker" are game objects set up as endpoints.
These objects are attached to the script in the Object Reference section.
The name fields in that section should match these names for the example. --]]
local startMarker = Space.Host.GetReference("StartMarker");
local endMarker = Space.Host.GetReference("EndMarker");
local ball = Space.Host.ExecutingObject;
local speed = 1.0;
local startTime;
local journeyLength;
ball.SubscribeToEvents();
local onStartMethod = function()
startTime = Space.Time;
journeyLength = startMarker.LocalPosition.Distance(endMarker.LocalPosition);
end
-- This ball object will move from the start endpoint object to the end endpoint.
local moveBall = function()
local distCovered = (Space.Time - startTime) * speed;
local fracJourney = distCovered / journeyLength;
ball.LocalPosition = startMarker.LocalPosition.Lerp(endMarker.LocalPosition, fracJourney);
end
ball.OnStart(onStartMethod);
Spherically linear interpolates between this and other based on factor t (0..1)
--[["Sunrise" and "Sunset" are game objects set up as endpoints.
These objects are attached to the script in the Object Reference section.
The name fields in that section should match these names for the example. --]]
local sunrise = Space.Host.GetReference("Sunrise");
local sunset = Space.Host.GetReference("Sunset");
local ball = Space.Host.ExecutingObject;
local startTime;
local journeyTime = 1.0;
ball.SubscribeToEvents();
local onStartMethod = function()
startTime = Space.Time;
end
-- This ball object will move from the start endpoint object to the end endpoint.
local moveBall = function()
local center = (sunrise.LocalPosition + sunset.LocalPosition) * 0.5;
center = center - Vector.Up;
local riseRelCenter = sunrise.LocalPosition - center;
local setRelCenter = sunset.LocalPosition - center;
local fracComplete = (Space.Time - startTime) / journeyTime;
ball.LocalPosition = riseRelCenter.Slerp(setRelCenter, fracComplete);
ball.LocalPosition = ball.LocalPosition + center;
end
ball.OnStart(onStartMethod);
Moves this vector closer to other by a maximum of maxDistance units
--[["Target" is a game object set up as a target position.
This object is attached to the script in the Object Reference section.
The name field in that section should match this name for the example. --]]
local target = Space.Host.GetReference("Target");
local ball = Space.Host.ExecutingObject;
local speed = 1.0;
ball.SubscribeToEvents();
local onStartMethod = function()
startTime = Space.Time;
end
-- This ball object will move towards the target. Negative values for
-- the maxDistance parameter will push the ball away from the target.
local moveBall = function()
local step = speed * Space.DeltaTime;
ball.LocalPosition = ball.LocalPosition.MoveTowards(target.LocalPosition, step);
end
ball.OnStart(onStartMethod);
ball.OnUpdate(moveBall);
Returns the dot product between this and other (note - normalise your vectors first!
local vectorOtherA = Vector.New(1, 0, 0);
Space.Log(vectorA.Dot(vectorOtherA));
-- prints 0
local vectorB = Vector.New(0, 1, 0);
local vectorOtherB = Vector.New(0, 1, 0);
Space.Log(vectorB.Dot(vectorOtherB));
-- prints 1
local vectorC = Vector.New(0, 1, 0);
local vectorOtherC = Vector.New(0, -1, 0);
Space.Log(vectorC.Dot(vectorOtherC));
Equivalent of new SVector(0,1,0)
Equivalent of new SVector(0,-1,0)
Equivalent of new SVector(-1,0,0)
Equivalent of new SVector(1,0,0)
Equivalent of new SVector(0,0,1)
Equivalent of new SVector(0,0,-1)
Equivalent of new SVector(0,0,0)
Equivalent of new SVector(1,1,1)
Contains the largest possible negative vector
Contains the largest possible vector
|