Skip to content

Commit

Permalink
Add non-player bodies to world.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Sep 3, 2012
1 parent c0e5429 commit 9540863
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 36 deletions.
4 changes: 4 additions & 0 deletions body.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
this.yaw = Math.PI/4 * 5; this.yaw = Math.PI/4 * 5;
this.flying = false; this.flying = false;
this.noclip = false; this.noclip = false;
this.isPlayerBody = false;


// non-persisted properties // non-persisted properties
this.world = world; this.world = world;
Expand All @@ -43,6 +44,8 @@
json.flying = this.flying; json.flying = this.flying;
if (this.noclip !== false) if (this.noclip !== false)
json.noclip = this.noclip; json.noclip = this.noclip;
if (this.isPlayerBody !== false)
json.isPlayerBody = this.isPlayerBody;
return json; return json;
}; };


Expand All @@ -54,6 +57,7 @@
if ("yaw" in json) body.yaw = +json.yaw; if ("yaw" in json) body.yaw = +json.yaw;
if ("flying" in json) body.flying = !!json.flying; if ("flying" in json) body.flying = !!json.flying;
if ("noclip" in json) body.noclip = !!json.noclip; if ("noclip" in json) body.noclip = !!json.noclip;
if ("isPlayerBody" in json) body.isPlayerBody = !!json.isPlayerBody;
return body; return body;
}; };


Expand Down
5 changes: 4 additions & 1 deletion player.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
this.selection = null; this.selection = null;


// find or make body // find or make body
var body = world.playerBody; var body = world.getPlayerBody();
if (body) { if (body) {
this.bodyIsWorldly = true; this.bodyIsWorldly = true;
//console.log("Found existing body at " + vec3.str(body.pos)); //console.log("Found existing body at " + vec3.str(body.pos));
Expand Down Expand Up @@ -127,6 +127,9 @@
currentPlace.body.debugHitAABBs.forEach(function (aabb) { currentPlace.body.debugHitAABBs.forEach(function (aabb) {
draw(ZEROVEC, aabb, [0,1,0]); draw(ZEROVEC, aabb, [0,1,0]);
}); });
currentPlace.body.world.forEachBody(function (body) {
draw(body.pos, body.aabb, [1,0,0]);
});
} }
}); });


Expand Down
9 changes: 5 additions & 4 deletions test/test-world.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ describe("World", function() {
// TODO test that it dirties // TODO test that it dirties
}); });


it("should step the playerBody", function () { it("should step the bodies", function () {
var world = new World([1, 1, 1], new Blockset([])); var world = new World([1, 1, 1], new Blockset([]));
world.playerBody = new Body(world, Player.aabb); var body = new Body(world, Player.aabb);
world.addBody(body); // TODO proper add/remove interface
world.step(1/60); world.step(1/60);
expect(world.playerBody.vel[1]).toBeLessThan(0); expect(body.vel[1]).toBeLessThan(0);
expect(world.playerBody.pos[1]).toBeLessThan(0); expect(body.pos[1]).toBeLessThan(0);
}); });
}); });


Expand Down
12 changes: 11 additions & 1 deletion world-gen.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(function () { (function () {
"use strict"; "use strict";


var AAB = cubes.util.AAB;
var abs = Math.abs; var abs = Math.abs;
var Blockset = cubes.Blockset; var Blockset = cubes.Blockset;
var BlockType = cubes.BlockType; var BlockType = cubes.BlockType;
Expand Down Expand Up @@ -1117,10 +1118,19 @@
}()); }());


var playerBody = new Body(topWorld, Player.aabb, null); var playerBody = new Body(topWorld, Player.aabb, null);
playerBody.isPlayerBody = true;
playerBody.pos[0] = topWorld.wx/2; playerBody.pos[0] = topWorld.wx/2;
playerBody.pos[1] = topWorld.wy; playerBody.pos[1] = topWorld.wy;
playerBody.pos[2] = topWorld.wz/2; playerBody.pos[2] = topWorld.wz/2;
topWorld.playerBody = playerBody; topWorld.addBody(playerBody);

var testBodyAABB = new AAB(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
for (var tbi = 0; tbi < 10; tbi++) {
var body = new Body(topWorld, testBodyAABB);
vec3.set([wx/2, wy, wz/2], body.pos);
vec3.set([Math.random(), Math.random(), Math.random()], body.vel);
topWorld.addBody(body);
}


return topWorld; return topWorld;
} }
Expand Down
83 changes: 53 additions & 30 deletions world.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
// Blocks which a body is touching. Values are of the form {facevector: true}. // Blocks which a body is touching. Values are of the form {facevector: true}.
var contacts = new IntVectorMap(); var contacts = new IntVectorMap();


var playerBody = null; var bodies = [];


