How to use setUserPointer/getUserPointer from Ammo.js

I want to print names of colliding objects. I made a very simple example.

I created an object to keep a user data:

        const userData = { name: name };

I keep this object in a body using setUserPointer:

body.setUserPointer(userData);

I try to get this names using getUserPointer and print them. But I get “undefined” instead of names:

function detectCollison(): void
{
    const dispatcher = physicsWorld.getDispatcher();
    const numManifolds = dispatcher.getNumManifolds();

    for (let i = 0; i < numManifolds; i++)
    {
        const contactManifold = dispatcher.getManifoldByIndexInternal(i);
        const body0 = contactManifold.getBody0();
        const body1 = contactManifold.getBody1();

        const p0 = body0.getUserPointer();
        const p1 = body1.getUserPointer();

        console.log("first object: " + p0.name);
        console.log("second object: " + p1.name);
    }
}

Edited

This is pure TypeScript version. I tried to keep names as a body property but it does not work too. It prints “undefined” instead of names:

        (this.body as any).name = name;

        physicsWorld.addRigidBody(this.body);
function detectCollison(): void
{
    const dispatcher = physicsWorld.getDispatcher();
    const numManifolds = dispatcher.getNumManifolds();

    for (let i = 0; i < numManifolds; i++)
    {
        const contactManifold = dispatcher.getManifoldByIndexInternal(i);
        const body0 = contactManifold.getBody0();
        const body1 = contactManifold.getBody1();

        console.log("first object: " + (body0 as any).name);
        console.log("second object: " + (body1 as any).name);
    }
}

Answer

Solution:

const userData = { name: name };
(this.body as any).userData = userData;
function detectCollison(): void
{
    const dispatcher = physicsWorld.getDispatcher();
    const numManifolds = dispatcher.getNumManifolds();

    for (let i = 0; i < numManifolds; i++)
    {
        const contactManifold = dispatcher.getManifoldByIndexInternal(i);
        const body0 = contactManifold.getBody0();
        const body1 = contactManifold.getBody1();

        const rb0 = (Ammo as any).castObject( contactManifold.getBody0(), Ammo.btRigidBody );
        const rb1 = (Ammo as any).castObject( contactManifold.getBody1(), Ammo.btRigidBody );

        console.log("first object:", rb0.userData);
        console.log("second object:", rb1.userData);
    }
}