hey guys,
so recently i was asked if it was possible to get the XYZ distances between two objects in realtime, and this is what I came up with.
so here is a quick little(possibly useful) script that just simply gets the X,Y, and Z distances between the owner object, and a user-defined object, its pretty straight forward. and here it is:
##################################################################################
# #
# XYZ distance script by QAZWSX2541(kendrick2541) #
# #
# Two optional properties can be added to the object #
# that this script is attached to including: #
# #
# "print" - this will print the distances to the console #
# "absoulute" - true will return absoulute distances (always positive numbers) #
# #
##################################################################################
import GameLogic
cont = GameLogic.getCurrentController()
own = cont.owner
### Enter the name of the other object here
obj = "Cube.001"
### Defining the function
def xyzDist(obj):
### Importing objects and properties
obj = "OB" + obj
scene = GameLogic.getCurrentScene()
objList = scene.objects
obj = objList[obj]
dist = own.getDistanceTo(obj)
debug = False
absoulute = True
if own.has_key("print"):
debug = own['print']
if own.has_key("absoulute"):
absoulute = own['absoulute']
### Geting the world position of eachobject
ownpos0 = own.worldPosition[0]
ownpos1 = own.worldPosition[1]
ownpos2 = own.worldPosition[2]
objpos0 = obj.worldPosition[0]
objpos1 = obj.worldPosition[1]
objpos2 = obj.worldPosition[2]
### Doing a bit of math to find the XYZ distances
xdist = objpos0 - ownpos0
ydist = objpos1 - ownpos1
zdist = objpos2 - ownpos2
### If you add the propery "absoulute" to the owner object
### and turn it to false it will return the "local" distaces.
### otherwise it will return the absoulute values
if absoulute == True:
xyzDist = [abs(xdist),abs(ydist),abs(zdist)]
else:
xyzDist = [xdist,ydist,zdist]
### If you add the propery "print" to the owner object
### and turn it to true, it will print the XYZ distances
if debug == True:
print(xyzDist)
return xyzDist
xyzDist(obj)
And here is the .blend test file:
dist test.blend (136 KB)
so far its only working in 2.49b though