var numToDisturbPerSec = cubeCount * spontaneousBaseRate; var numToDisturbPerSec = cubeCount * spontaneousBaseRate;


Expand Down Expand Up @@ -539,10 +539,14 @@


evaluateLightsInQueue(); evaluateLightsInQueue();


if (playerBody) { var someBodyChanged = false;
playerBody.step(timestep, function () { function signal() { someBodyChanged = true; }
self.persistence.dirty(); for (var bi = bodies.length - 1; bi >= 0; bi--) {
}); var body = bodies[bi];
body.step(timestep, signal);
}
if (someBodyChanged) {
self.persistence.dirty();
} }
} }


Expand Down Expand Up @@ -711,6 +715,31 @@
notifier.notify("transientEvent", cube, gt(cube[0],cube[1],cube[2]), mode); notifier.notify("transientEvent", cube, gt(cube[0],cube[1],cube[2]), mode);
} }


function addBody(body) {
if (bodies.indexOf(body) !== -1) return;
if (body.world !== self && body.world !== null) {
throw new Error("the provided body already belongs to another world");
}
body.world = self;
bodies.push(body);
self.persistence.dirty();
}
function forEachBody(f) {
bodies.forEach(function (body) {
f(body);
});
}
function getPlayerBody() {
// TODO optimize?
var playerBody = null;
bodies.forEach(function (candidate) {
if (candidate.isPlayerBody && !playerBody) {
playerBody = candidate;
}
});
return playerBody;
}

var RLE_BASE = 0xA1; var RLE_BASE = 0xA1;
function rleBytes(bytes) { function rleBytes(bytes) {
var ser = []; var ser = [];
Expand Down Expand Up @@ -744,7 +773,7 @@
blocks: rleBytes(blocks), blocks: rleBytes(blocks),
subData: rleBytes(subData), subData: rleBytes(subData),
lightCache: rleBytes(lighting), lightCache: rleBytes(lighting),
playerBody: playerBody ? subSerialize(playerBody) : null bodies: bodies.map(subSerialize)
}; };
subSerialize.setUnserializer(json, World); subSerialize.setUnserializer(json, World);
return json; return json;
Expand Down Expand Up @@ -783,14 +812,18 @@
this.getCircuits = function () { return circuits; }; // TODO should be read-only interface this.getCircuits = function () { return circuits; }; // TODO should be read-only interface
this.getCircuit = function (block) { return blockCircuits.get(block) || null; }; this.getCircuit = function (block) { return blockCircuits.get(block) || null; };
this.edit = edit; this.edit = edit;


this.addBody = addBody;
this.forEachBody = forEachBody;
this.getPlayerBody = getPlayerBody;

this.step = step; this.step = step;
this.polishLightInVicinity = polishLightInVicinity; this.polishLightInVicinity = polishLightInVicinity;

this.setContacts = setContacts; this.setContacts = setContacts;
this.getContacts = getContacts; this.getContacts = getContacts;
this.transientEvent = transientEvent; this.transientEvent = transientEvent;

this.listen = notifier.listen; this.listen = notifier.listen;
this.serialize = serialize; this.serialize = serialize;


Expand All @@ -814,26 +847,6 @@
self.persistence.dirty(); self.persistence.dirty();
} }
} }
},
playerBody: {
enumerable: true,
get: function () {
return playerBody;
},
set: function (body) {
if (body === playerBody) return;
if (playerBody) {
playerBody.world = null;
}
if (body !== null) {
if (body.world !== self && body.world !== null) {
throw new Error("the provided body already belongs to another world");
}
body.world = self;
}
playerBody = body;
self.persistence.dirty();
}
} }
}); });


Expand All @@ -858,11 +871,21 @@
} }


var world = new World([json.wx, json.wy, json.wz], unserialize(json.blockset || json.blockSet, Blockset)); var world = new World([json.wx, json.wy, json.wz], unserialize(json.blockset || json.blockSet, Blockset));

unrleBytes(json.blocks, world.raw); unrleBytes(json.blocks, world.raw);
unrleBytes(json.subData, world.rawSubData); unrleBytes(json.subData, world.rawSubData);
unrleBytes(json.lightCache, world.rawLighting); unrleBytes(json.lightCache, world.rawLighting);
world.notifyRawEdit(); world.notifyRawEdit();
world.playerBody = json.playerBody ? unserialize(json.playerBody) : null;
(json.bodies || []).forEach(function (bodyJson) {
world.addBody(unserialize(bodyJson));
});
if (json.playerBody) { // obsolete serialization
var body = unserialize(json.playerBody);
body.isPlayerBody = true;
world.addBody(body);
}

return world; return world;
}; };


Expand Down

0 comments on commit 9540863

Please sign in to comment.