Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Adjusted by http://www.box2d.org/manual.html#_Toc258082976
- // http://www.box2dflash.org/docs/2.1a/reference/
- // http://blog.sethladd.com/2011/09/box2d-javascript-example-walkthrough.html
- // http://code.google.com/p/box2dweb/source/browse/trunk/example.html
- // http://code.google.com/p/jquery-box2dweb/source/browse/trunk/assets/?r=29
- //http://stackoverflow.com/questions/7628078/which-box2d-javascript-library-should-i-use
- // Box2d vars
- var b2AABB = Box2D.Collision.b2AABB;
- var b2Vec2 = Box2D.Common.Math.b2Vec2;
- var b2World = Box2D.Dynamics.b2World;
- //var b2BoxDef = Box2D.Collision.Shapes.b2EdgeChainDef;
- var b2BodyDef = Box2D.Dynamics.b2BodyDef;
- var b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
- var b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
- var b2BodyDef = Box2D.Dynamics.b2BodyDef;
- var b2Body = Box2D.Dynamics.b2Body;
- var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;
- var b2Shape = Box2D.Collision.Shapes.b2Shape;
- var b2Math = Box2D.Common.Math.b2Math;
- var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
- function drawWorld(world, context) {
- for (var j = world.m_jointList; j; j = j.m_next) {
- drawJoint(j, context);
- }
- for (var b = world.m_bodyList; b; b = b.m_next) {
- // console.log(b);
- //for (var s = b.GetShapeList(); s != null; s = s.GetNext()) {
- for (var s = b.GetFixtureList(); s != null; s = s.GetNext()) {
- console.log('shape:');
- var shape = s.GetShape();
- console.log(shape);
- var shapeType = shape.GetType();
- //console.log(shapeType);
- //console.log(b2Shape.e_polygonShape);
- //drawShape(s, context);
- drawShape(shape, context);
- }
- }
- }
- function drawJoint(joint, context) {
- var b1 = joint.m_body1;
- var b2 = joint.m_body2;
- var x1 = b1.m_position;
- var x2 = b2.m_position;
- var p1 = joint.GetAnchor1();
- var p2 = joint.GetAnchor2();
- 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(shape, context) {
- // console.log(shape);
- context.strokeStyle = '#000000';
- context.beginPath();
- //console.log('type:');
- // console.log(shape.GetType()); //0 or 1
- //switch (shape.m_type) {
- switch (shape.GetType()) {
- case b2Shape.e_circleShape:
- {
- return;
- alert('circle');
- var circle = shape;
- //var pos = circle.m_position;
- var pos = circle.GetLocalPosition();
- var r = circle.m_radius;
- var segments = 16.0;
- var theta = 0.0;
- var dtheta = 2.0 * Math.PI / segments;
- // draw circle
- context.moveTo(pos.x + r, pos.y);
- for (var i = 0; i < segments; i++) {
- var d = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));
- var v = b2Math.AddVV(pos, d);
- context.lineTo(v.x, v.y);
- theta += dtheta;
- }
- context.lineTo(pos.x + r, pos.y);
- // draw radius
- context.moveTo(pos.x, pos.y);
- var ax = circle.m_R.col1;
- var pos2 = new b2Vec2(pos.x + r * ax.x, pos.y + r * ax.y);
- context.lineTo(pos2.x, pos2.y);
- }
- break;
- //case b2Shape.e_polyShape:
- case b2Shape.e_polygonShape:
- {
- //console.log(b2Shape.e_polyShape + 'te');
- //createBox(world, 3, 230, 60, 180, true, 'ground');
- var poly = shape;
- console.log('poly');
- console.log(poly);
- // alert(poly.GetVertices());
- //var tV = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[0]));
- // var vert = poly.GetVertices();
- // var tV = b2Math.AddVV(poly.m_position, b2Math.MulMV(poly.m_R, vert[0]));
- //context.moveTo(tV.x, tV.y);
- context.moveTo(poly.m_vertices[0].x, y_to_canvas(poly.m_vertices[0].y));
- //for (var i = 0; i < poly.m_vertexCount; i++) {
- for (var i = 0; i < poly.GetVertexCount(); i++) {
- //var v = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[i]));
- //context.lineTo(v.x, v.y);
- // context.lineTo(poly.m_vertices[i].x, y_to_canvas(poly.m_vertices[i].y));
- context.lineTo(poly.m_vertices[i].x + 50, y_to_canvas(poly.m_vertices[i].y));
- } ;
- //context.lineTo(tV.x, tV.y);
- context.lineTo(poly.m_vertices[0].x, y_to_canvas(poly.m_vertices[0].y));
- }
- break;
- default:
- {
- //alert('default');
- }
- }
- context.stroke();
- }
- // Canvas Y coordinates start at opposite location, so we flip
- function y_to_canvas(y) {
- return canvasHeight - y;
- }
- function createWorld() {
- var worldAABB = new b2AABB();
- //worldAABB.minVertex.Set(-1000, -1000);
- worldAABB.lowerBound.Set(-1000, -1000);
- //worldAABB.maxVertex.Set(1000, 1000);
- worldAABB.upperBound.Set(1000, 1000);
- var gravity = new b2Vec2(0, 300);
- var doSleep = true;
- var world = new b2World(worldAABB, gravity, doSleep);
- return world;
- }
- function createGround(world) {
- var groundSd = new b2BoxDef();
- groundSd.extents.Set(1000, 50);
- groundSd.restitution = 0.2;
- var groundBd = new b2BodyDef();
- groundBd.AddShape(groundSd);
- groundBd.position.Set(-500, 340);
- return world.CreateBody(groundBd)
- }
- function createBall(world, x, y) { //not used
- var ballSd = new b2CircleDef();
- ballSd.density = 1.0;
- ballSd.radius = 20;
- ballSd.restitution = 1.0;
- ballSd.friction = 0;
- var ballBd = new b2BodyDef();
- ballBd.AddShape(ballSd);
- ballBd.position.Set(x,y);
- return world.CreateBody(ballBd);
- }
- function createBox(world, x, y, width, height, fixed, userData) {
- if (typeof(fixed) == 'undefined') fixed = true;
- //var boxSd = new b2BoxDef();
- //console.log(boxSd);
- /*
- if (!fixed) boxSd.density = 1.0;
- boxSd.userData = userData;
- boxSd.extents.Set(width, height);
- */
- /* is kit psl **/
- var bodyDef = new b2BodyDef;
- // bodyDef.type = b2Body.b2_dynamicBody;
- bodyDef.type = b2Body.b2_staticBody;
- /*
- bodyDef.position.x = x ;
- bodyDef.position.y = y;
- */
- bodyDef.position.Set(x / 2 , y/ 2);
- var fixDef = new b2FixtureDef;
- fixDef.shape = new b2PolygonShape;
- fixDef.shape.SetAsBox(
- width / 2 //half width
- , height / 2 //half height
- );
- console.log('bodyDef');
- console.log(bodyDef);
- var body = world.CreateBody(bodyDef);
- console.log(body);
- box = body.CreateFixture(fixDef);
- console.log('box');
- console.log(box);
- return box;
- //return world.CreateBody(bodyDef).CreateFixture(fixDef);
- //return world.CreateBody(bodyDef);
- /* end mano */
- /*
- var boxBd = new b2BodyDef();
- boxBd.AddShape(boxSd);
- boxBd.position.Set(x,y);
- return world.CreateBody(boxBd)
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement