Birch-san / box2d-wasm

Box2D physics engine compiled to WebAssembly. Supports TypeScript and ES modules.
263 stars 21 forks source link

liquidfun's GetPositionBuffer() doesn't return a list #77

Open 8Observer8 opened 2 weeks ago

8Observer8 commented 2 weeks ago

I try to use liquid fun. GetPositionBuffer doesn't return a list:

    const positions = particleSystem.GetPositionBuffer();
    console.log(positions[0]);

I prints undefined.

But it must return a list because C++ works like this:

    b2Vec2 *pos = m_particleSystem->GetPositionBuffer();
    qDebug() << pos[0].x  << pos[0].y;

Full example that includes the liquid fun lib library: get-position-buffer-box2dwasm-js.zip

index.js

import { box2d, initBox2D } from "./init-box2d.js";

async function init() {
    await initBox2D();

    const {
        b2CircleShape,
        b2ParticleGroupDef,
        b2ParticleSystemDef,
        b2Vec2,
        b2World
    } = box2d;

    const world = new b2World();
    const pixelsPerMeter = 50;

    const particleSystemDef = new b2ParticleSystemDef();
    particleSystemDef.radius = 5 / pixelsPerMeter;
    const particleSystem = world.CreateParticleSystem(particleSystemDef);
    const spawnArea = new b2CircleShape();
    spawnArea.m_radius = 20 / pixelsPerMeter;
    spawnArea.m_p.x = 100 / pixelsPerMeter;
    spawnArea.m_p.y = 30 / pixelsPerMeter;
    const particleGroupDefinition = new b2ParticleGroupDef();
    particleGroupDefinition.shape = spawnArea;
    particleSystem.CreateParticleGroup(particleGroupDefinition);

    const positions = particleSystem.GetPositionBuffer();
    console.log(positions[0].x);
}

init();

index.html

<!DOCTYPE html>

<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Liquid fun</title>
</head>

<body>
    <script type="importmap">
        {
            "imports": {
                "box2d-wasm": "/libs/box2d-wasm@7.0.0/es/entry.js"
            }
        }
    </script>

    <script type="module" src="./js/index.js"></script>
</body>

</html>
8Observer8 commented 2 weeks ago

And I have this messages: wasm streaming and falling back to ArrayBuffer when I run the example above:

image

flygOn-LiTe commented 2 weeks ago

I am having the same issue. The GetPositionBuffer method is returning a single object with x and y properties instead of a buffer. I can confirm all particles are there when debugging but cannot render the particles due to GetPositionBuffer not working as expected.