I’m sure there is a easy way to do this, I am just not familiar enough with python yet to understand how. I am trying to move an object using the setPosition function but I want the object to be offset from the object it is getting the position from. Can someone please tell me how to offset the coordinates in any or all of the 3 axis.
Thank You
first of all, be careful if you’re moving dynamic objects with setPosition, it’s not compatible with the physics engine.
you can just do something like this (untested, could have typos or stupid mistakes)
o=GameLogic.getCurrentController().getOwner()
x,y,z=[1,1,1]#replace 1 with how far you want it to move in each axis. 0 is no movement, surprisingly enough XD
pos=o.getPosition()
o.setPosition([pos[0]+x,pos[1]+y,pos[2]+z])
you could use the motion actuator, but I always avoid using a logic brick if I can do it in python.
obj.getPosition() will return to you a list of the three positional co-ordinates, in X, Y, Z format.
# Getting the position
pos = obj.getPosition()
# Splitting the position into it's elements
X = pos[0]
Y = pos[1]
Z = pos[2]
Above demonstrates how you can split the list into it’s elements.
Now, you can modify and apply those elements like this:
# Defining how much you want to offset the X, Y, and Z.
offsetX = 1.0
offsetY = 0.0
offsetZ = 0.0
# Adding the offset values to the original values
newX = X + offsetX
newY = Y + offsetY
newZ = Z + offsetZ
# Putting it in list format
newPos = [newX, newY, newZ]
# Setting the object's position
obj.setPosition(newPos)
Of course, this is just like Captain Oblivion’s script, but all stretched out to help you understand.
I actually figured it out after several hours of messing around with it, but I appreciate you taking the time to break it out like you did. I can now go back and clean up my code a little bit.
Thanks