Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 253 lines (225 sloc) 9.098 kb
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 246 247 248 249 250 251 252
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Box2DJS - Physics Engine for JavaScript</title>

<!--=============================-->
<!-- Copy this part to your app. -->
<!-- START -->
<!--=============================-->
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Droid+Sans" type="text/css">
    <script src="lib/prototype-1.6.0.2.js"></script>

<!-- box2djs -->
    <script src='js/box2d/common/b2Settings.js'></script>
    <script src='js/box2d/common/math/b2Vec2.js'></script>
    <script src='js/box2d/common/math/b2Mat22.js'></script>
    <script src='js/box2d/common/math/b2Math.js'></script>
    <script src='js/box2d/collision/b2AABB.js'></script>
    <script src='js/box2d/collision/b2Bound.js'></script>
    <script src='js/box2d/collision/b2BoundValues.js'></script>
    <script src='js/box2d/collision/b2Pair.js'></script>
    <script src='js/box2d/collision/b2PairCallback.js'></script>
    <script src='js/box2d/collision/b2BufferedPair.js'></script>
    <script src='js/box2d/collision/b2PairManager.js'></script>
    <script src='js/box2d/collision/b2BroadPhase.js'></script>
    <script src='js/box2d/collision/b2Collision.js'></script>
    <script src='js/box2d/collision/Features.js'></script>
    <script src='js/box2d/collision/b2ContactID.js'></script>
    <script src='js/box2d/collision/b2ContactPoint.js'></script>
    <script src='js/box2d/collision/b2Distance.js'></script>
    <script src='js/box2d/collision/b2Manifold.js'></script>
    <script src='js/box2d/collision/b2OBB.js'></script>
    <script src='js/box2d/collision/b2Proxy.js'></script>
    <script src='js/box2d/collision/ClipVertex.js'></script>
    <script src='js/box2d/collision/shapes/b2Shape.js'></script>
    <script src='js/box2d/collision/shapes/b2ShapeDef.js'></script>
    <script src='js/box2d/collision/shapes/b2BoxDef.js'></script>
    <script src='js/box2d/collision/shapes/b2CircleDef.js'></script>
    <script src='js/box2d/collision/shapes/b2CircleShape.js'></script>
    <script src='js/box2d/collision/shapes/b2MassData.js'></script>
    <script src='js/box2d/collision/shapes/b2PolyDef.js'></script>
    <script src='js/box2d/collision/shapes/b2PolyShape.js'></script>
    <script src='js/box2d/dynamics/b2Body.js'></script>
    <script src='js/box2d/dynamics/b2BodyDef.js'></script>
    <script src='js/box2d/dynamics/b2CollisionFilter.js'></script>
    <script src='js/box2d/dynamics/b2Island.js'></script>
    <script src='js/box2d/dynamics/b2TimeStep.js'></script>
    <script src='js/box2d/dynamics/contacts/b2ContactNode.js'></script>
    <script src='js/box2d/dynamics/contacts/b2Contact.js'></script>
    <script src='js/box2d/dynamics/contacts/b2ContactConstraint.js'></script>
    <script src='js/box2d/dynamics/contacts/b2ContactConstraintPoint.js'></script>
    <script src='js/box2d/dynamics/contacts/b2ContactRegister.js'></script>
    <script src='js/box2d/dynamics/contacts/b2ContactSolver.js'></script>
    <script src='js/box2d/dynamics/contacts/b2CircleContact.js'></script>
    <script src='js/box2d/dynamics/contacts/b2Conservative.js'></script>
    <script src='js/box2d/dynamics/contacts/b2NullContact.js'></script>
    <script src='js/box2d/dynamics/contacts/b2PolyAndCircleContact.js'></script>
    <script src='js/box2d/dynamics/contacts/b2PolyContact.js'></script>
    <script src='js/box2d/dynamics/b2ContactManager.js'></script>
    <script src='js/box2d/dynamics/b2World.js'></script>
    <script src='js/box2d/dynamics/b2WorldListener.js'></script>
    <script src='js/box2d/dynamics/joints/b2JointNode.js'></script>
    <script src='js/box2d/dynamics/joints/b2Joint.js'></script>
    <script src='js/box2d/dynamics/joints/b2JointDef.js'></script>
    <script src='js/box2d/dynamics/joints/b2DistanceJoint.js'></script>
    <script src='js/box2d/dynamics/joints/b2DistanceJointDef.js'></script>
    <script src='js/box2d/dynamics/joints/b2Jacobian.js'></script>
    <script src='js/box2d/dynamics/joints/b2GearJoint.js'></script>
    <script src='js/box2d/dynamics/joints/b2GearJointDef.js'></script>
    <script src='js/box2d/dynamics/joints/b2MouseJoint.js'></script>
    <script src='js/box2d/dynamics/joints/b2MouseJointDef.js'></script>
    <script src='js/box2d/dynamics/joints/b2PrismaticJoint.js'></script>
    <script src='js/box2d/dynamics/joints/b2PrismaticJointDef.js'></script>
    <script src='js/box2d/dynamics/joints/b2PulleyJoint.js'></script>
    <script src='js/box2d/dynamics/joints/b2PulleyJointDef.js'></script>
    <script src='js/box2d/dynamics/joints/b2RevoluteJoint.js'></script>
    <script src='js/box2d/dynamics/joints/b2RevoluteJointDef.js'></script>
