[Bf-committers] GameObject Python Distance function

charlie c snailrose at bresnan.net
Mon Jan 10 22:53:07 CET 2005

```Hey all, here is a small function. One that i think will be helpful
as opposed to having to write a def() function every time you want to find
the distance between
two objects:

import GameLogic

objs = GameLogic.getCurrentScene().getObjectList()

Obj1 = objs['OBPosition1']
Obj2 = objs['OBPosition2']

print Obj1.getDistance(Obj2.getPosition())

Index: source/gameengine/Ketsji/KX_GameObject.cpp
===================================================================
RCS file:
/cvsroot/bf-blender/blender/source/gameengine/Ketsji/KX_GameObject.cpp,v
retrieving revision 1.11
diff -u -r1.11 KX_GameObject.cpp
--- source/gameengine/Ketsji/KX_GameObject.cpp 6 Nov 2004 04:58:09 -0000
1.11
+++ source/gameengine/Ketsji/KX_GameObject.cpp 10 Jan 2005 21:33:53 -0000
@@ -621,6 +621,7 @@
{"getParent", (PyCFunction)KX_GameObject::sPyGetParent,METH_VARARGS},
{"getMesh", (PyCFunction)KX_GameObject::sPyGetMesh,METH_VARARGS},
{"getPhysicsId",
(PyCFunction)KX_GameObject::sPyGetPhysicsId,METH_VARARGS},
+ {"getDistance", (PyCFunction)KX_GameObject::sPyGetDistance,METH_VARARGS},

{NULL,NULL} //Sentinel
@@ -1100,6 +1101,21 @@
return PyInt_FromLong(physid);
}

+
+PyObject* KX_GameObject::PyGetDistance(PyObject* self,
+              PyObject* args,
+              PyObject* kwds)
+{
+ MT_Point3 a,b;
+ if(PyVecArgTo(args, b)){
+  a = NodeGetWorldPosition();
+  float nx = a[0] - b[0];
+  float ny = a[1] - b[1];
+  float nz = a[2] - b[2];
+  return PyFloat_FromDouble(sqrt(nx*nx+ny*ny+nz*nz));
+ }
+ return NULL;
+}
/* ---------------------------------------------------------------------
* Some stuff taken from the header
* --------------------------------------------------------------------- */
Index: source/gameengine/Ketsji/KX_GameObject.h
===================================================================
RCS file:
/cvsroot/bf-blender/blender/source/gameengine/Ketsji/KX_GameObject.h,v
retrieving revision 1.11
diff -u -r1.11 KX_GameObject.h
--- source/gameengine/Ketsji/KX_GameObject.h 26 May 2004 12:06:41 -0000 1.11
+++ source/gameengine/Ketsji/KX_GameObject.h 10 Jan 2005 20:54:08 -0000
@@ -613,6 +613,7 @@
KX_PYMETHOD(KX_GameObject,GetMesh);
KX_PYMETHOD(KX_GameObject,GetParent);
KX_PYMETHOD(KX_GameObject,GetPhysicsId);
+ KX_PYMETHOD(KX_GameObject,GetDistance);

private :

```