1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
| var canvas; var ctx; var canvasWidth; var canvasHeight;
var world; var box, wallLeft, wallRight, ground;
function createWorld() { var worldAABB = new b2AABB(); worldAABB.minVertex.Set(-4000, -4000); worldAABB.maxVertex.Set(4000, 4000);
var gravity = new b2Vec2(0, 300);
var doSleep = false;
var world = new b2World(worldAABB, gravity, doSleep);
return world; }
function drawWorld(world, context) { for (var j = world.m_jointList; j; j = j.m_next) { } for (var b = world.m_bodyList; b != null; b = b.m_next) { for (var s = b.GetShapeList(); s != null; s = s.GetNext()) { if (s.GetUserData() != undefined) { var img = s.GetUserData();
var x = s.GetPosition().x; var y = s.GetPosition().y; var topleftX = -img.clientWidth / 2; var topleftY = -img.clientHeight / 2;
context.save(); context.translate(x, y); context.rotate(s.GetBody().GetRotation()); context.drawImage(img, topleftX, topleftY); context.restore(); } drawShape(s, context); } } }
function createBall(world, x, y, r, custom) { var ballSd = new b2CircleDef(); ballSd.density = 1.0; if (custom === 'fixed') ballSd.density = 0.0; else ballSd.userData = custom; ballSd.radius = 20; ballSd.restitution = 1.0; ballSd.friction = 0; var ballBd = new b2BodyDef(); ballBd.AddShape(ballSd); ballBd.position.Set(x || 0, y || 0); return world.CreateBody(ballBd); }
function createBox(world, x, y, width, height, custom) { var boxSd = new b2BoxDef(); boxSd.extents.Set(width || 1200, height || 5); boxSd.density = 1.0; if (custom === 'fixed') boxSd.density = 0.0; else boxSd.userData = custom; boxSd.restitution = .3; boxSd.friction = 1;
var boxBd = new b2BodyDef(); boxBd.AddShape(boxSd); boxBd.position.Set(x || 10, y || 10); return world.CreateBody(boxBd) }
function step() { world.Step(1.0 / 60, 1); checkContact(); ctx.clearRect(0, 0, canvasWidth, canvasHeight); drawWorld(world, ctx);
setTimeout(step, 10); }
function drawShape(shape, context) { context.strokeStyle = '#003300'; context.beginPath(); switch (shape.m_type) { case b2Shape.e_circleShape: var circle = shape; var pos = circle.m_position; var r = circle.m_radius; var segments = 16.0; var theta = 0.0; var dtheta = 2.0 * Math.PI / segments; 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);
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: var poly = shape; var tV = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[0])); context.moveTo(tV.x, tV.y); for (var i = 0; i < poly.m_vertexCount; 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(tV.x, tV.y); break; } context.stroke(); }
function checkContact(){ for (var cn = world.GetContactList(); cn != null; cn = cn.GetNext()) { var body1 = cn.GetShape1().GetBody(); var body2 = cn.GetShape2().GetBody();
if(body1 === box && body2.IsStatic() == false ){ world.DestroyBody(body2); }
if(body2 === box && body1.IsStatic() == false ){ world.DestroyBody(body1); } } }
function GetBodyAtPosition(x, y) { var mousePVec = new b2Vec2(x, y); var aabb = new b2AABB(); aabb.minVertex.Set(mousePVec.x - 0.001, mousePVec.y - 0.001); aabb.maxVertex.Set(mousePVec.x + 0.001, mousePVec.y + 0.001);
var k_maxCount = 10; var shapes = new Array(); var count = world.Query(aabb, shapes, k_maxCount);
var findBody = null; for (var i = 0; i < count; ++i) { if (shapes[i].GetBody().IsStatic() == false) { var tShape = shapes[i]; var inside = tShape.GetBody(); if (inside) { findBody = tShape.GetBody(); break; } } } return findBody; }
function getMousePos(event) { var e = event || window.event; var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft; var scrollY = document.documentElement.scrollTop || document.body.scrollTop; var x = e.pageX || e.clientX + scrollX; var y = e.pageY || e.clientY + scrollY; return { 'x': x, 'y': y }; }
function handleMousedown(e) { var e = e || window.event; var newMouse = getMousePos(e); var selectBody = GetBodyAtPosition(newMouse.x - canvas.offsetLeft, newMouse.y - canvas.offsetTop); if (selectBody) { var LinearVelocity = new b2Vec2(500, -200); selectBody.WakeUp(); selectBody.SetLinearVelocity(LinearVelocity); } else { var width = parseInt(Math.random() * 50); var height = parseInt(Math.random() * 50); createBox(world, newMouse.x, newMouse.y, width, height); } }
document.addEventListener('mousedown', handleMousedown, false);
window.onload = function () { canvas = document.getElementById('canvas'); ctx = canvas.getContext('2d'); canvasWidth = parseInt(canvas.width); canvasHeight = parseInt(canvas.height); world = createWorld(); createBall(world, 100, 20, 20); createBall(world, 300, 60, 10); createBox(world, 100, 200, 25, 30, 'fixed'); createBox(world, 200, 50, 20, 20); box = createBox(world, 400, 80, 20, 20, document.getElementById('box')); wallLeft = createBox(world, 0, 0, 10, 600, 'fixed'); wallRight = createBox(world, 1290, 0, 10, 400, 'fixed'); ground = createBox(world, 30, 595, 1200, 5, 'fixed'); step(); };
|