/ Published in: JavaScript
Simulation of the collision of 4 balls using limejs and box2d.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//set main namespace goog.provide('collisionLime'); //get requirements goog.require('lime.Director'); goog.require('lime.Scene'); goog.require('lime.Layer'); goog.require('lime.Circle'); goog.require('box2d.World'); goog.require('box2d.Body'); goog.require('box2d.BodyDef'); goog.require('box2d.CircleDef'); goog.require('box2d.Vec2'); // entrypoint collisionLime.start = function(){ // colours var red = 'rgb(155, 0, 0)'; var green = 'rgb(0, 155, 0)'; var blue = 'rgb(0, 0, 155)'; var yellow = 'rgb(155, 155, 0)'; // director var director = new lime.Director(document.body, 600, 600); var scene = new lime.Scene(); var layer = new lime.Layer; // photoshop layer; append child // layer.setPosition(300, 300); scene.appendChild(layer); director.replaceScene(scene); // box2d var gravity = new box2d.Vec2(0, 0); // no gravity in x and y direction var bounds = new box2d.AABB(); bounds.minVertex.Set(0, 0); bounds.maxVertex.Set(600, 600); var world = new box2d.World(bounds, gravity, false); // create a circle function createCircle(radius, x, y, color, layer) { // limejs var circle = (new lime.Circle) .setFill(color) .setSize(radius * 2, radius * 2); layer.appendChild(circle); // box2d var bodyDef = new box2d.BodyDef; bodyDef.type = box2d.Body.b2_dynamicBody; // can move and rotate bodyDef.position.Set(x, y); bodyDef.awake = true; bodyDef.angularDamping = 0; // friction without contact - 0.001 bodyDef.userData = circle; var circleDef = new box2d.CircleDef; circleDef.radius = radius; circleDef.density = 5; // kg/m^2 circleDef.friction = 0.5; // 0 to 1, 0 means no friction circleDef.restitution = 0.5; // 0 to 1, 0 means won't bounce bodyDef.AddShape(circleDef); var body = world.CreateBody(bodyDef); return body; } function createRect(x, y, width, height, rotation, layer) { // limejs var rect = (new lime.Sprite) .setFill('#ccc') .setSize(width, height); layer.appendChild(rect); // box2d var bodyDef = new box2d.BodyDef; bodyDef.type = box2d.Body.b2_staticBody; bodyDef.position.Set(x, y); bodyDef.rotation = -rotation / 180 * Math.PI; bodyDef.userData = rect; var rectDef = new box2d.BoxDef; rectDef.density = 0; // static object, so not required rectDef.friction = 1; rectDef.restitution = 0.9; rectDef.extents.Set(width / 2, height / 2); bodyDef.AddShape(rectDef); var body = world.CreateBody(bodyDef); return body; } function autoMove(shape, vel_x, vel_y) { shape._body.SetLinearVelocity(new box2d.Vec2(vel_x, vel_y)); } function updateBody(body){ var rot = body.GetRotation(); var shape = body.GetUserData(); shape.setRotation(-rot / Math.PI * 180); shape.setPosition(body.GetCenterPosition().x, body.GetCenterPosition().y); } var circleP1 = createCircle(30, 50, 300, red, layer), circleP2 = createCircle(30, 300, 50, green, layer), circleP3 = createCircle(30, 550, 300, blue, layer), circleP4 = createCircle(30, 300, 550, yellow, layer), rectLeft = createRect(10, 300, 20, 600, 0, layer), rectTop = createRect(300, 10, 600, 20, 0, layer), rectRight = createRect(590, 300, 20, 600, 0, layer), rectBottom = createRect(300, 590, 600, 20, 0, layer); var vel_x = 50, vel_y = 50; circleP1.SetLinearVelocity(new box2d.Vec2(vel_x, 0)); circleP2.SetLinearVelocity(new box2d.Vec2(0, vel_y)); circleP3.SetLinearVelocity(new box2d.Vec2(-vel_x, 0)); circleP4.SetLinearVelocity(new box2d.Vec2(0 , -vel_y)); lime.scheduleManager.schedule(function(dt) { world.Step(dt / 60, 6, 2); // 60Hz, velocityIt, positionIt updateBody(circleP1); updateBody(circleP2); updateBody(circleP3); updateBody(circleP4); updateBody(rectLeft); updateBody(rectTop); updateBody(rectRight); updateBody(rectBottom); },this); } //this is required for outside access after code is compiled in ADVANCED_COMPILATIONS mode goog.exportSymbol('collisionLime.start', collisionLime.start);