Create, Compete & Win at Redbrick Connect 2024! 🎉
Text codingGet StartedAbsolute & Relative Coordinates

Absolute and Relative Coordinates

This guide explains the concepts and usage of absolute and relative coordinates.

⚠️

For PLAYER objects or objects with the Physics Body property enabled, you need to run the code Object.body.needUpdate = true; after using .position.set() or updating the .position property to apply the position change.

Moving with Absolute Coordinates

Absolute coordinates represent a fixed position relative to a specific reference point (the world).
These coordinates point to a specific location in the global coordinate system, independent of other objects’ positions.

Example: Moving to the position (100, 50, 200) in the world coordinate system.

In Redbrick Studio, you can adjust the position of an object based on absolute coordinates as follows:

Object.position.set()

Using Object.position.set(x, y, z), you can instantly move an object to a specific coordinate within the world.

position.set()
const obj = WORLD.getObject("obj");
const obj2 = WORLD.getObject("obj2");
 
obj.position.set(10, 10, 10); //Move object to coordinates x=10, y=10, z=10
obj.position.set(PLAYER.position.x, PLAYER.position.y, PLAYER.position.z); //Move object to the player's position
PLAYER.position.set(obj2.position.x, obj2.position.y, obj2.position.z) //Move player to obj2's position
 
//For PLAYER objects or objects with the Physics Body property enabled, run the code below after changing position to apply the change
PLAYER.body.needUpdate = true; 
obj.body.needUpdate = true;

Object.position.copy()

Besides using position.set(), you can use Object.position.copy(vector) to immediately move an object to a specific position in the world.

position.copy()
const obj = WORLD.getObject("obj");
const obj2 = WORLD.getObject("obj2");
 
obj.position.copy(obj2.position); // Move obj to obj2's position
obj2.position.copy(PLAYER.position); // Move obj2 to the player's position
PLAYER.position.copy(obj.position); // Move the player to obj's position
 
// For PLAYER objects or objects with the Physics Body property enabled,
// execute the following code after changing the position to apply the update
PLAYER.body.needUpdate = true; 
obj.body.needUpdate = true;

Directly Updating position Values

Instead of using the .set() method, you can directly update the values of the .position property.

position_value_update
const obj = WORLD.getObject("obj");
const obj2 = WORLD.getObject("obj2");
 
obj.position.x = 10; //Move the object to x=10 while keeping the current y, z coordinates
obj2.position.z = PLAYER.position.z; //Set the object's z coordinate to the player's z coordinate
PLAYER.position.y = 100; //Change the player's height (y) to 100
 
//For PLAYER objects or objects with the Physics Body property enabled, run the code below after changing position to apply the change
PLAYER.body.needUpdate = true;
obj.body.needUpdate = true;

PLAYER.spawn()

If the object you want to move is the player avatar, you can use the .spawn(target) method.
If you do not provide a target parameter, it will move to the location of the starting_point asset (if it exists in the scene).

.spawn()
const obj = WORLD.getObject("object");
 
PLAYER.spawn(); //Move to starting_point if no parameter is provided
PLAYER.spawn(obj); //Move to obj's location
PLAYER.spawn("object"); //Move to obj's location (specified by title)

Moving with Relative Coordinates

Relative coordinates represent a position relative to the current location or a specific reference point.
They change based on the reference point’s location and are expressed in terms of direction and distance.

Example:
Moving 10 units along the x-axis and 5 units along the y-axis from the current position.
Moving a specific distance relative to another object.

In Redbrick Studio, you can adjust the position of an object based on relative coordinates as follows:

Object.position.set()

You can implement relative movement using the Object.position.set(x, y, z) method as follows:

position.set()
const obj = WORLD.getObject("obj");
const obj2 = WORLD.getObject("obj2");
 
obj.position.set(obj2.position.x + 10, obj2.position.y, obj2.position.z); //Move obj by x+10 relative to obj2's position
PLAYER.position.set(obj2.position.x, obj2.position.y + 10, obj2.position.z) //Move player by y+10 relative to obj2's position
 
//For PLAYER objects or objects with the Physics Body property enabled, run the code below after changing position to apply the change
PLAYER.body.needUpdate = true;
obj.body.needUpdate = true;

Directly Updating position Values

You can implement relative movement by directly updating the values of the .position property.

position_value_update
const obj = WORLD.getObject("obj");
const obj2 = WORLD.getObject("obj2");
 
obj.position.x += 10; //Move the object by x+10 from the current position (keeping y, z coordinates the same)
obj2.position.z = PLAYER.position.z - 10; //Move the object to z-10 from the player's position (keeping x, y coordinates the same)
PLAYER.position.y += 10; //Increase the player's height (y) by 10 from the current height
 
//For PLAYER objects or objects with the Physics Body property enabled, run the code below after changing position to apply the change
PLAYER.body.needUpdate = true;
obj.body.needUpdate = true;

Object.go()

Using the Object.go() method, you can move an object by a certain distance from the current position.

.go()
const obj = WORLD.getObject("obj");
 
obj.go(0, 5, 0); //Move the object up by 5 units from the current height (y)
PLAYER.go(5, 5, 5); //Move the player by 5 units along x, y, z from the current position

Object.move()

Using the Object.move() method, you can make an object move over a specified time based on the current position.

.go()
const obj = WORLD.getObject("obj");
 
obj.move(10, 0, 10, 5); //Move the object by x+10, z+10 from the current position over 5 seconds
PLAYER.go(0, 5, 0, 10); //Move the player up by 5 units (y) from the current position over 10 seconds