# world coordinates to local?

how do i convert world coordinates to local coordinates and vice versa? I would think the function takes a KX_GameObject and a [x, y, z] list (world coordinates), or maybe there is another method you have to use.

Bumpity, bump, bump

You know bumping should not be used so often? Its kinda spam you know? And its quite annoying too…

Please respect other posters, thank you

EDIT: I’m not trying to be mean, just so you know…

Local coordinates like a child parented to something else?

``````from Mathutils import Vector, Matrix

parent = ...
child = parent.children['OBchild']

parent_pos = Vector(parent.position)
child_pos = Vector(child.position)
parent_ori = Matrix(parent.orientation[0], parent.orientation[1], parent.orientation[2])

local_pos = parent_ori * (parent_pos - child_pos)
``````

The Mathutils module is very handy and is the default class for positions and matrices in 2.5, which means in 2.5 the above code would just be:

``````local_pos = parent.orientation * (parent.position - child.position)
``````

I hope I understood what you meant by local position

andrew-101: But if the object is already parented, why not use

``````local_pos = child.localPosition
``````

Im sure you know what world cordinates are, this is what local cordinates are:

in local cordinates the origin (0, 0, 0) is the objects center. When you place a verticie on an object, you place it on its local cordinates. When you move an object, its local cordinates stay the same, but the world cordinates change. The also applys for rotating an object.

The reason I need to convert world to local is to add a contraint to an object. I have the world cordinates I would like the constraint to be added at, but the addConstraint function takes local cordinates. It has nothing to do with parenting. I have a world location ([x, y, z] list) and a KX_GameObject, and I need to turn the world cordinates into the game objects local cordinates, maybe this is a little clearer. Any other help would be great, thx.

Im sorry, I will stop bumping as much, only when my thread is 3/4 down the first page and I really need an answer.

You use relative position, which is fairly simple.

Global coordinates will tell you where an object is relative to the global origin. So you stand at the global origin and given the objects global coordinates you can tell where it is.

Local coordinates are relative to an objects origin, or the objects center in blenders case.

So, in your case we have your object located at point A, lets say. You can get that easily using KX_GameObject functions. And you have a constraint located at point B in global coordinates. What you want is the location of B relative to the local coordinate system at A. In vector mathematics you would right it out like this:
B = A + B wrt A
You want B wrt A, so you get:
B wrt A = B - A

If you want the local coordinates you take your constraint location and subtract the objects position and that will be your local coordinates.

Probably an overly long and more indepth reply than what you wanted, but it does help to know how it’s done, than just getting the answer. Hope this helps!

I get what your saying, and this is how I did it in the first place, I got the constraint offset by subtracting it from the objects position, but the problem with this is that it doesn’t take the objects rotation into accout, you would probably need to use some trig also, which im a little rusty on.

I think for rotation it should be the same operation, just with vectors? ( I’m a bit rusty with rotations )

You take the vector A direction, the vector B direction, and subtract them, will that do?

I dont really get what you mean, maybe a blend file?

B.rot wrt A.rot = B.rot - A.rot

What you need is the rotation of the objects, and for this you can look at andrew-101 post, if you feel confortable with matrices…

Though I don’t know if thats matematically correct, as I said I don’t usually use this operations…

EDIT: I don’t know why I wrote vector, maybe I’m just sleepy -.-

lol, i just noticed somthing, in the second post down from the top you wrote EDIT: but the post wasn’t even edited. Any I think andrew method was right, just haven’t got a chance to test it yet.

LoL, you’re right xD I don’t know how its possible though o_O Maybe I was so heavily sleepy I wrote EDIT without editing, dunno lol, its impossible…

Yes his method should be right, just you don’t need to get the child, but the second object

Cya =)

well, I coulden’t get it to work, any other ideas guys?

There isn’t a second object, just a world coordinate [x,y,z] list, so how can i turn that into a local coordinate of an object that also accounts for the objects rotation?

I know the fist part is:

locPos = worldCord - Obj.position

but how do i rotate locPos around the objects center, using the objects current rotation?

i figured it out:

``````
orientation = selObj.orientation
ori_mat = Matrix(orientation[0], orientation[1], orientation[2])

local_pos = (Vector(worldPos) - Vector(selObj.position)) * ori_mat

``````

for some odd reason:

``````
# this returns the correct local position
(Vector(worldPos) - Vector(selObj.position)) * ori_mat

``````

gets a different result than what Andrew said:

``````
# this returns something else
ori_mat * (Vector(worldPos) - Vector(selObj.position))

``````

I know my order of operations, but why are these giving different results? Is there something i’m missing?

Multiplication isn’t commutative for matrices, thats what you aren’t getting the same answer. Orientation is given as a matrix, not a vector.

Ohh, I didn’t even think of that, I thought it always went by the table of precedence, thats weird?

I still don’t understand what you mean for local, the position is local with respect to something, local to the world is the global position…

Sorry I can’t understand what you need…

Strange, I always thought the matrix went first as I think thats what I always do. If you say it works vector first (which does make more sense) than I guess I just got myself a bit confused

The reason Padre posted EDIT and it doesn’t say the post was edited is because if you edit your post fast enough (within a minute or something) it won’t show that it was edited. I think thats for the spelling mistakes you only notice after you post

@supersocks:
Because I’m a big silly head who didn’t know it existed