Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author J Barker & P Kirven
- *
- * This is the main entry point of the programme.
- *
- */
- var b2Vec2 = Box2D.Common.Math.b2Vec2,
- b2AABB = Box2D.Collision.b2AABB,
- b2BodyDef = Box2D.Dynamics.b2BodyDef,
- b2Body = Box2D.Dynamics.b2Body,
- b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
- b2Fixture = Box2D.Dynamics.b2Fixture,
- b2World = Box2D.Dynamics.b2World,
- b2MassData = Box2D.Collision.Shapes.b2MassData,
- b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
- b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
- b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
- b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef,
- b2Shape = Box2D.Collision.Shapes.b2Shape,
- b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef,
- b2Joint = Box2D.Dynamics.Joints.b2Joint,
- b2Settings = Box2D.Common.b2Settings;
- var world;
- var ctx;
- var canvas_width;
- var canvas_height;
- var scale = 100;
- $(function() {
- //first create the world
- world = createWorld();
- ctx = $('#canvas').get(0).getContext('2d');
- var canvas = $('#canvas');
- canvas_width = parseInt(canvas.width());
- canvas_height = parseInt(canvas.height());
- createBall(world, 5, 3.75);
- //testy(world);
- //draw_hammer_2(world);
- step();
- });
- function createWorld() {
- //Gravity vector x, y - 10 m/s2 - thats earth!!
- var gravity = new b2Vec2(0, 10);
- world = new b2World(gravity, true);
- createGround(world);
- return world;
- }
- function createGround(world) {
- var bodyDef = new b2BodyDef();
- fixDef = new b2FixtureDef;
- fixDef.density = 1.0;
- fixDef.friction = .005;
- fixDef.restitution = 0.5;
- fixDef.shape = new b2PolygonShape;
- fixDef.shape.SetAsBox(6.00, 1.50);
- bodyDef.position.Set(6, 6);
- /*var bodyDef_2 = new b2BodyDef();
- fixDef_2 = new b2FixtureDef;
- fixDef_2.density = 1.0;
- fixDef_2.friction = .005;
- fixDef_2.restitution = 0.5;
- fixDef_2.shape = new b2PolygonShape;
- fixDef_2.shape.SetAsBox(6.00 , 1.50);
- bodyDef_2.position.Set(6 , 6);
- var body_2 = world.CreateBody(bodyDef_2);*/
- var body = world.CreateBody(bodyDef);
- //var radians = 5 * Math.PI / 180;
- //body.SetAngle(radians);
- body.CreateFixture(fixDef);
- //body_2.CreateFixture(fixDef_2);
- }
- /*function createBox(world, x, y, width, height, options)
- {
- //default setting
- options = $.extend(true, {
- 'density' : 1.0 ,
- 'friction' : 1.0 ,
- 'restitution' : 0.5 ,
- 'linearDamping' : 0.0 ,
- 'angularDamping' : 0.0 ,
- 'type' : b2Body.b2_dynamicBody
- }, options);
- var body_def = new b2BodyDef();
- var fix_def = new b2FixtureDef;
- fix_def.density = options.density;
- fix_def.friction = options.friction;
- fix_def.restitution = options.restitution;
- fix_def.shape = new b2PolygonShape();
- fix_def.shape.SetAsBox( width , height );
- body_def.position.Set(x , y);
- body_def.linearDamping = options.linearDamping;
- body_def.angularDamping = options.angularDamping;
- body_def.type = options.type;
- var b = world.CreateBody( body_def );
- var f = b.CreateFixture(fix_def);
- return b;
- }
- */
- function createBall(world, x, y) {
- // first circle shape and def
- var fix_def1 = new b2FixtureDef;
- fix_def1.density = 1.0;
- fix_def1.friction = 0.5;
- fix_def1.restitution = .65;
- fix_def1.bullet = false;
- var shape1 = new b2CircleShape();
- fix_def1.shape = shape1;
- fix_def1.shape.SetLocalPosition(new b2Vec2(-.5, -.5));
- fix_def1.shape.SetRadius(.3);
- // second circle def and shape
- var fix_def2 = new b2FixtureDef;
- fix_def2.density = 1.0;
- fix_def2.friction = 0.5;
- fix_def2.restitution = .65;
- fix_def2.bullet = false;
- var shape2 = new b2CircleShape();
- fix_def2.shape = shape2;
- fix_def2.shape.SetLocalPosition(new b2Vec2(.5, .5));
- fix_def2.shape.SetRadius(.3);
- // creating the body
- var body_def = new b2BodyDef();
- body_def.type = b2Body.b2_dynamicBody;
- body_def.position.Set(5, 1);
- var b = world.CreateBody(body_def);
- b.CreateFixture(fix_def1);
- b.CreateFixture(fix_def2);
- return b;
- /*var fix_def = new b2FixtureDef;
- fix_def.density = 1.0;
- fix_def.friction = 0.5;
- fix_def.restitution = .65;
- fix_def.bullet = false;
- var shape = new b2CircleShape(0.2);
- fix_def.shape = shape;
- var fix_def2 = new b2FixtureDef;
- fix_def2.density = 1.0;
- fix_def2.friction = 0.5;
- fix_def2.restitution = .65;
- fix_def2.bullet = false;
- var shape2 = new b2CircleShape();
- alert(shape2.SetLocalPosition);
- //shape2.SetLocalPosition(new b2Vec2(-.1, -.1));
- fix_def2.shape = shape2;
- //alert(shape2.m_p.Set);
- var body_def = new b2BodyDef();
- body_def.position.Set(x , y);
- body_def.linearDamping = 0;
- body_def.angularDamping = 0;
- body_def.type = b2Body.b2_dynamicBody;
- var b = world.CreateBody( body_def );
- //b.CreateFixture(fix_def);
- b.CreateFixture(fix_def2);
- return b;*/
- }
- function step(cnt) {
- var stepping = false;
- //fps = 60 , time steps
- var fps = 100;
- var timeStep = 1.0 / fps;
- //move the world ahead , step ahead man!!
- world.Step(timeStep, 8, 3);
- world.ClearForces();
- //first clear the canvas
- ctx.clearRect(0, 0, canvas_width, canvas_height);
- //redraw the world
- draw_world(world, ctx);
- //call this function again after 10 seconds
- setTimeout('step(' + (cnt || 0) + ')', 10);
- }
- function draw_world(world, context) {
- //Draw the joints
- for (var j = world.GetJointList(); j; j = j.GetNext()) {
- drawJoint(j, context);
- }
- //Draw the bodies
- for (var b = world.GetBodyList(); b; b = b.GetNext()) {
- //A body has many fixtures
- for (var f = b.GetFixtureList(); f != null; f = f.GetNext()) {
- var shape = f.GetShape();
- var shapeType = shape.GetType();
- if (isNaN(b.GetPosition().x)) {
- alert('Invalid Position : ' + b.GetPosition().x);
- } else {
- drawShape(b, shape, context);
- }
- }
- }
- }
- function drawJoint(joint, context) {
- var b1 = joint.GetBodyA();
- var b2 = joint.GetBodyB();
- var x1 = b1.GetPosition();
- var x2 = b2.GetPosition();
- var p1 = joint.GetAnchorA();
- var p2 = joint.GetAnchorB();
- context.strokeStyle = '#00eeee';
- context.beginPath();
- switch (joint.m_type) {
- case b2Joint.e_distanceJoint:
- context.moveTo(p1.x, p1.y);
- context.lineTo(p2.x, p2.y);
- break;
- case b2Joint.e_pulleyJoint:
- // TODO
- break;
- default:
- if (b1 == world.m_groundBody) {
- context.moveTo(p1.x, p1.y);
- context.lineTo(x2.x, x2.y);
- } else if (b2 == world.m_groundBody) {
- context.moveTo(p1.x, p1.y);
- context.lineTo(x1.x, x1.y);
- } else {
- context.moveTo(x1.x, x1.y);
- context.lineTo(p1.x, p1.y);
- context.lineTo(x2.x, x2.y);
- context.lineTo(p2.x, p2.y);
- }
- break;
- }
- context.stroke();
- }
- function drawShape(body, shape, context) {
- context.strokeStyle = '#000';
- if (shape.density == 1.0) {
- context.fillStyle = "red";
- } else {
- context.fillStyle = "#fff";
- }
- context.beginPath();
- switch (shape.GetType()) {
- case b2Shape.e_circleShape:
- {
- var circle = shape;
- var pos = body.GetPosition();
- var r = shape.GetRadius();
- var segments = 16.0;
- var theta = 0.0;
- var dtheta = 2.0 * Math.PI / segments;
- // draw circle
- context.moveTo((pos.x + r) * scale, pos.y * scale);
- for (var i = 0; i < segments; i++) {
- var d = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));
- var v = pos.Copy();
- v.Add(d);
- context.lineTo(v.x * scale, v.y * scale);
- theta += dtheta;
- }
- context.lineTo((pos.x + r) * scale, pos.y * scale);
- // draw radius line
- context.moveTo(pos.x * scale, pos.y * scale);
- var ax = body.GetTransform().R.col1;
- var pos2 = new b2Vec2(pos.x + r * ax.x, pos.y + r * ax.y);
- context.lineTo(pos2.x * scale, pos2.y * scale);
- }
- break;
- case b2Shape.e_polygonShape:
- {
- var poly = shape;
- var vert = shape.GetVertices();
- var position = body.GetPosition();
- var tV = position.Copy();
- var a = vert[0].Copy();
- a.MulM(body.GetTransform().R);
- tV.Add(a);
- context.moveTo(tV.x * scale, tV.y * scale);
- for (var i = 0; i < vert.length; i++) {
- var v = vert[i].Copy();
- v.MulM(body.GetTransform().R);
- v.Add(position);
- context.lineTo(v.x * scale, v.y * scale);
- }
- context.lineTo(tV.x * scale, tV.y * scale);
- }
- break;
- }
- //this will fill a shape
- context.fill();
- //this will create the outline of a shape
- context.stroke();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement