Koding Konversation: Using Vectors

This is a complex area of Project Spark koding but probably vital if you want to do anything regarding object positioning, screen display or object movement. The following information was posted to the Project Spark Forum. I have amended it to include the standard kode shorthand used in this website, and edited the conversation to pull out the relevant sections and correct spelling for ease of reading.

Original Post

GDNA PIETER:
So a lot of pppl have bin asking me about vectors….
these can be used to do almost an infinite number of things so here is a small guide on how to use them.
First off all u will want to set up a variable vector. this is like a variable number but then with vectors. If you don’t know what vectors are they are like coordinates giving you a specific position on the map. So once you have created a variable vector  you will want to give it a value

here is the code to do so

WHEN DO [vectorvar:VV1][x] = 0  //(right left)
WHEN DO [vectorvar:VV1][y] = 0  //(up down)
WHEN DO [vectorvar:VV1][z] =  0  //(forward backwards) (I could have something switched here)

so we now have a variable vector at a set location of 0,0,0

These can also be used as a true variable increasing and decreasing over time or at specific time’s by using this code WHEN DO [vectorvar:VV1] [y]= [a variable number]
the variable number can be increased, decreased and wil therefore alter the loction of the vector location
So now that u have a variable vector you will want to start using it 🙂

You can use this for any number of things that would need a specific location..
(by example: teleportation, creating terrain, hud, moving objects, attached to character object,….)

Make sure you always take in account where the starting point of these vectors are.. this could be any number of things again
(your character, the screen, the world, a random object,….)

So I’m gonna show you a small example of a screen display with this

WHEN DO [display] [object(could be anything)][at screen location,][vectorvar:VV1]

so this code wil now display the object you selected at screen location 0.0.0. (center screen)
if you want this to be more to the right you wil want to increase the X axis (by example: VV1 X = 0.4)same thing goes for any other axes.
You will now see that the object is displayed to the right in the screen..
You can place you object on any location on the screen this way.

So just for the fun of it here is another full example on how to use

WHEN [RT] DO [subtract terrain][at location][with offset][vectorvar: VV2]
WHEN DO [vectorvar:VV2][x] = 1
WHEN DO [vectorvar:VV2][y] = 0
WHEN DO [vectorvar:VV2][z] =  0

this a a working code to subtract terrain from any location you want. (yes you wont fall through the map anymore like this if you do I right)
by using the vectors you can set it up so that the terrain removal ball is in front of your character by example and then is able to tunnel through a mountain
(my example will not work because I was guessing the VV2 value’s, have a try at it and locate it correctly and it will work)

Mestrodas:
Nice initiative. Also note that the vector axis values vary from -1 to 1. Remember your trigonometry classes where 0,0,0 is the origin (the exact middle of the screen). X is the left/right indicator, Y the up/down and Z is depth. I’ll give some examples of x and y axis values (useful to create HUDs):

x,y (1,0) – Right Center Screen
x,y (-1,0) – Left Center Screen
x,y (0,1) – Top Center Screen
x,y (0,-1) – Bottom Center Screen

Mindcraft Max:

The [object space] tile is used to get the coordinate of a point/vector in the coordinate system of your object, this is to say that the origin of the system will be your object’s [position], the x axis will be [right], the y axis [up], and the z axis [forward].
If you want to calculate the vector in the object space, simply write [vector] [to object space]. If you need to get the object space coordinates of a point however, calculate the offset vector from your position to the point. Then, you can use [to object space] on the vector to get the coordinates of the point in the coordinate system of the character.
Use [from object space] again to get the offset with the world coordinates.

Be careful, trying to use the [to object space] tile directly on the position of the object does not work. You can only apply [to object space] and [to camera space] to a vector.
Applying [from object space] to a position “pos” (relative to your character’s coordinate system) works well, since it is equivalent to the vector [pos] [minus] [zero] where zero is the origin in the character’s coordinate system.

Some tips:
– Make sure like I said in my vectors tutorial to know when you’re using a point, and when you’re using a vector. Some functions have modifiers like “toward” or “in direction”. “Toward” needs a point, while “in direction” needs a vector.
– You can use
WHEN DO [display] [UI element] [on screen at] [mouse position]
WHEN DO [display] [mouse position] [screen centre]
to know where to place your UI elements.
– Some inputs are vectors: left stick, right stick, WASD, arrow keys, D-pad. And you also have mouse position, and its modifiers “object”, “terrain” or “world”.
If you need the left stick vector, store it in a vector variable “left stick”, and use that variable, as the [left stick] tile is unstable and sometimes gives a vector with x and z coordinates, and sometimes with x and y coordinates.
If you need the vector with x and z coordinates, use this Kode:
[left stick (vector variable)] [equals] [left stick]
And if you prefer x and y coordinates:
[left stick (vector variable)] [equals] [vector rotate] [left stick]
since for some reasons, [left stick] after [vector rotate] (but also after [display], and some other tiles) changes to x and y coordinates.
– If you need some randomness, there’s a [random vector] tile. It has lots of modifiers, so make sure to check them out.
– Object relative vectors like “forward”, “up”, etc., the camera vector “camera forward”, and world relative vectors like “east”, “world up”, etc. are normalised vectors (they have a length of 1). For example, setting forward to 2*forward won’t do anything.

Comments are closed.

Powered by WordPress.com.

Up ↑

%d bloggers like this: