Closed 8Observer8 closed 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);
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
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) {}
});
}
}
I want to try to rewrite my Pong game from
@box2d/core
tobox2d-wasm
because I was stuck with the issue: https://github.com/Lusito/box2d.ts/issues/42These is no any examples in docs about b2ContactListener. How to rewrite the code below from
@box2d/core
tobox2d-wasm
:contact-listener.js
index.js