Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Convert transform from world to local space

$
0
0

I have a hierarchical node system. Given a world space transform, I need to obtain the transform for a specific node in local space.

My Node class has the following method.

public void worldToLocal(Matrix4 world, Matrix4 local) {
    if (dirty) update();
    local.set(worldInverse).mul(world);
}

worldInverse is the inverse of the node’s world transformation matrix.

I’m using LibGDX, so this is equivalent to doing:

LOCAL = T^-1 * WORLD

T^-1 being the inverse of the node’s world transform. However, this doesn’t seem to be working correctly.

Let’s say we have a node at (2, 4, 0) rotated 180 degrees along its Z axis.

    Node node = new Node();
    node.setPosition(2.0f, 4.0f, 0.0f);
    node.rotate(new Vector3(0.0f, 0.0f, 1.0f), 180.0f);

Now, the point (1, 7, 0) should be (2, 1, 0) in local space for that node.

    Matrix4 local = new Matrix4();
    Matrix4 world = new Matrix4();
    Vector3 pos = new Vector3();

    world.translate(1.0f, 7.0f, 0.0f);
    node.worldToLocal(world, local);
    local.getTranslation(pos);

However, I get (1, -3, 0).

I’m using this operation to translate from world space Bullet body transforms to local node transforms.


Viewing all articles
Browse latest Browse all 434

Trending Articles