Created
May 11, 2012 14:44
-
-
Save incompl/2660196 to your computer and use it in GitHub Desktop.
Example of box2dweb with C++ heritage noted in comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code from http://lib.ivank.net/?p=demos&d=box2D | |
// These look like imports, but in JavaScript they are just busy-work. | |
var | |
b2Vec2 = Box2D.Common.Math.b2Vec2, | |
b2BodyDef = Box2D.Dynamics.b2BodyDef, | |
b2Body = Box2D.Dynamics.b2Body, | |
b2FixtureDef = Box2D.Dynamics.b2FixtureDef, | |
b2World = Box2D.Dynamics.b2World, | |
b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape; | |
// Constructor functions should use ProperCase in JavaScript. | |
world = new b2World(new b2Vec2(0, 10), true); | |
// Someone without a programming background may not be able to guess | |
// what this abbreviation means. | |
up = new b2Vec2(0, -7); | |
// Here we're creating a fixture, then a body, then a shape. For | |
// simple learning exercises these distinctions aren't useful. | |
var fixDef = new b2FixtureDef(); | |
fixDef.density = 1.0; | |
var bodyDef = new b2BodyDef(); | |
bodyDef.type = b2Body.b2_staticBody; // mixed camelCase and _s | |
fixDef.shape = new b2PolygonShape(); | |
// Here ProperCase is used for methods, which should be reserved | |
// for constructors in JavaScript. | |
bodyDef.position.Set(9, stage.stageHeight/100 + 1); | |
fixDef.shape.SetAsBox(10, 1); | |
world.CreateBody(bodyDef).CreateFixture(fixDef); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var canvas = document.getElementById("game"); | |
var world = boxbox.createWorld(canvas); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var player = world.createEntity(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ground = world.createEntity({ | |
color: "green", | |
x: 13, | |
y: 18, | |
width: 12, | |
height: 0.5, | |
type: "static" | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
player.onKeydown(function(e) { | |
this.applyImpulse(100, 45); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reusable template | |
var targetTemplate = { | |
color: "blue", | |
height: 1.5, | |
width: 0.25 | |
}; | |
// Bottom level | |
world.createEntity(targetTemplate, {x: 18, y: 15}); | |
world.createEntity(targetTemplate, {x: 21, y: 15}); | |
world.createEntity(targetTemplate, {x: 24, y: 15}); | |
// Bottom horizontal platform | |
world.createEntity({ | |
color: "purple", | |
width: 3, | |
height: 0.25, | |
x: 21, | |
y: 13 | |
}); | |
// Middle | |
world.createEntity(targetTemplate, {x: 23, y: 11}); | |
world.createEntity(targetTemplate, {x: 19, y: 11}); | |
// Top horizontal platform | |
world.createEntity({ | |
color: "purple", | |
width: 2, | |
height: 0.25, | |
x: 21, | |
y: 9 | |
}); | |
// Top level | |
world.createEntity(targetTemplate, {x: 21, y: 7}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment