https://www.sirikata.com/wiki/index.php?title=Guides/Scripting_Guide/Tutorials/Move_Presence&feed=atom&action=historyGuides/Scripting Guide/Tutorials/Move Presence - Revision history2024-03-28T13:51:19ZRevision history for this page on the wikiMediaWiki 1.35.7https://www.sirikata.com/wiki/index.php?title=Guides/Scripting_Guide/Tutorials/Move_Presence&diff=1267&oldid=prevEwencp: Initial import from sirikata-docs2012-05-25T22:27:24Z<p>Initial import from sirikata-docs</p>
<p><b>New page</b></p><div>= Moving in the World =<br />
<br />
It would be a pretty boring world if all presences just stayed in one place. This tutorial explains how a scripter can move presences throughout the world.<br />
<br />
There are two basic types of movement in the system: translational and rotational. In translational movement, every point of the presence's mesh moves the same distance; in rotational movement, a presence's mesh spins around a fixed axis. A useful way of understanding the difference between translational and rotational movement is to think about a wheeled office chair. A person wanting to translate his/her office chair would roll it from one point in a room to another. A person wanting to rotate his/her office chair would spin in place on the office chair.<br />
<br />
Translational and rotational movement can be combined (ie, one can use Emerson to translate and rotate a presence simultaneously). For the simplicity of this tutorial, we instead focus on each separately.<br />
<br />
Before we begin, a quick note on code in the following examples: the Emerson run-time keeps track of all of an entity's connected presences in an array available to the scripter as <tt>system.presences</tt>. For the example code used in this tutorial, we'll assume that the presence being moved is named by <tt>system.self</tt>.<br />
<br />
== Translational motion ==<br />
Emerson allows a scripter to either instantly teleport a presence to a new position or to assign a velocity to the presence. A presence with an assigned velocity automatically moves through the world in the assigned direction and rate. For both position and velocity, Emerson uses standard Cartesian coordinates, and represents each using 3-dimensional vectors. We'll talk about position and velocity separately below, but it may be useful for the reader to refer to the Vec3 type described here&lt;lkjs&gt; before beginning.<br />
<br />
=== Teleport/SetPosition ===<br />
Assume that you have a presence that you want to instantly translate from its current position to a position 100m away in the x direction.<br />
<br />
We'll break such a script into 3 pieces:<br />
<br />
<blockquote>* Getting the presence's current x, y, and z coordinates in the space.<br />
* Specifying a new 3-dimensional position based on the presence's current x, y, and z coordinates in the space.<br />
* Requesting the system to move your object to this new position.<br />
</blockquote><br />
Here's the entire script to perform the above.<br />
<br />
var pos = system.self.getPosition();<br />
pos.x = pos.x + 100;<br />
system.self.setPosition(pos);<br />
<br />
The above is explained more methodically below.<br />
<br />
==== Getting the presence's current coordinates in the space ====<br />
<br />
To get a 3-dimensional vector representing the current position of a presence in space, simply use the <tt>getPosition()</tt> function. As an example, open a scripting window and enter:<br />
<br />
var pos = system.self.getPosition(); system.print(pos);<br />
<br />
You should see the position of a presence printed in the following form: &lt;x,y,z&gt;. You can access the individual x, y, and z fields of this position with the accessors `.x`, `.y`, and `.z`, respectively. That is, if you wanted to just print the x position of a presence, you would replace the second line of the above function as shown below:<br />
<br />
var pos = system.self.getPosition(); system.print(pos.x);<br />
<br />
====Specifying a new 3-dimensional position based on <tt>pos</tt>====<br />
After the previous section, the variable <tt>pos</tt> stores the current position associated with <tt>system.self</tt>. We need to specify a new target position. Doing so is simple:<br />
<br />
pos.x = pos.x + 100;<br />
<br />
It is important to note, the above line does ''not'' move the presence by itself. It instead changes the value stored in <tt>pos</tt>. <tt>pos</tt> now describes the position that we want to move our presence to.<br />
<br />
====Requesting the system to move your object to <tt>pos</tt>====<br />
To actually set the position of <tt>system.self</tt> to <tt>pos</tt>, one simply uses the <tt>setPosition</tt> function:<br />
<br />
system.self.setPosition(pos);<br />
<br />
The <tt>setPosition</tt> function takes in a single argument, a 3-dimensional vector, and instantly moves the associated presence to that point in the space.<br />
<br />
=== SetVelocity ===<br />
<br />
The above example instantly moved a presence from one point to another. While such an operation is possible in a virtual world, it certainly does not agree with (most of) the real world: one wouldn't see a train teleport from one station to another -- instead, the train acquires a velocity and moves from point to point non-instantaneously.<br />
<br />
Emerson provides similar capabilities for presences by allowing a scripter to set a presence's velocity (both magnitude and direction). (Note: in Emerson, a scripter ''cannot'' set higher order movement properties for presences such as acceleration or jerk.)<br />
<br />
Consider the simple task of moving a stationary presence 30m in the y direction. To do so, a scripter would use the <tt>setVelocity</tt> command. Like the <tt>setPosition</tt> command described in the previous section, <tt>setVelocity</tt> takes in a single argument: a 3-dimensional vector. The x, y, and z components of this argument 3-dimensional vector specifies the velocity that the presence should move in meters per second (in the x, y, and z directions respectively). You can experiment with the <tt>setPosition</tt> function pretty easily. For instance, at a scripting prompty, enter the following code:<br />
<br />
var zeroVelocity = new util.Vec3(0,0,0);<br />
var posYVelocity = new util.Vec3(0,1,0);<br />
var negYVelocity = new util.Vec3(0,-1,0);<br />
<br />
Now, to get a presence to stop moving, just enter:<br />
<br />
system.self.setVelocity(zeroVelocity);<br />
<br />
To get a presence to start moving in the positive y direction, enter:<br />
<br />
system.self.setVelocity(posYVelocity);<br />
<br />
And, to get a presence to start moving in the negative y direction, enter:<br />
<br />
system.self.setVelocity(negYVelocity);<br />
<br />
To complete the simple task of moving a stationary presence 30m in the y direction, enter into the command prompt:<br />
<br />
system.self.setVelocity(posYVelocity);<br />
<br />
and then 30 seconds later:<br />
<br />
system.self.setVelocity(zeroVelocity);<br />
<br />
The first command should start the presence moving in the y direction at 1 m/s. The second command should stop the presence. Manually entering such commands obviously leaves something to be desired. For a more robust solution, read about <tt>timeout</tt>s in the tutorial.<br />
<br />
== Rotational motion ==<br />
Ugh. I hate quaternions.</div>Ewencp