<!--=============================-->
<!-- Copy this part to your app. -->
<!-- END -->
<!--=============================-->

    
    <script src='js/draw_world.js'></script>
    
  <script>
  // Create world
  var worldAABB = new b2AABB();
  worldAABB.minVertex.Set(-1000, -1000);
  worldAABB.maxVertex.Set(2000, 2000);
  var gravity = new b2Vec2(0, 300);
  var world = new b2World(worldAABB, gravity, true); // true is for allowSleep
  
  var throttle = 0;
  var MAX_SPEED = 300;
  var rotation = 0;
  var MAX_ROTATION = 0.5;
  var bike;
  
  function setupWorld() {
    // Create the motorbike
    var rearWheel = new b2CircleDef();
    rearWheel.density = 1.0;
    rearWheel.restitution = 0.2;
    rearWheel.friction = 0;
    rearWheel.localPosition.x = 0;
    rearWheel.localPosition.y = 0;
    rearWheel.radius = 20;
    
    var frontWheel = new b2CircleDef();
    frontWheel.density = 1.0;
    frontWheel.restitution = 0.2;
    frontWheel.friction = 0;
    frontWheel.localPosition.x = 80;
    frontWheel.localPosition.y = -10;
    frontWheel.radius = 30;
    
    var frame = new b2BoxDef()
    frame.density = 1.0;
    frame.restitution = 1.0;
    frame.friction = 0;
    frame.extents.x = 55;
    frame.extents.y = 8;
    frame.localRotation = -0.1 * Math.PI;
    frame.localPosition.x = 40;
    frame.localPosition.y = -30;
    
    var bikeBd = new b2BodyDef();
    bikeBd.AddShape(frontWheel);
    bikeBd.AddShape(rearWheel);
    bikeBd.AddShape(frame);
    bikeBd.position.Set(50, 300);
    
    bike = world.CreateBody(bikeBd);

var groundSd = new b2BoxDef();
groundSd.extents.Set(100, 10);
groundSd.restitution = 0.5;
groundSd.localRotation = -0.15 * Math.PI;
var groundBd = new b2BodyDef();
groundBd.AddShape(groundSd);
groundBd.position.Set(290, 485);
world.CreateBody(groundBd);

var groundSd1 = new b2BoxDef();
groundSd1.extents.Set(100, 10);
groundSd1.restitution = 0.5;
groundSd1.localRotation = 0.1 * Math.PI;
var groundBd1 = new b2BodyDef();
groundBd1.AddShape(groundSd1);
groundBd1.position.Set(100, 500);
world.CreateBody(groundBd1);

var groundSd2 = new b2BoxDef();
groundSd2.extents.Set(100, 10);
groundSd2.restitution = 0.5;
var groundBd2 = new b2BodyDef();
groundBd2.AddShape(groundSd2);
groundBd2.position.Set(800, 550);
world.CreateBody(groundBd2);
    
  }
  
  // Repeat
  var ctx;
  var canvasWidth;
  var canvasHeight;
  function step(cnt) {
    world.Step(1.0/60, 1);
    ctx.clearRect(0, 0, canvasWidth, canvasHeight);
    
    // Update the bike velocity
    var vel = bike.GetLinearVelocity();
    vel.x += throttle;
    if (vel.x > MAX_SPEED) { vel.x = MAX_SPEED; }
    if (vel.x < -MAX_SPEED) {vel.x = -MAX_SPEED; }
    bike.SetLinearVelocity(vel);
   
    // Update the bike rotation
    var rot = bike.GetAngularVelocity();
    rot += rotation;
    //if (rot > MAX_ROTATION) { vel.x = MAX_SPEED; }
    //if (rot < -MAX_SPEED) {vel.x = -MAX_SPEED; }
    bike.SetAngularVelocity(rot);
   
    drawWorld(world, ctx);
    setTimeout('step(' + (cnt || 0) + ')', 10);
  }
  
  
  Event.observe(window, 'load', function() {
    setupWorld(world);
    ctx = $('canvas').getContext('2d');
    var canvasElm = $('canvas');
    canvasWidth = parseInt(canvasElm.width);
    canvasHeight = parseInt(canvasElm.height);
    step();
  });
  
  
  var KEY_LEFT = 37;
  var KEY_UP = 38;
  var KEY_RIGHT = 39;
  var KEY_DOWN = 40;
  
  Event.observe(window, 'keydown', function(event) {
    switch(event.keyCode) {
      case KEY_RIGHT:
        throttle = 10;
        break;
      case KEY_LEFT:
        throttle = -10;
        break;
        
      case KEY_UP:
        rotation = 0.1;
        break;
      case KEY_DOWN:
        rotation = -0.1;
        break;
    }
  });
  
    Event.observe(window, 'keyup', function(event) {
    switch(event.keyCode) {
      case KEY_RIGHT:
      case KEY_LEFT:
        throttle = 0;
      break;
      case KEY_UP:
      case KEY_DOWN:
        rotation = 0;
        break;
    }
  });
  </script>
    
<link href="style/box2d.css" rel="stylesheet" type="text/css" />
  </head>
<body>
<hgroup>
<h1>Schlomoff-Dybskiy Motocross</h1>
<h2> by Gregory Schlomoff and Yuriy Dybskiy</h2>
</hgroup>
<canvas id="canvas" width='1000' height='600'></canvas>
  <footer>
    <h3>Buy new tires</h3>
    <img src="img/googlecheckout.png" />
  </footer>
</body>
</html>
Something went wrong with that request. Please try again.