Birch-san / box2d-wasm

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

How to use b2ContactListener in box2d-wasm #62

Closed 8Observer8 closed 12 months ago

8Observer8 commented 12 months ago

I want to try to rewrite my Pong game from @box2d/core to box2d-wasm because I was stuck with the issue: https://github.com/Lusito/box2d.ts/issues/42

These is no any examples in docs about b2ContactListener. How to rewrite the code below from @box2d/core to box2d-wasm:

contact-listener.js

import { b2ContactListener, b2Vec2 } from "@box2d/core";

export default class ContactListener extends b2ContactListener {

    constructor() {
        super();
    }

    BeginContact(contact) {
        console.log("ok");
    }
}

index.js

import ContactListener from "./contact-listener.js";

const contactListener = new ContactListener();
world.SetContactListener(contactListener);
Deflaktor commented 12 months ago

I have an example in typescript

const contactListener = Object.assign(new box2D.JSContactListener(), {
    BeginContact(contact: Box2D.b2Contact | number) {
        console.log("BeginContact");
    },
    EndContact(contact: Box2D.b2Contact | number) {
        console.log("EndContact");
    },
    PreSolve(contact: Box2D.b2Contact | number, oldManifold: Box2D.b2Manifold | number) {
        console.log("PreSolve");
        console.log(contact)
        if (typeof contact === 'number') {
            contact = box2D.wrapPointer(contact, box2D.b2Contact);
        }
        const bodyA = contact.GetFixtureA().GetBody();
        const bodyB = contact.GetFixtureB().GetBody();
    },
    PostSolve(contact: Box2D.b2Contact | number, impulse: Box2D.b2ContactImpulse | number) {
        console.log("PostSolve");
    }
});
this._world.SetContactListener(contactListener);
8Observer8 commented 12 months ago

Thank you very much!

Playground: https://plnkr.co/edit/PY7KoZhEH5ysC7uy?preview

GitHub: https://github.com/8Observer8/falling-box-contact-listener-box2dwasm-webgl-js

Discussion: https://github.com/Birch-san/box2d-wasm/discussions/63

falling-box-contact-listener-box2dwasm-webgl-js

contact-listener.js

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

export default class ContactListener {

    constructor(metaData) {
        this.metaData = metaData;

        const {
            b2Contact,
            getPointer,
            JSContactListener,
            wrapPointer
        } = box2d;

        const self = this;
        this.instance = Object.assign(new JSContactListener(), {
            BeginContact(contact) {
                // console.log("BeginContact");
                contact = wrapPointer(contact, b2Contact);
                // console.log(contact);
                const fixtureA = contact.GetFixtureA();
                const fixtureB = contact.GetFixtureB();
                const nameA = self.metaData[getPointer(fixtureA)].name;
                const nameB = self.metaData[getPointer(fixtureB)].name;
                console.log(`nameA = "${nameA}"`);
                console.log(`nameB = "${nameB}"`);
            },
            EndContact(contact) {},
            PreSolve(contact) {},
            PostSolve(contact) {}
        });
    }
}