3 Commits

Author SHA1 Message Date
John Alanbrook
c014f29ce7 fill out box2d methods
Some checks failed
Build and Deploy / build-macos (push) Failing after 6s
Build and Deploy / build-linux (push) Failing after 1m30s
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
2025-05-25 01:58:10 -05:00
John Alanbrook
f1b3e5eddc Merge branch 'master' into box2d
Some checks failed
Build and Deploy / build-macos (push) Failing after 5s
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
Build and Deploy / build-linux (push) Has been cancelled
2025-05-24 22:45:52 -05:00
John Alanbrook
2594c03765 initial box2d integration
All checks were successful
Build and Deploy / build-linux (push) Successful in 1m12s
Build and Deploy / build-windows (CLANG64) (push) Successful in 9m33s
Build and Deploy / package-dist (push) Has been skipped
Build and Deploy / deploy-itch (push) Has been skipped
Build and Deploy / deploy-gitea (push) Has been skipped
2025-02-26 07:47:04 -06:00
12 changed files with 2817 additions and 15 deletions

View File

@@ -0,0 +1,8 @@
var config = {
title: "Box2D Physics Demo",
width: 1280,
height: 720,
fullscreen: false
}
return config

343
examples/box2d_demo/main.js Normal file
View File

@@ -0,0 +1,343 @@
var box2d = use('box2d')
var moth = use('moth', $_.delay)
moth.initialize()
var draw2d = use('draw2d')
// Physics world setup
var world = new box2d.World({
gravity: {x: 0, y: -10}
})
// Ground body (static)
var ground = world.createBody({
type: 'static',
position: {x: 0, y: -10}
})
var groundShape = ground.createBoxShape({
width: 50,
height: 0.5,
density: 0,
friction: 0.7
})
// Walls
var leftWall = world.createBody({
type: 'static',
position: {x: -25, y: 0}
})
leftWall.createBoxShape({
width: 0.5,
height: 30,
density: 0
})
var rightWall = world.createBody({
type: 'static',
position: {x: 25, y: 0}
})
rightWall.createBoxShape({
width: 0.5,
height: 30,
density: 0
})
// Dynamic bodies array
var boxes = []
var circles = []
// Create some dynamic boxes
for (var i = 0; i < 5; i++) {
var box = world.createBody({
type: 'dynamic',
position: {x: -10 + i * 4, y: 5 + i * 3},
angle: Math.random() * Math.PI
})
box.createBoxShape({
width: 2,
height: 2,
density: 1.0,
friction: 0.3,
restitution: 0.5
})
boxes.push(box)
}
// Create some circles
for (var i = 0; i < 3; i++) {
var circle = world.createBody({
type: 'dynamic',
position: {x: 5 + i * 3, y: 10 + i * 2}
})
circle.createCircleShape({
radius: 1,
density: 0.5,
friction: 0.2,
restitution: 0.8
})
circles.push(circle)
}
// Connected bodies with distance joint
var bodyA = world.createBody({
type: 'dynamic',
position: {x: -5, y: 15}
})
bodyA.createCircleShape({
radius: 0.5,
density: 1.0
})
var bodyB = world.createBody({
type: 'dynamic',
position: {x: 0, y: 15}
})
bodyB.createCircleShape({
radius: 0.5,
density: 1.0
})
var joint = world.createDistanceJoint({
bodyA: bodyA,
bodyB: bodyB,
localAnchorA: {x: 0, y: 0},
localAnchorB: {x: 0, y: 0},
length: 5
})
// Mouse interaction
var mouseBody = null
var mousePressed = false
// Game state
var camera = {x: 0, y: 0, zoom: 10}
// Input state
var keys = {}
var mouse = {
pos: {x: 0, y: 0},
buttons: [false, false, false]
}
// Main update function
prosperon.on('update', function(dt) {
// Step physics simulation
world.step(dt, 4)
// Camera controls
if (keys[4]) camera.x -= 20 * dt // A
if (keys[7]) camera.x += 20 * dt // D
if (keys[26]) camera.y += 20 * dt // W
if (keys[22]) camera.y -= 20 * dt // S
if (keys[20]) camera.zoom *= 1 + dt // Q
if (keys[8]) camera.zoom *= 1 - dt // E
// Mouse interaction
var mouseWorld = screenToWorld(mouse.pos)
// Raycast to find body under mouse
var result = world.rayCast(mouseWorld, {x: 0, y: -1}, 0.1)
if (!result.hit) {
// Create new body at mouse position
if (keys[225]) { // LSHIFT
// Create circle
var newCircle = world.createBody({
type: 'dynamic',
position: mouseWorld
})
newCircle.createCircleShape({
radius: 0.5 + Math.random(),
density: 1.0,
restitution: 0.3 + Math.random() * 0.5
})
circles.push(newCircle)
} else {
// Create box
var newBox = world.createBody({
type: 'dynamic',
position: mouseWorld,
angle: Math.random() * Math.PI * 2
})
newBox.createBoxShape({
width: 1 + Math.random() * 2,
height: 1 + Math.random() * 2,
density: 1.0,
restitution: 0.2 + Math.random() * 0.3
})
boxes.push(newBox)
}
}
boxes.forEach(function(box) {
var dir = {
x: box.position.x - mouseWorld.x,
y: box.position.y - mouseWorld.y
}
var dist = Math.sqrt(dir.x * dir.x + dir.y * dir.y)
if (dist < 10 && dist > 0.1) {
dir.x /= dist
dir.y /= dist
box.applyLinearImpulse({x: dir.x * 50, y: dir.y * 50})
}
})
// Reset with R
if (keys[21] && !keys[21 + '_prev']) { // R key pressed
// Reset all dynamic bodies
boxes.forEach(function(box) {
box.position = {x: Math.random() * 20 - 10, y: 10 + Math.random() * 10}
box.angle = Math.random() * Math.PI * 2
box.linearVelocity = {x: 0, y: 0}
box.angularVelocity = 0
})
circles.forEach(function(circle) {
circle.position = {x: Math.random() * 20 - 10, y: 10 + Math.random() * 10}
circle.linearVelocity = {x: 0, y: 0}
})
}
// Update previous key states
for (var k in keys) {
if (k.indexOf('_prev') === -1) {
keys[k + '_prev'] = keys[k]
}
}
})
// Event handlers
prosperon.on('key_down', function(e) {
keys[e.scancode] = true
})
prosperon.on('key_up', function(e) {
keys[e.scancode] = false
})
prosperon.on('mouse_button_down', function(e) {
mouse.buttons[e.which] = true
if (e.which === 0 && !mousePressed) {
mousePressed = true
var mouseWorld = screenToWorld(mouse.pos)
// Raycast to find body under mouse
var result = world.rayCast(mouseWorld, {x: 0, y: -1}, 0.1)
if (!result.hit) {
// Create new body at mouse position
if (keys[225]) { // LSHIFT
// Create circle
var newCircle = world.createBody({
type: 'dynamic',
position: mouseWorld
})
newCircle.createCircleShape({
radius: 0.5 + Math.random(),
density: 1.0,
restitution: 0.3 + Math.random() * 0.5
})
circles.push(newCircle)
} else {
// Create box
var newBox = world.createBody({
type: 'dynamic',
position: mouseWorld,
angle: Math.random() * Math.PI * 2
})
newBox.createBoxShape({
width: 1 + Math.random() * 2,
height: 1 + Math.random() * 2,
density: 1.0,
restitution: 0.2 + Math.random() * 0.3
})
boxes.push(newBox)
}
}
}
if (e.which === 1) {
// Apply impulse with right click
var mouseWorld = screenToWorld(mouse.pos)
boxes.forEach(function(box) {
var dir = {
x: box.position.x - mouseWorld.x,
y: box.position.y - mouseWorld.y
}
var dist = Math.sqrt(dir.x * dir.x + dir.y * dir.y)
if (dist < 10 && dist > 0.1) {
dir.x /= dist
dir.y /= dist
box.applyLinearImpulse({x: dir.x * 50, y: dir.y * 50})
}
})
}
})
prosperon.on('mouse_button_up', function(e) {
mouse.buttons[e.which] = false
if (e.which === 0) {
mousePressed = false
}
})
prosperon.on('mouse_motion', function(e) {
mouse.pos = e.pos
})
// Rendering
prosperon.on('draw', function() {
// Clear background
// Draw ground
drawBox(ground.position, 50, 0.5, ground.angle, {r: 0.5, g: 0.5, b: 0.5, a: 1})
// Draw walls
drawBox(leftWall.position, 0.5, 30, 0, {r: 0.5, g: 0.5, b: 0.5, a: 1})
drawBox(rightWall.position, 0.5, 30, 0, {r: 0.5, g: 0.5, b: 0.5, a: 1})
// Draw boxes
boxes.forEach(function(box) {
drawBox(box.position, 2, 2, box.angle, {r: 0.8, g: 0.3, b: 0.3, a: 1})
})
// Draw circles
circles.forEach(function(circle) {
draw2d.circle(circle.position, 1, {r: 0.3, g: 0.8, b: 0.3, a: 1})
})
// Draw connected bodies
draw2d.circle(bodyA.position, 0.5, {r: 0.8, g: 0.8, b: 0.3, a: 1})
draw2d.circle(bodyB.position, 0.5, {r: 0.8, g: 0.8, b: 0.3, a: 1})
draw2d.line([bodyA.position, bodyB.position], {r: 1, g: 1, b: 0, a: 0.5})
// Draw UI
draw2d.text("Box2D Demo", {x: 10, y: 10}, 20, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("Controls:", {x: 10, y: 40}, 16, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- WASD: Move camera", {x: 10, y: 60}, 14, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- Q/E: Zoom in/out", {x: 10, y: 80}, 14, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- Left click: Create box", {x: 10, y: 100}, 14, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- Shift + Left click: Create circle", {x: 10, y: 120}, 14, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- Right click: Apply impulse", {x: 10, y: 140}, 14, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- R: Reset bodies", {x: 10, y: 160}, 14, {r: 1, g: 1, b: 1, a: 1})
// Show physics stats
draw2d.text("Bodies: " + (boxes.length + circles.length + 4), {x: 10, y: 200}, 14, {r: 1, g: 1, b: 1, a: 1})
})
// Helper functions
function drawBox(pos, width, height, angle, color) {
draw2d.rectangle({x:pos.x,y:pos.y,width, height})
}
function screenToWorld(screenPos) {
return {
x: (screenPos.x - prosperon.x * 0.5) / camera.zoom + camera.x,
y: (screenPos.y - prosperon.y * 0.5) / camera.zoom + camera.y
}
}

View File

@@ -94,6 +94,18 @@ sdl3_opts.add_cmake_defines({
'SDL_PULSEAUDIO': 'ON',
})
box2d_opts = cmake.subproject_options()
box2d_opts.add_cmake_defines({
'BOX2D_SAMPLES': 'OFF',
'BOX2D_BUILD_STATIC': 'ON',
'BOX2d_AVX2': 'ON',
'BOX2D_BUILD_SHARED': 'OFF',
'CMAKE_BUILD_TYPE': 'Release',
})
box2d_proj = cmake.subproject('box2d', options: box2d_opts)
deps += box2d_proj.dependency('box2d')
cc = meson.get_compiler('c')
if host_machine.system() == 'darwin'
@@ -142,7 +154,6 @@ deps += dependency('qjs-layout', static:true)
deps += dependency('qjs-miniz', static:true)
deps += dependency('physfs', static:true)
deps += dependency('threads')
deps += dependency('chipmunk', static:true)
if host_machine.system() != 'emscripten'
deps += dependency('enet', static:true)
@@ -192,12 +203,14 @@ endif
link_args = link
sources = []
src += [
'anim.c', 'config.c', 'datastream.c','font.c','HandmadeMath.c','jsffi.c','model.c',
'render.c','simplex.c','spline.c', 'transform.c','prosperon.c', 'wildmatch.c',
'sprite.c', 'rtree.c', 'qjs_nota.c', 'qjs_soloud.c', 'qjs_sdl.c', 'qjs_math.c', 'qjs_geometry.c', 'qjs_transform.c', 'qjs_sprite.c', 'qjs_io.c', 'qjs_os.c', 'qjs_actor.c',
'qjs_qr.c', 'qjs_wota.c', 'monocypher.c', 'qjs_blob.c', 'qjs_crypto.c', 'qjs_time.c', 'qjs_http.c', 'qjs_rtree.c', 'qjs_spline.c', 'qjs_js.c', 'qjs_debug.c'
]
src += 'qjs_box2d.c'
# quirc src
src += [
'thirdparty/quirc/quirc.c', 'thirdparty/quirc/decode.c',

View File

@@ -1,5 +1,7 @@
#include "HandmadeMath.h"
#include <stdlib.h>
const HMM_Vec2 v2zero = {0,0};
const HMM_Vec2 v2one = {1,1};
const HMM_Vec3 v3zero = {0,0,0};

View File

@@ -96,8 +96,6 @@
#ifndef HANDMADE_MATH_H
#define HANDMADE_MATH_H
#include <chipmunk/chipmunk.h>
#if !defined(HANDMADE_MATH_NO_SIMD)
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
#define HANDMADE_MATH__USE_NEON 1
@@ -272,12 +270,6 @@ typedef union HMM_Vec3 {
HMM_Vec2 VW;
};
struct
{
HMM_Vec2 cp;
float _Ignored5;
};
float Elements[3];
float e[3];
@@ -373,12 +365,6 @@ typedef union HMM_Vec4 {
HMM_Vec2 ZW;
};
struct
{
HMM_Vec2 cp;
HMM_Vec2 wh;
};
HMM_Quat quat;
struct {float x, y, z, w; };
struct {float r, g, b, a; };

View File

@@ -32,6 +32,7 @@
#include "qjs_dmon.h"
#include "qjs_nota.h"
#include "qjs_wota.h"
#include "qjs_box2d.h"
#include "qjs_enet.h"
#include "qjs_soloud.h"
#include "qjs_qr.h"
@@ -48,6 +49,7 @@
#include "qjs_spline.h"
#include "qjs_js.h"
#include "qjs_debug.h"
#include "qjs_box2d.h"
#ifndef NSTEAM
#include "qjs_steam.h"
#endif
@@ -2895,6 +2897,7 @@ void ffi_load(JSContext *js)
arrput(rt->module_registry, MISTLINE(enet));
arrput(rt->module_registry, MISTLINE(qr));
arrput(rt->module_registry, MISTLINE(wota));
arrput(rt->module_registry, MISTLINE(box2d));
arrput(rt->module_registry, MISTLINE(crypto));
arrput(rt->module_registry, MISTLINE(blob));
arrput(rt->module_registry, MISTLINE(http));
@@ -2903,6 +2906,7 @@ void ffi_load(JSContext *js)
arrput(rt->module_registry, MISTLINE(rtree));
arrput(rt->module_registry, MISTLINE(sprite));
arrput(rt->module_registry, MISTLINE(transform));
arrput(rt->module_registry, MISTLINE(box2d));
#ifdef TRACY_ENABLE
arrput(rt->module_registry, MISTLINE(tracy));

2341
source/qjs_box2d.c Normal file

File diff suppressed because it is too large Load Diff

8
source/qjs_box2d.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef QJS_BOX2D_H
#define QJS_BOX2D_H
#include "quickjs.h"
JSValue js_box2d_use(JSContext*);
#endif

View File

@@ -2,6 +2,7 @@
#include "stb_ds.h"
#include "transform.h"
#include "math.h"
#include "float.h"
/* -------------------------------------------------------------------------
Cubic Spline Basis Matrices

View File

@@ -1,5 +1,7 @@
#include "sprite.h"
#include <stdlib.h>
sprite *make_sprite(void)
{
sprite *sprite = calloc(sizeof(*sprite),1);

4
subprojects/box2d.wrap Normal file
View File

@@ -0,0 +1,4 @@
[wrap-git]
directory=box2d
url=https://github.com/erincatto/box2d.git
revision=v3.0.0

90
tests/box2d.js Normal file
View File

@@ -0,0 +1,90 @@
var box2d = use('box2d')
// Create world with constructor
var world = new box2d.World({gravity: {x: 0, y: -9.8}})
console.log("Initial gravity:", world.gravity)
// Create a dynamic body
var body = world.createBody({
type: 'dynamic',
position: {x: 0, y: 10}
})
// Add a box shape to the body
var shape = body.createBoxShape({
width: 1,
height: 1,
density: 1.0,
friction: 0.3,
restitution: 0.5
})
console.log("Initial position:", body.position)
console.log("Initial velocity:", body.linearVelocity)
// Simulate
world.step(1/60, 4);
console.log("After 1 step:", body.position)
world.step(1/60, 4);
console.log("After 2 steps:", body.position)
world.step(1/60, 4);
console.log("After 3 steps:", body.position)
// Test applying forces
body.applyForce({x: 100, y: 0})
world.step(1/60, 4);
console.log("After force:", body.position, "velocity:", body.linearVelocity)
// Test properties
console.log("Body type:", body.type)
console.log("Body mass:", body.getMass())
console.log("Body angle:", body.angle)
// Test string body types
var staticBody = world.createBody({
type: 'static',
position: {x: 0, y: 0}
})
console.log("Static body type:", staticBody.type)
// Test ray casting
var rayResult = world.rayCast({x: -5, y: 10}, {x: 10, y: 0}, 1.0)
console.log("Ray cast result:", rayResult)
// Test distance joint
var bodyA = world.createBody({
type: 'dynamic',
position: {x: -2, y: 5}
})
bodyA.createCircleShape({
radius: 0.5,
density: 1.0
})
var bodyB = world.createBody({
type: 'dynamic',
position: {x: 2, y: 5}
})
bodyB.createCircleShape({
radius: 0.5,
density: 1.0
})
var joint = world.createDistanceJoint({
bodyA: bodyA,
bodyB: bodyB,
localAnchorA: {x: 0, y: 0},
localAnchorB: {x: 0, y: 0},
length: 4.0
})
console.log("Created distance joint")
// Test multiple steps with joint
for (var i = 0; i < 10; i++) {
world.step(1/60, 4)
}
console.log("Body A position after joint simulation:", bodyA.position)
console.log("Body B position after joint simulation:", bodyB.position)