Using Vectors

An explanation of how vectors are used in Project Spark

There are two sorts of vectors in Project Spark – 2D and 3D. 2D vectors, using x and y co-ordinates are used for displaying on screen, while 3D vectors , which adds a z co-ordinate are used to describe a place in your world. You can specify them in kode in 2 different ways – using x, y ,z or east, world up, north.

On screen

The screen is 1 x 1 with 0 being the centre point. x or east controls the horizontal coordinate and y or world up controls the vertical coordinate.

Imagine the screen divided up into a grid like this:

grid-diagram

To place an image on the screen you work out the coordinates on the grid

Then you create a vector variable that contains these numbers.

grid-diagram2

Long form

WHEN DO [vectorvar: star][x][=][0.8]
WHEN DO [vector var:star][y][=][0.5]

Short form

WHEN DO [vectorvar:star][=][east][multiplied by][0.8][+][world up][multiplied by][0.5]

You then display the icon at the vector position

WHEN DO [display][icon:star][at screen position][vectorvar:star]

In World

In world you need 3 coordinates. To find out a specific place in your world, put down an object and add this to its brain

WHEN DO [display][position][above]

When you run test you will see 3 numbers displayed above  such as:

1.000, 10.000, 3.000

The first is the x coordinate

The second is the y coordinate

The third is the z coordinate

(Note: In a default world the ground appears at 10 metres of the y coordinate)

The world is laid out in a metre grid. So 0,10,0 is the centre of your world (at ground level), and 1,12,3 is 1 metre east, 2 metres up, and 3 metres north. Use negative numbers for west, down and south.

Long form

WHEN DO [vectorvar: star][x][=][1]
WHEN DO [vector var:star][y][=][12]
WHEN DO [vector var:star][z][=][3]

Short form

WHEN DO [vectorvar:star][=][east][multiplied by [1][+][world up][multiplied by][12][+][north][x][3]

You can then position objects at this coordinate

WHEN [once] DO [position][=][vectorvar:star]

or

WHEN [once] DO [create][acorn][at position][vectorvar:star]

Using vectors as offsets

Instead of specifying a fixed position in the world from the world grid, an offset vector specifies a position from the original socket or position. So for example, If you were wanting to offset from the head socket, the head vector would be 0,0,0. If you wanted the offset to be 1 metre above the head socket you would define the y coordinate, and so on.

WHEN DO [vectorvar:acorn][y][=][1]
WHEN [once] DO [attach][acorn][to socket][head][offset][vectorvar:acorn]

Using vectors to move objects

You can increment and decrement the number in a vector to move objects.

This for example will make an object raise up (best used with fixed physics)

WHEN [once] DO [numvar:raise][=][10]
WHEN DO [numvar:raise][increment by][0.01]
WHEN DO [vector:acorn][y][=][numvar:raise]
WHEN DO [position][=][vector:acorn]

you can also clamp the number so that it raises to a specific height.

WHEN DO [numvar:raise][=][clamp][max][14][numvar:raise]

Comments are closed.

Powered by WordPress.com.

Up ↑

%d bloggers like this: