Files
cell/tests/box2d.js
John Alanbrook c014f29ce7
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
fill out box2d methods
2025-05-25 01:58:10 -05:00

91 lines
2.0 KiB
JavaScript

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)