# Vertex local coordinates to global coordinates

So, I can get vertex positions but that only in local coordinates. How do I convert these to global coordinates and then convert them back?

The local vertex positions are relative to the center of the object they belong to (the “object center” is not always at the center of the mesh). The object center can be retrieved using GameObject.position (this is the new way of getting the position, instead of using GameObject.getPosition() as we did in the past. getPosition() will no longer be supported in Blender 2.5)

Just use simple addition from there to calculate the global position of the vertex. (As in, the object center X + the vertex local X = the vertex global X). To reverse the operation, subtract the object position from the global vertex position.

Thats what I originally thought. When you get the local coordinate of the vertex it is returned as if the object has no rotation applied to it.

So I have to somehow use the orientation matrix to manipulate the positions?

I should have figured it couldn’t be as simple as it seemed it should be. That’s a very complicated problem indeed, and the only solutions I can imagine would involve rather complex math (at least, complex at my skill level).

Are you trying to find the location of random vertices? Like the vertex where a collision occurs? Or is it the same vertex every time?

The same vertex everytime. I’m asking this because I believe its the solution to this problem:

Also, I found a thread here showing that it can be done very easily, but its the wrong API ):

Well, like I said, I don’t know the right math to use, but vector and matrix math are possible in the Game Engine using the Mathutils library ( import Mathutils ). If you’re a bit better at that sort of thing than I am, you can find the documentation here:
http://www.blender.org/documentation/248PythonDoc/GE/index.html

Follow the link to the Mathutils library. It is apparently a copy of Blender.Mathutils, so it links to that documentation, but you should be able to use the same function calls (just don’t import Blender.Mathutils, import Mathutils instead…elsewise it won’t work in a runtime).

Edit: Oh, btw. One problem you may run into trying to solve that problem is that collision meshes currently cannot be updated once the game starts. So if you move the vertex, it will not update the collision mesh.

I’m not sure, but maybe it is what you need or at least it can help you

check the code in the 10º post.

Mathutils may be what I need, if I set up the matrix and position and multiply them I may get global coordinates.

Oh, btw. One problem you may run into trying to solve that problem is that collision meshes currently cannot be updated once the game starts. So if you move the vertex, it will not update the collision mesh.
Luckily I don’t need collision, but thanks for telling me this. Is there any plans to fix this?

I’m not sure, but maybe it is what you need or at least it can help you

check the code in the 10º post.
Unfortunately the code is not much help, thanks for posting though.

The BGE API could make this easy
vecWorld= gameObject.worldSpacePoint(vecLocal)
vecLocal= gameObject.worldSpacePoint(vecWorld)

• Should be a lot faster then doing it in python, worth adding?

I think on the methods that return and set positions should have a ‘local’ argument, like the motion actuator.

Anyway, I got it working, thanks blendezno. Heres what I got:

``````import Mathutils

cont = GameLogic.getCurrentController()
own  = cont.getOwner()

ori  = own.getOrientation()

mesh = own.getMesh()

XYZ  = mesh.getVertex(0,1).getXYZ()

mat  = Mathutils.Matrix(ori[0],ori[1],ori[2])

vec  = Mathutils.Vector(XYZ[0],XYZ[1],XYZ[2])

globalXYZ  = mat*vec
``````

Had no idea what I was doing but it worked out for me first go.

Edit: forgot to add the objects position into the vector, but yea, you get the idea.

Andrew,

Glad to hear it worked out. Very sharp script you have there, too.

I only found out about the Mathutils library last week. I sure could have used it several weeks ago when I needed to calculate the angle between two vectors. Instead I had to re-teach myself vector math from an online textbook, which, while invigorating (in a strange, mathematical way) was not how I had intended to spend those few hours.

Campbell,

``````GameObject.mesh.vertex[material][vert_index].local_coords