Permalink
Browse files

Add autonomous behavior to bodies.

  • Loading branch information...
1 parent 5a26a35 commit 671a5f2a93c272455356dd88acc7eadb856876a1 @kpreid committed Jun 20, 2012
Showing with 18 additions and 2 deletions.
  1. +14 −0 body.js
  2. +4 −2 world-gen.js
View
14 body.js
@@ -25,6 +25,7 @@
this.flying = false;
this.noclip = false;
this.isPlayerBody = false;
+ this.autonomous = false; // TODO to be replaced with a component system or something
this.skin = skin || null;
// non-persisted properties
@@ -47,6 +48,8 @@
json.noclip = this.noclip;
if (this.isPlayerBody !== false)
json.isPlayerBody = this.isPlayerBody;
+ if (this.autonomous !== false)
+ json.autonomous = this.autonomous;
if (this.skin !== null)
json.skin = subSerialize(this.skin);
return json;
@@ -61,6 +64,7 @@
if ("flying" in json) body.flying = !!json.flying;
if ("noclip" in json) body.noclip = !!json.noclip;
if ("isPlayerBody" in json) body.isPlayerBody = !!json.isPlayerBody;
+ if ("autonomous" in json) body.autonomous = !!json.autonomous;
if ("skin" in json) body.skin = unserialize(json.skin);
return body;
};
@@ -81,6 +85,16 @@
var curPos = this.pos;
var curVel = this.vel;
+ // autonomous control
+ if (this.autonomous) {
+ this.flying = true;
+ var home = vec3.createFrom(world.wx/2, world.wy, world.wz/2);
+ vec3.subtract(home, curPos);
+ vec3.scale(home, timestep * 0.1);
+ vec3.add(curVel, home);
+ this.yaw = Math.atan2(curVel[0], curVel[2]);
+ }
+
// gravity
if (!this.flying) {
curVel[1] -= timestep * GRAVITY;
View
6 world-gen.js
@@ -1143,8 +1143,10 @@
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, skin);
- vec3.set([wx/2, wy, wz/2], body.pos);
- vec3.set([Math.random(), Math.random(), Math.random()], body.vel);
+ vec3.set([random()*topWorld.wx, (random()+0.5)*topWorld.wy, random()*topWorld.wz], body.pos);
+ vec3.set([(random() - 0.5) * 9, (random() - 0.5) * 9, (random() - 0.5) * 9], body.vel);
+ body.flying = Math.random() > 0.5;
+ body.autonomous = true;
topWorld.addBody(body);
}

0 comments on commit 671a5f2

Please sign in to comment.