Basic Examples of Using box2d/core with Canvas API and JavaScript #47
Replies: 3 comments
-
Debug drawer of box2d/core colliders This example uses the b2Draw class to draw colliders. Playground: https://plnkr.co/edit/4QdnwIgKEtOiUHC0?preview GitHub: https://github.com/8Observer8/debug-drawer-box2dcore-canvas2d-js |
Beta Was this translation helpful? Give feedback.
-
Click detection using QueryAABB This example uses QueryAABB to detect a mouse click: function getBodyCallback(fixture) {
const userData = fixture.GetUserData();
if (userData) {
console.log(userData.name);
}
}
canvas.onmousedown = (e) => {
const mouseX = (e.clientX - canvas.offsetLeft) / pixelsPerMeter;
const mouseY = (e.clientY - canvas.offsetTop) / pixelsPerMeter;
const aabb = new b2AABB();
aabb.lowerBound.Set(mouseX - 0.001, mouseY - 0.001);
aabb.upperBound.Set(mouseX + 0.001, mouseY + 0.001);
world.QueryAABB(aabb, getBodyCallback);
} Playground: https://plnkr.co/edit/snCTZkNjSyBiCuic?preview GitHub: https://github.com/8Observer8/click-detection-using-queryaabb-box2dcore-canvas2d-js |
Beta Was this translation helpful? Give feedback.
-
Drag objects with mouse Playground: https://plnkr.co/edit/KTsPCuEoJMEjYGqw?preview GitHub: https://github.com/8Observer8/drag-with-mouse-box2dcore-canvas2d-js |
Beta Was this translation helpful? Give feedback.
-
Table of contents:
Beta Was this translation helpful? Give feedback.
All reactions