wiki.sine.space | sinespace

Difference between revisions of "Scripting/SVector"

From wiki.sine.space
Jump to: navigation, search
Line 57: Line 57:
 
''-- prints [5, 15, 5]''}}
 
''-- prints [5, 15, 5]''}}
  
{{ScriptFunction|SVector|Scale|(SVector other);|Multiplies another vector to this, and returns the result}}
+
{{ScriptFunction|SVector|Scale|(SVector other);|Multiplies another vector to this, and returns the result|5=
 +
local vector = Vector.New(0, 10, 0);<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}}
 
{{ScriptFunction|SVector|Scale|(float other);|Multiplies a float to each axis of this, and returns the result}}
 
{{ScriptFunction|SVector|Divide|(SVector other);|Divides another vector to this, and returns the result}}
 
{{ScriptFunction|SVector|Divide|(SVector other);|Divides another vector to this, and returns the result}}

Revision as of 02:44, 23 April 2017

The SVector struct contains a simple 3D vector of X, Y and Z floats

Fields

X

float X { get; set; }

X axis (red axis)

local obj = Space.Host.ExecutingObject;

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

obj.OnStart(onStartMethod);


Y

float Y { get; set; }

Y axis (green axis)

local obj = Space.Host.ExecutingObject;

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

obj.OnStart(onStartMethod);


Z

float Z { get; set; }

Z axis (blue axis)

local obj = Space.Host.ExecutingObject;

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

obj.OnStart(onStartMethod);


Constructors

__new

SVector __new (float x, float y, float z)

Initialises vector from three floats

Vector.New(0, 1, 0);
-- 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)

Add

SVector Add (SVector other);

Adds another vector to this, and returns the result

local vector = Vector.New(0, 10, 0);

local vectorOther = Vector.New(40, 50, 6);
Space.Log(vector.Add(vectorOther));
-- prints [40, 60, 6]

Space.Log(vector + vectorOther);

-- prints [40, 60, 6]


Add

SVector Add (float other);

Adds a float to each axis of this, and returns the result

local vectorB = Vector.New(0, 10, 0);

Space.Log(vectorB.Add(5.0));

-- prints [5, 15, 5]


Scale

SVector Scale (SVector other);

Multiplies another vector to this, and returns the result

local vector = Vector.New(0, 10, 0);

Space.Log(vector.Scale(vectorOther));
-- prints [0, 50, 0]

Space.Log(vector * vectorOther);

-- prints [0, 50, 0]


Scale

SVector Scale (float other);

Multiplies a float to each axis of this, and returns the result

No example provided yet


Divide

SVector Divide (SVector other);

Divides another vector to this, and returns the result

No example provided yet


Divide

SVector Divide (float other);

Divides a float to each axis of this, and returns the result

No example provided yet


Magnitude

float Magnitude { get ; }

Returns the magnitude of this vector

No example provided yet


Normalised

SVector Normalised { get ; }

Returns the normalised version of this vector

No example provided yet


Distance

float Distance (SVector other);

Returns the distance between this vector and other in meters

No example provided yet


SquareDistance

float SquareDistance (SVector other);

Returns the square of the distance between this vector and other in meters, considerably faster than distance()

No example provided yet


InRange

bool InRange (SVector other, float range);

Returns if other is within range meters of this vector, inclusive

No example provided yet


Cross

SVector Cross (SVector other);

Returns the cross product of this vector and other

No example provided yet


Lerp

SVector Lerp (SVector other, float t);

Linear interpolates between this and other based on factor t (0..1)

No example provided yet


Slerp

SVector Slerp (SVector other, float t);

Spherically linear interpolates between this and other based on factor t (0..1)

No example provided yet


MoveTowards

SVector MoveTowards (SVector other, float maxDistance);

Moves this vector closer to other by a maximum of maxDistance units

No example provided yet


Dot

float Dot (SVector other);

Returns the dot product between this and other (note - normalise your vectors first!)

No example provided yet


Static Members

Up

SVector Up { get; }

Equivalent of new SVector(0,1,0)

No example provided yet


Down

SVector Down { get; }

Equivalent of new SVector(0,-1,0)

No example provided yet


Left

SVector Left { get; }

Equivalent of new SVector(-1,0,0)

No example provided yet


Right

SVector Right { get; }

Equivalent of new SVector(1,0,0)

No example provided yet


Forward

SVector Forward { get; }

Equivalent of new SVector(0,0,1)

No example provided yet


Back

SVector Back { get; }

Equivalent of new SVector(0,0,-1)

No example provided yet


Zero

SVector Zero { get; }

Equivalent of new SVector(0,0,0)

No example provided yet


One

SVector One { get; }

Equivalent of new SVector(1,1,1)

No example provided yet


MinValue

SVector MinValue { get; }

Contains the largest possible negative vector

No example provided yet


MaxValue

SVector MaxValue { get; }

Contains the largest possible vector

No example provided yet