I am playing around in the game engine and I am moving this cube around with this code:

import bge
from mathutils import Matrix, Euler
from math import radians
obj = bge.logic.getCurrentScene().objects["cube"]
property = bge.logic.getCurrentScene().objects["Empty"].get("movement")
move = 0.0
if property == 1: move = [1, 0.1]
elif property == 2: move = [1, -0.1]
elif property == 3: move = (obj.worldOrientation).to_euler(); move.rotate_axis("Z", radians(2.0))
elif property == 4: move = (obj.worldOrientation).to_euler(); move.rotate_axis("Z", radians(-2.0))
if move != 0.0 and property in (1, 2):
    obj.localPosition[move[0]] += move[1]
elif move != 0.0:
    obj.worldOrientation = move

Where the property is just a int representing which way to go or rotate. The movement is applied to the localPosition so I would think that this would respect the rotation, but it doesn't and this is the result: enter image description here

So how do I get it to always move in the local +- Y direction?


localPosition is actually the position relative to the objects parent (for the most part, see below). If the cube has no parent, it will be the same as worldPosition.

To move forward along the local Y axis:

obj.applyMovement((0, 0.1, 0), True)

Irrelevant localPosition quirk

localPosition isn't truly relative to the parent. It's more like... relative to the parent modified by its world position when initially parented (or something like that). So if your parent isn't at the origin when hiting control+P, the result might be kind of weird. Intended behavior? No idea.

| improve this answer | |
  • $\begingroup$ The localPosition quirk can be resolved by pressing ctrl+A and selecting "All Transforms to Deltas" $\endgroup$ – sdfgeoff May 28 '17 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.