mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
prepared for box2d joint tests
This commit is contained in:
parent
b155faa63c
commit
5898f29167
4 changed files with 11017 additions and 0 deletions
10864
joint/box2d.js
Normal file
10864
joint/box2d.js
Normal file
File diff suppressed because it is too large
Load diff
55
joint/client.js
Normal file
55
joint/client.js
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
var id = null;
|
||||||
|
var lastIntervalTime = new Date().getTime();
|
||||||
|
|
||||||
|
function setupCanvas() {
|
||||||
|
var debugDraw = new b2DebugDraw();
|
||||||
|
|
||||||
|
debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
|
||||||
|
debugDraw.SetDrawScale(30.0);
|
||||||
|
debugDraw.SetFillAlpha(0.5);
|
||||||
|
debugDraw.SetLineThickness(1.0);
|
||||||
|
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
|
||||||
|
|
||||||
|
world.SetDebugDraw(debugDraw);
|
||||||
|
}
|
||||||
|
|
||||||
|
function init() {
|
||||||
|
setupWorld(9);
|
||||||
|
setupCanvas();
|
||||||
|
|
||||||
|
window.setInterval(update, 1000 / 60);
|
||||||
|
|
||||||
|
var body;
|
||||||
|
|
||||||
|
function update() {
|
||||||
|
var newTime = new Date().getTime()
|
||||||
|
lastIntervalTime = newTime;
|
||||||
|
world.Step(1 / 60, 10, 10);
|
||||||
|
world.DrawDebugData();
|
||||||
|
world.ClearForces();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//helpers
|
||||||
|
|
||||||
|
//http://js-tut.aardon.de/js-tut/tutorial/position.html
|
||||||
|
function getElementPosition(element) {
|
||||||
|
var elem = element, tagname = "", x = 0, y = 0;
|
||||||
|
|
||||||
|
while((typeof(elem) == "object") && (typeof(elem.tagName) != "undefined")) {
|
||||||
|
y += elem.offsetTop;
|
||||||
|
x += elem.offsetLeft;
|
||||||
|
|
||||||
|
tagname = elem.tagName.toUpperCase();
|
||||||
|
|
||||||
|
if(tagname == "BODY") elem = 0;
|
||||||
|
|
||||||
|
if(typeof(elem) == "object") {
|
||||||
|
if(typeof(elem.offsetParent) == "object") elem = elem.offsetParent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {x: x, y: y};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.onload = init;
|
||||||
84
joint/common.js
Normal file
84
joint/common.js
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
var bodiesNum = 3;
|
||||||
|
var world;
|
||||||
|
|
||||||
|
|
||||||
|
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,
|
||||||
|
b2ContactListener = Box2D.Dynamics.b2ContactListener;
|
||||||
|
|
||||||
|
function setupWorld(gravity) {
|
||||||
|
world = new b2World(new b2Vec2(0, gravity), true);
|
||||||
|
|
||||||
|
var fixDef = new b2FixtureDef;
|
||||||
|
fixDef.density = 1.0;
|
||||||
|
fixDef.friction = 0.99;
|
||||||
|
fixDef.restitution = .51;
|
||||||
|
|
||||||
|
var bodyDef = new b2BodyDef;
|
||||||
|
|
||||||
|
// create ground
|
||||||
|
bodyDef.type = b2Body.b2_staticBody;
|
||||||
|
fixDef.shape = new b2PolygonShape;
|
||||||
|
fixDef.shape.SetAsBox(20, 2);
|
||||||
|
|
||||||
|
bodyDef.position.Set(10, 400 / 30 + 1.8);
|
||||||
|
|
||||||
|
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||||
|
bodyDef.position.Set(10, -1.8);
|
||||||
|
|
||||||
|
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||||
|
fixDef.shape.SetAsBox(2, 14);
|
||||||
|
|
||||||
|
bodyDef.position.Set(-1.8, 13);
|
||||||
|
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||||
|
|
||||||
|
bodyDef.position.Set(21.8, 13);
|
||||||
|
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||||
|
|
||||||
|
// create some objects
|
||||||
|
bodyDef.type = b2Body.b2_dynamicBody;
|
||||||
|
|
||||||
|
for(var i = 0; i < bodiesNum; i++) {
|
||||||
|
fixDef.shape = new b2PolygonShape;
|
||||||
|
fixDef.shape.SetAsBox(0.4, 0.4);
|
||||||
|
|
||||||
|
bodyDef.position.x = ((i + 1) * 2) % 8;
|
||||||
|
bodyDef.position.y = 3;
|
||||||
|
|
||||||
|
bodyDef.userData = {
|
||||||
|
'bodyId': i + ''
|
||||||
|
};
|
||||||
|
|
||||||
|
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function jump() {
|
||||||
|
var body = findBody(1);
|
||||||
|
body.SetAwake(true);
|
||||||
|
body.ApplyImpulse(new b2Vec2(8, -15), body.GetPosition());
|
||||||
|
body.SetAngularVelocity(1.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
function findBody(index) {
|
||||||
|
var body = null;
|
||||||
|
|
||||||
|
var nextBody = world.GetBodyList();
|
||||||
|
for (var i = 0; i < bodiesNum; i++) {
|
||||||
|
if (nextBody.GetUserData().bodyId == index) { body = nextBody; break; }
|
||||||
|
nextBody = nextBody.GetNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
14
joint/index.html
Normal file
14
joint/index.html
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>join demo</title>
|
||||||
|
<script src="box2d.js"></script>
|
||||||
|
<script src="common.js"></script>
|
||||||
|
<script src="client.js"></script>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<canvas id="canvas" width="600" height="400" style="background-color:#333333;"></canvas>
|
||||||
|
<button onclick="jump()">jump</button>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue