wiki.sine.space | sinespace

Difference between revisions of "Scripting/SRigidbody"

From wiki.sine.space
Jump to: navigation, search
(Added Velocity and WorldCenterOfMass ; more grammar fixing)
m
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
The Rigidbody class works with the physics of the object. Requires the Rigidbody component to function.
 
The Rigidbody class works with the physics of the object. Requires the Rigidbody component to function.
 +
 +
==Members==
 +
 +
{{ScriptFunction|void|AddExplosionForce|(float explosionForce, SVector explosionPosition, float explosionRadius, float upwardsModifier)|Applies a force to a rigidbody that simulates explosion effects. Other rigidbodies will be affected by the explosion within its radius - the closer they are to the explosionPosition, the stronger the force will be exerted on them.|5=
 +
local obj = Space.Host.ExecutingObject;<br><br>
 +
obj.Rigidbody.AddExplosionForce (300, obj.WorldPosition, 10, 20)<br>
 +
''-- The explosion will occur at the location of the ExecutingObject.'' <br>
 +
''-- Place other rigidbodies around it to observe the effect!''}}
 +
 +
{{ScriptFunction|void|AddForce|(SVector force)|Adds a force to the Rigidbody that is continuously exerted on it in the given direction.|5=
 +
local obj = Space.Host.ExecutingObject;<br><br>
 +
obj.Rigidbody.AddForce (Vector.New(0,100,0));<br>
 +
''-- Now the rigidbody is under a continuous force directed upwards (Y direction)''}}
 +
 +
{{ScriptFunction|void|AddForceAtPosition|(SVector force, SVector position)|Adds a force to the Rigidbody at a given position (should be within the range of the rigidbody for a realistic result). Thus, both a torque and force are applied to the object.|5=
 +
local obj = Space.Host.ExecutingObject;<br>
 +
local applyForceHere = Vector.New(obj.WorldPosition.x-0.5,obj.WorldPosition.y-0.5,obj.WorldPosition.z-0.5)<br>
 +
''-- This vector is equivalent to one of the lower corners of a 1x1x1 cube.''<br><br>
 +
obj.Rigidbody.AddForceAtPosition (Vector.New(0,40,0), applyForceHere);<br>
 +
''-- The object is experiencing an effect similar to being tipped upwards at the aforementioned corner.''}}
 +
 +
{{ScriptFunction|void|AddRelativeTorque| (SVector torque)|Adds a torque to the rigidbody relative to the local coordinate system. |5=
 +
local obj = Space.Host.ExecutingObject;<br><br>
 +
obj.Rigidbody.AddRelativeTorque (Vector.New(0,100,0));<br>
 +
''-- Now the object spins about its own Y axis''}}
 +
 +
{{ScriptFunction|void|AddTorque| (SVector torque)|Adds a torque to the rigidbody relative to the global coordinate system. |5=
 +
local obj = Space.Host.ExecutingObject;<br><br>
 +
obj.Rigidbody.AddTorque (Vector.New(0,100,0));<br>
 +
''-- Now the object spins about the global Y axis''}}
 +
 +
{{ScriptFunction|SVector|ClosestPointOnBounds| (SVector input)|Returns the closest point on the bounding box of the attached colliders. |5=
 +
local obj = Space.Host.ExecutingObject;<br><br>
 +
Space.Log(obj.Rigidbody.ClosestPointOnBounds (Vector.Zero));<br>
 +
''-- prints [x,y,z] to the console, where x,y,z are coordinates of the rigidbody's point that is the closest to the global origin''}}
 +
 +
{{ScriptFunction|void|MovePosition| (SVector point)|Moves the rigidbody to position. |5=
 +
local obj = Space.Host.ExecutingObject;<br>
 +
local moveHere = Vector.New(obj.WorldPosition.x+10,obj.WorldPosition.y,obj.WorldPosition.z);<br><br>
 +
obj.Rigidbody.MovePosition (moveHere);<br>
 +
''-- The object has been moved by 10 units in the positive X direction''}}
 +
 +
{{ScriptFunction|void|MoveRotation| (SQuaternion rotation)|Rotates the rigidbody to rotation. |5=
 +
local obj = Space.Host.ExecutingObject;<br>
 +
local setRotationTo = Quaternion.Euler(60,0,0)<br><br>
 +
obj.Rigidbody.MoveRotation (setRotationTo);<br>
 +
''-- The object's rotation has been set to 60 degrees in the positive X direction and 0 in Y and Z''}}
 +
 +
{{ScriptFunction|void|ResetCenterOfMass| ()|Reset the center of mass of the rigidbody. |5=
 +
local obj = Space.Host.ExecutingObject;<br><br>
 +
obj.Rigidbody.CenterOfMass = Vector.New(1,1,1);<br>
 +
Space.Log(obj.Rigidbody.CenterOfMass);<br>
 +
''-- prints "[1,1,1]" to the console''<br><br>
 +
obj.Rigidbody.ResetCenterOfMass();<br>
 +
Space.Log(obj.Rigidbody.CenterOfMass);<br>
 +
''-- prints "[0,0,0]" to the console''}}
 +
 +
{{ScriptFunction|void|Sleep| ()|Forces a rigidbody to sleep. |5=
 +
local obj = Space.Host.ExecutingObject;<br><br>
 +
obj.Rigidbody.Sleep();<br>
 +
Space.Log(obj.Rigidbody.Sleeping);<br>
 +
''-- prints "True" to the console''<br><br>
 +
obj.Rigidbody.WakeUp();<br>
 +
Space.Log(obj.Rigidbody.Sleeping);<br>
 +
''-- prints "False" to the console''}}
 +
 +
{{ScriptFunction|void|WakeUp| ()|Forces a rigidbody to wake up. |5=
 +
local obj = Space.Host.ExecutingObject;<br><br>
 +
obj.Rigidbody.Sleep();<br>
 +
Space.Log(obj.Rigidbody.Sleeping);<br>
 +
''-- prints "True" to the console''<br><br>
 +
obj.Rigidbody.WakeUp();<br>
 +
Space.Log(obj.Rigidbody.Sleeping);<br>
 +
''-- prints "False" to the console''}}
 +
  
 
==Properties==
 
==Properties==
  
 
{{ScriptFunction|float|AngularDrag|{ get; set; }|The angular drag of the object.|5=
 
{{ScriptFunction|float|AngularDrag|{ get; set; }|The angular drag of the object.|5=
 
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Set a new angular drag value''<br>
 
''-- Set a new angular drag value''<br>
Line 14: Line 88:
  
 
{{ScriptFunction|SVector|AngularVelocity |{ get; set; }|The angular velocity vector of the rigidbody (in radians per second).|5=
 
{{ScriptFunction|SVector|AngularVelocity |{ get; set; }|The angular velocity vector of the rigidbody (in radians per second).|5=
 
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Set a new angular velocity vector''<br>
 
''-- Set a new angular velocity vector''<br>
Line 26: Line 99:
  
 
{{ScriptFunction|SVector|CenterOfMass |{ get; set; }|The center of mass relative to the local origin.|5=
 
{{ScriptFunction|SVector|CenterOfMass |{ get; set; }|The center of mass relative to the local origin.|5=
 
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Set a new center of mass''<br>
 
''-- Set a new center of mass''<br>
Line 37: Line 109:
  
 
{{ScriptFunction|float|Drag |{ get; set; }|The drag of the object.|5=
 
{{ScriptFunction|float|Drag |{ get; set; }|The drag of the object.|5=
 
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Set a new drag value''<br>
 
''-- Set a new drag value''<br>
Line 48: Line 119:
  
 
{{ScriptFunction|bool|FreezeRotation |{ get; set; }|Controls whether physics will have any impact on the rotation of the object.|5=
 
{{ScriptFunction|bool|FreezeRotation |{ get; set; }|Controls whether physics will have any impact on the rotation of the object.|5=
 
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Set FreezeRotation to True''<br>
 
''-- Set FreezeRotation to True''<br>
Line 58: Line 128:
  
 
{{ScriptFunction|bool|Kinematic |{ get; set; }|Controls whether physics will have any impact on the object.|5=
 
{{ScriptFunction|bool|Kinematic |{ get; set; }|Controls whether physics will have any impact on the object.|5=
 
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Set Kinematic to True''<br>
 
''-- Set Kinematic to True''<br>
Line 68: Line 137:
  
 
{{ScriptFunction|float|Mass |{ get; set; }|The mass of the rigidbody.|5=
 
{{ScriptFunction|float|Mass |{ get; set; }|The mass of the rigidbody.|5=
 
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Set a new mass of the rigidbody''<br>
 
''-- Set a new mass of the rigidbody''<br>
Line 77: Line 145:
  
 
{{ScriptFunction|float|MaxAngularVelocity |{ get; set; }|The maximum angular velocity of the rigidbody (7 by default). Can be useful to prevent an object from spinning uncontrollably fast.|5=
 
{{ScriptFunction|float|MaxAngularVelocity |{ get; set; }|The maximum angular velocity of the rigidbody (7 by default). Can be useful to prevent an object from spinning uncontrollably fast.|5=
 
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Set a new value for the maximum angular velocity''<br>
 
''-- Set a new value for the maximum angular velocity''<br>
Line 96: Line 163:
  
 
{{ScriptFunction|bool|UseGravity |{ get; set; }|Controls whether gravity affects the rigidbody.|5=
 
{{ScriptFunction|bool|UseGravity |{ get; set; }|Controls whether gravity affects the rigidbody.|5=
 
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Set UseGravity to False (it is set to True by default)''<br>
 
''-- Set UseGravity to False (it is set to True by default)''<br>
Line 106: Line 172:
  
 
{{ScriptFunction|SVector|Velocity |{ get; set; }|The velocity vector of the rigidbody. (in units per second).|5=
 
{{ScriptFunction|SVector|Velocity |{ get; set; }|The velocity vector of the rigidbody. (in units per second).|5=
 
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Set a new velocity vector''<br>
 
''-- Set a new velocity vector''<br>
Line 115: Line 180:
 
''-- prints "[0, 0, 1]" to the console''}}
 
''-- prints "[0, 0, 1]" to the console''}}
  
{{ScriptFunction|SVector|WorldCenterOfMass |{ get; set; }|The center of mass of the rigidbody relative to the global origin (Read Only).|5=
+
{{ScriptFunction|SVector|WorldCenterOfMass |{ get; }|The center of mass of the rigidbody relative to the global origin (Read Only).|5=
 
+
 
local obj = Space.Host.ExecutingObject;<br><br>
 
local obj = Space.Host.ExecutingObject;<br><br>
 
''-- Get the current center of mass''<br>
 
''-- Get the current center of mass''<br>
 
Space.Log(obj.Rigidbody.WorldCenterOfMass);<br>
 
Space.Log(obj.Rigidbody.WorldCenterOfMass);<br>
''-- prints "[x, y, z]" to the console, where x,y,z are global coordinates of the center of mass. If CenterOfMass == [0,0,0], then x,y,z are equal to the global coordinates of the position of the object.''}}
+
''-- prints "[x, y, z]" to the console, where x,y,z are global coordinates of the center of mass. If CenterOfMass == [0,0,0], then x,y,z are equal to the global coordinates of the object.''}}
 +
 
 +
{{ScriptFunction|bool|Sleeping|{ get; }|Is the rigidbody sleeping?|5=
 +
local obj = Space.Host.ExecutingObject;<br><br>
 +
obj.Rigidbody.Sleep();<br>
 +
Space.Log(obj.Rigidbody.Sleeping);<br>
 +
''-- prints "True" to the console''<br><br>
 +
obj.Rigidbody.WakeUp();<br>
 +
Space.Log(obj.Rigidbody.Sleeping);<br>
 +
''-- prints "False" to the console''}}
 +
 
 +
{{ScriptFunction|float|Density|{ set; }|The density of the object (1 by default). Changing this value will affect the mass of the object (the volume will remain unchanged).|5=
 +
local obj = Space.Host.ExecutingObject;<br><br>
 +
Space.Log(obj.Rigidbody.Mass);<br>
 +
-- prints "1" to the console<br>
 +
obj.LocalScale = Vector.New(2,2,2);<br>
 +
obj.Rigidbody.Density = 0.5;<br>
 +
Space.Log(obj.Rigidbody.Mass);<br>
 +
''-- prints "4" to the console - the density is 2 times lower, and the object is 8 times bigger, therefore it's 4 times heavier.''}}
  
 
{{Scripting Navbox}}
 
{{Scripting Navbox}}

Revision as of 08:24, 25 July 2017

The Rigidbody class works with the physics of the object. Requires the Rigidbody component to function.

Members

AddExplosionForce

void AddExplosionForce (float explosionForce, SVector explosionPosition, float explosionRadius, float upwardsModifier)

Applies a force to a rigidbody that simulates explosion effects. Other rigidbodies will be affected by the explosion within its radius - the closer they are to the explosionPosition, the stronger the force will be exerted on them.

local obj = Space.Host.ExecutingObject;

obj.Rigidbody.AddExplosionForce (300, obj.WorldPosition, 10, 20)
-- The explosion will occur at the location of the ExecutingObject.

-- Place other rigidbodies around it to observe the effect!


AddForce

void AddForce (SVector force)

Adds a force to the Rigidbody that is continuously exerted on it in the given direction.

local obj = Space.Host.ExecutingObject;

obj.Rigidbody.AddForce (Vector.New(0,100,0));

-- Now the rigidbody is under a continuous force directed upwards (Y direction)


AddForceAtPosition

void AddForceAtPosition (SVector force, SVector position)

Adds a force to the Rigidbody at a given position (should be within the range of the rigidbody for a realistic result). Thus, both a torque and force are applied to the object.

local obj = Space.Host.ExecutingObject;

local applyForceHere = Vector.New(obj.WorldPosition.x-0.5,obj.WorldPosition.y-0.5,obj.WorldPosition.z-0.5)
-- This vector is equivalent to one of the lower corners of a 1x1x1 cube.

obj.Rigidbody.AddForceAtPosition (Vector.New(0,40,0), applyForceHere);

-- The object is experiencing an effect similar to being tipped upwards at the aforementioned corner.


AddRelativeTorque

void AddRelativeTorque (SVector torque)

Adds a torque to the rigidbody relative to the local coordinate system.

local obj = Space.Host.ExecutingObject;

obj.Rigidbody.AddRelativeTorque (Vector.New(0,100,0));

-- Now the object spins about its own Y axis


AddTorque

void AddTorque (SVector torque)

Adds a torque to the rigidbody relative to the global coordinate system.

local obj = Space.Host.ExecutingObject;

obj.Rigidbody.AddTorque (Vector.New(0,100,0));

-- Now the object spins about the global Y axis


ClosestPointOnBounds

SVector ClosestPointOnBounds (SVector input)

Returns the closest point on the bounding box of the attached colliders.

local obj = Space.Host.ExecutingObject;

Space.Log(obj.Rigidbody.ClosestPointOnBounds (Vector.Zero));

-- prints [x,y,z] to the console, where x,y,z are coordinates of the rigidbody's point that is the closest to the global origin


MovePosition

void MovePosition (SVector point)

Moves the rigidbody to position.

local obj = Space.Host.ExecutingObject;

local moveHere = Vector.New(obj.WorldPosition.x+10,obj.WorldPosition.y,obj.WorldPosition.z);

obj.Rigidbody.MovePosition (moveHere);

-- The object has been moved by 10 units in the positive X direction


MoveRotation

void MoveRotation (SQuaternion rotation)

Rotates the rigidbody to rotation.

local obj = Space.Host.ExecutingObject;

local setRotationTo = Quaternion.Euler(60,0,0)

obj.Rigidbody.MoveRotation (setRotationTo);

-- The object's rotation has been set to 60 degrees in the positive X direction and 0 in Y and Z


ResetCenterOfMass

void ResetCenterOfMass ()

Reset the center of mass of the rigidbody.

local obj = Space.Host.ExecutingObject;

obj.Rigidbody.CenterOfMass = Vector.New(1,1,1);
Space.Log(obj.Rigidbody.CenterOfMass);
-- prints "[1,1,1]" to the console

obj.Rigidbody.ResetCenterOfMass();
Space.Log(obj.Rigidbody.CenterOfMass);

-- prints "[0,0,0]" to the console


Sleep

void Sleep ()

Forces a rigidbody to sleep.

local obj = Space.Host.ExecutingObject;

obj.Rigidbody.Sleep();
Space.Log(obj.Rigidbody.Sleeping);
-- prints "True" to the console

obj.Rigidbody.WakeUp();
Space.Log(obj.Rigidbody.Sleeping);

-- prints "False" to the console


WakeUp

void WakeUp ()

Forces a rigidbody to wake up.

local obj = Space.Host.ExecutingObject;

obj.Rigidbody.Sleep();
Space.Log(obj.Rigidbody.Sleeping);
-- prints "True" to the console

obj.Rigidbody.WakeUp();
Space.Log(obj.Rigidbody.Sleeping);

-- prints "False" to the console



Properties

AngularDrag

float AngularDrag { get; set; }

The angular drag of the object.

local obj = Space.Host.ExecutingObject;

-- Set a new angular drag value
obj.Rigidbody.AngularDrag = 0.20;

-- Get the current angular drag value
Space.Log(obj.Rigidbody.AngularDrag);

-- prints "0.200000..." to the console



AngularVelocity

SVector AngularVelocity { get; set; }

The angular velocity vector of the rigidbody (in radians per second).

local obj = Space.Host.ExecutingObject;

-- Set a new angular velocity vector
obj.Rigidbody.AngularVelocity = Vector.New(0,Space.Math.Pi,0);
-- Now the object is rotating about the Y axis at a speed of 180 degrees per second
-- (or 30 revolutions per minute)

-- Get the current angular velocity vector
Space.Log(obj.Rigidbody.AngularVelocity);

-- prints "[0, 3.141593, 0]" to the console



CenterOfMass

SVector CenterOfMass { get; set; }

The center of mass relative to the local origin.

local obj = Space.Host.ExecutingObject;

-- Set a new center of mass
obj.Rigidbody.CenterOfMass = Vector.New(1,0,0);
-- Now the object's center of mass has been moved by 1 at the X axis

-- Get the current center of mass
Space.Log(obj.Rigidbody.CenterOfMass);

-- prints "[1, 0, 0]" to the console



Drag

float Drag { get; set; }

The drag of the object.

local obj = Space.Host.ExecutingObject;

-- Set a new drag value
obj.Rigidbody.Drag = 20;
-- Now the object's drag is set to 20 - the higher the number, the more it is resistant to gravity

-- Get the current drag value
Space.Log(obj.Rigidbody.Drag);

-- prints "20" to the console



FreezeRotation

bool FreezeRotation { get; set; }

Controls whether physics will have any impact on the rotation of the object.

local obj = Space.Host.ExecutingObject;

-- Set FreezeRotation to True
obj.Rigidbody.FreezeRotation = true;
-- Now under no circumstances the object's rotation coordinates will change.

-- Get the FreezeRotation value (find out if Freeze Rotation is in action)
Space.Log(obj.Rigidbody.FreezeRotation);

-- prints "True" to the console


Kinematic

bool Kinematic { get; set; }

Controls whether physics will have any impact on the object.

local obj = Space.Host.ExecutingObject;

-- Set Kinematic to True
obj.Rigidbody.Kinematic = true;
-- Now the object will not be affected by gravity, collisions, or other forces.

-- Get the Kinematic value (find out if Kinematic is in action)
Space.Log(obj.Rigidbody.Kinematic);

-- prints "True" to the console


Mass

float Mass { get; set; }

The mass of the rigidbody.

local obj = Space.Host.ExecutingObject;

-- Set a new mass of the rigidbody
obj.Rigidbody.Mass = 0.1;

-- Get the current value of the rigidbody's mass
Space.Log(obj.Rigidbody.Mass);

-- prints "0.1000000..." to the console


MaxAngularVelocity

float MaxAngularVelocity { get; set; }

The maximum angular velocity of the rigidbody (7 by default). Can be useful to prevent an object from spinning uncontrollably fast.

local obj = Space.Host.ExecutingObject;

-- Set a new value for the maximum angular velocity
obj.Rigidbody.MaxAngularVelocity = 1;
-- Now the object, for example, is more resistant to rolling over upon collision with another object.

-- Get the current value of the rigidbody's maximum angular velocity
Space.Log(obj.Rigidbody.MaxAngularVelocity);

-- prints "1" to the console



MaxDepenetrationVelocity

float MaxDepenetrationVelocity { get; set; }

The maximum depenetration velocity of the rigidbody (1.00000003318135E+32 by default). Can be useful to make colliding objects bounce away in a smoother fashion.

local obj = Space.Host.ExecutingObject;

-- Set a new value for the maximum depenetration velocity
obj.Rigidbody.MaxDepenetrationVelocity = 1;

-- Get the current value of the rigidbody's maximum depenetration velocity
Space.Log(obj.Rigidbody.MaxDepenetrationVelocity);

-- prints "1" to the console


UseGravity

bool UseGravity { get; set; }

Controls whether gravity affects the rigidbody.

local obj = Space.Host.ExecutingObject;

-- Set UseGravity to False (it is set to True by default)
obj.Rigidbody.UseGravity = false;
-- Now gravity does not affect the rigidbody.

-- Get the UseGravity value (find out if UseGravity is in action)
Space.Log(obj.Rigidbody.UseGravity);

-- prints "False" to the console


Velocity

SVector Velocity { get; set; }

The velocity vector of the rigidbody. (in units per second).

local obj = Space.Host.ExecutingObject;

-- Set a new velocity vector
obj.Rigidbody.Velocity = Vector.New(0, 0, 1);
-- Now the object is moving in the positive Z direction at a speed of 1 unit per second

-- Get the current velocity vector
Space.Log(obj.Rigidbody.Velocity);

-- prints "[0, 0, 1]" to the console


WorldCenterOfMass

SVector WorldCenterOfMass { get; }

The center of mass of the rigidbody relative to the global origin (Read Only).

local obj = Space.Host.ExecutingObject;

-- Get the current center of mass
Space.Log(obj.Rigidbody.WorldCenterOfMass);

-- prints "[x, y, z]" to the console, where x,y,z are global coordinates of the center of mass. If CenterOfMass == [0,0,0], then x,y,z are equal to the global coordinates of the object.


Sleeping

bool Sleeping { get; }

Is the rigidbody sleeping?

local obj = Space.Host.ExecutingObject;

obj.Rigidbody.Sleep();
Space.Log(obj.Rigidbody.Sleeping);
-- prints "True" to the console

obj.Rigidbody.WakeUp();
Space.Log(obj.Rigidbody.Sleeping);

-- prints "False" to the console


Density

float Density { set; }

The density of the object (1 by default). Changing this value will affect the mass of the object (the volume will remain unchanged).

local obj = Space.Host.ExecutingObject;

Space.Log(obj.Rigidbody.Mass);
-- prints "1" to the console
obj.LocalScale = Vector.New(2,2,2);
obj.Rigidbody.Density = 0.5;
Space.Log(obj.Rigidbody.Mass);

-- prints "4" to the console - the density is 2 times lower, and the object is 8 times bigger, therefore it's 4 times heavier.