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
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
This commit is contained in:
8
examples/box2d_demo/config.js
Normal file
8
examples/box2d_demo/config.js
Normal 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
343
examples/box2d_demo/main.js
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
@@ -2896,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));
|
||||
|
||||
2416
source/qjs_box2d.c
2416
source/qjs_box2d.c
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,90 @@
|
||||
var box2d = use('box2d')
|
||||
|
||||
var world = box2d.world()
|
||||
console.log(world.gravity)
|
||||
world.gravity = {x:0,y:-9.8}
|
||||
console.log(world.gravity)
|
||||
var body = world.body()
|
||||
// Create world with constructor
|
||||
var world = new box2d.World({gravity: {x: 0, y: -9.8}})
|
||||
console.log("Initial gravity:", world.gravity)
|
||||
|
||||
world.step(10, 4);
|
||||
world.step(10, 4);
|
||||
world.step(10, 4);
|
||||
// Create a dynamic body
|
||||
var body = world.createBody({
|
||||
type: 'dynamic',
|
||||
position: {x: 0, y: 10}
|
||||
})
|
||||
|
||||
console.log(body.getPosition())
|
||||
// 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)
|
||||
|
||||
Reference in New Issue
Block a user