I was wondering if it’s possible to move an object in game engine according to it’s local coordinates.
For example, I would use an actuator to move the object 10 unit in it’s local x-axis, then rotate it 37 degrees around it’s local z-axis and then move another 10 units in it’s local x-axis.
Sure it can be done using global coordinates and some trigonometry, but using local coordinates would be much easier. Anyone knows how I could do it?
Mark the L next to your Motion Values – most Times is is already marked, so your Movement already is local initially.
The ‘L’ C.A. mentions is in the Motion actuator. This can also be done in Python, as there is an equivalent argument for movement functions that allows you to move objects on their local axes.
thanks, people. got that working right.
now there’s another issue: the localPosition variable returns values relative to (it seems) the global axis.
for example, I rotated the object 45 degrees around it’s local z-axis, and moved it 10 units in it’s local x-axis, but the variable does not return 10 as the x value, it returns the global x-axis value. I can get around it with some trigonometry again, but it makes much harder to control.
is there a way to actually get the local position value, or this is just the way blender is?
You mean via Python?
obj.localPosition <==> obj.worldPosition
Although I never print the localPosition eager to tell it from the worldPosition, so maybe it does indeed preset the wrong Value and I just didn’t get it…
- The localPosition value is used for object’s positions relative to their parents’ positions, if they have any. If they don’t, then it just acts as worldPosition, apparently.
EDIT: So the object’s localPosition value will only return global values. If you parent it to another object and rotate that object, then the object will return local values. You could also store the values yourself. If you know that you’re moving it 1 BU on the local x-axis, then just add one to an object variable (which you would need to initialize before adding one to it, so that it’s equal to a value before you add one).
- If you want to move objects on a local axis, try using the object.applyMovement() function - the first argument is a list consisting of the values to move on, and the second is whether you want to move it locally or not. It would look like this:
obj.applyMovement([0, 1, 0], 1) # Move the object forward on the local Y-axis
What was the localPosition before you started?
Why should the local position be 10?
The object did a motion of 10 units relative to it’s previous position/orientation. Now it has a new position. The relative position to the objects origin is (0,0,0). Because the objects origin moves with with the object.
There is a difference between motion and position:
local motion = relative to the current position/orientation
global motion = relative to the scene’s origin
local Position/Orientation = relative to the parent’s position/orientation (no parent = scene is parent)
world Position/Orientation = relative to scene’s origin
For the BGE there is no reason to keep track of old positions/orientations. The Physics engine might do that, but it would keep it internal.
I hope it helps for understanding