2 - Transforming Object

Intro

Using python you can do more advanced stuff. We'll looking at the game engine's transforming functions.

Requirements

Range Engine 1.2+ or UPBGE 2.5b+

Diving into code

Local

localPosition, localOrientation and localScale

World

worldPosition, worldOrientation and worldScale

What were those above

If you're wondering what were those localPosition, worldOrientation, etc, they are noting but functions used to transform the objects using python. With this you can have control of objects and move, rotate and scale them in global and local axis.

What the heck is local and world

#explanation goes here for world

The default cube being moved in world axis

exp for local

local
The default cube being moved in local axis

Moving objects with python

In world axis

I hope you understood what local and world axis mean, now let's jump to python code. Open up the Text Editor (which is opened be default in Range and UPBGE). If it's not there, change the editor type to Text Editor. We'll be now changing the position of our default cube in world axis. let's import the Range module and get our python controller and owner like so.

import Range
own = Range.logic.getCurrentController()

And then we bring the function worldPosition, and set the values to [3,5,9] something like this: worldPosition = [3,5,9]. So the full code looks like this

import Range
own = Range.logic.getCurrentController()
own.worldPosition = [3,5,9]

Remeber, you gotta put own before almost any functions so the game engine know which object to move, or it won't move any objects.

Connecting with logic bricks

Bring a keyboard sensor and a Python controller. Select the script where we did all those moving the cube thing.

Connecting the bricks and selecting the script

Connect the bricks and run the game. Press the key which you assigned for the keyboard sensor and then the cube moves!

Changing cube's position with python
in local axis

Now, let's try moving the cube in local axis. Rotate the cube in any degrees and replace the worldPosition with localPosition.

You may wonder "wait why isnt my local position changing?" The problem with that is... your object's rotation needs to be relative to the parent object. Like if your parent object's orientation is [23,45,90] and if you use localPosition on your child object whose rotation is [23,45,90] too, the changed location will be in local axis.

How "localPosition" works

The bricks was setup like this: Always sensor <---------> Python controller.

Alternative setup for localPosition

You can also use applyMovement with an always sensor having true level triggering turned off. So the code looks like this.

import Range
own = Range.logic.getCurrentController()
own.applyMovement([3,5,9], True)

We're applying movement to a certain vector in local axis. You can use 0,1 instead of True, False. 0 = False, and 1 = True. Btw here is the project file.