Browse files

third commit

  • Loading branch information...
1 parent 3512173 commit afff6f70b99f9b74b0223c827cab553c12109ecc Your Name committed Oct 7, 2012
Showing with 5,084 additions and 2,550 deletions.
  1. +52 −23 { → sim}/simple.py
  2. BIN web/public/.client.js.swp
  3. +33 −27 web/public/client.js
  4. +4,999 −2,500 web/test.json
View
75 simple.py → sim/simple.py
@@ -1,5 +1,5 @@
import math
-
+from random import randrange
class Vec3:
def __init__(self,X=0.0,Y=0.0,Z=0.0):
@@ -36,19 +36,32 @@ def normalize(self):
-class Object:
+class SpaceObject:
def __init__(self):
self.pos = Vec3()
self.vel = Vec3()
self.acc = Vec3()
+
+ def update(self, dt):
+ self.vel += self.acc.scale(dt)
+ self.pos += self.vel.scale(dt)
+
+ def __repr__(self):
+ return '{ "SpaceObject":{ "pos":'+str(self.pos)+',"vel":'+str(self.vel)+',"acc":'+str(self.acc)+'}}'
+
+
+class Ship(SpaceObject):
+ def __init__(self):
+ SpaceObject.__init__(self)
+
+ self.id = 0
+ self.player = 'none'
self.look = Vec3()
self.maxAcc = 0.03
self.maxSpeed = 1.0
-
-
- def update(self, dt):
-
+
+ def update(self,dt):
if self.look.mag() != 0.0:
b = self.look.normalize()
a1 = b.scale(self.vel.dot(b))
@@ -58,37 +71,53 @@ def update(self, dt):
if self.vel.mag() >= self.maxSpeed:
self.vel = self.vel.scale( self.maxSpeed / self.vel.mag() )
- self.vel += self.acc.scale(dt)
- self.pos += self.vel.scale(dt)
-
+ SpaceObject.update(self,dt)
+
def __repr__(self):
- return '{ "pos":'+str(self.pos)+',"vel":'+str(self.vel)+',"acc":'+str(self.acc)+',"look":'+str(self.look)+'}'
+ return '{ "Ship":{ "id":'+str(self.id)+',"player":"'+self.player+'","Base":'+SpaceObject.__repr__(self)+'}}'
+
+class ShipSpawner:
+ def __init__(self, Player='none'):
+ self.player = Player
+ self.nextID = 0
+ self.pos = Vec3()
+ self.spawnRadius = 20
+
+ def spawn(self):
+ s = Ship()
+ s.player = self.player
+
+ s.id = self.nextID
+ self.nextID += 1
+ s.pos = self.pos + Vec3( randrange(self.spawnRadius),randrange(self.spawnRadius),randrange(self.spawnRadius))
+
+ return s
+
-o = Object()
-o.pos.set([200,0,0])
-o.vel.set([-1,1,-1])
-#o.acc.set([-0.1,0,0])
+base = ShipSpawner('patrick')
+base.pos.set([100,0,0])
+s1 = base.spawn()
+s2 = base.spawn()
simSteps = 2500
print '['
for i in range(simSteps):
- o.update(1)
-
- #d = ( - o.pos.normalize()).scale(0.1);
- #o.acc = d
+ s1.update(1)
+ s2.update(1)
- if i == 650:
- o.look.set([0,0,1])
+ if i == 950:
+ s1.look.set([0,0,1])
+ s2.look.set([-1,-1,-1])
+ print '{ "s1":'+str(s1)+',"s2":'+str(s2)+'}'
if i != (simSteps-1):
- print o,','
- else:
- print o
+ print ','
+
print ']'
View
BIN web/public/.client.js.swp
Binary file not shown.
View
60 web/public/client.js
@@ -18,6 +18,33 @@ var SimBox = {
Z : 512
};
+var ship1, ship2;
+function Ship ()
+{
+ this.geometry = new THREE.CylinderGeometry( 10, 0, 20 );
+ this.material = new THREE.MeshBasicMaterial( { color: 0x0000ff, wireframe: false } );
+ this.mesh = new THREE.Mesh( this.geometry, this.material );
+ scene.add( this.mesh );
+}
+Ship.prototype.update = function(simObject)
+{
+ simObject = simObject.Ship.Base.SpaceObject;
+ this.mesh.position.x = simObject.pos.x;
+ this.mesh.position.y = simObject.pos.y;
+ this.mesh.position.z = simObject.pos.z;
+
+ var vel = v(simObject.vel.x, simObject.vel.y, simObject.vel.z);
+ vel.normalize();
+
+ var xRot = Math.acos( vel.dot(v(1,0,0)));
+ var yRot = Math.acos( vel.dot(v(0,1,0)));
+ var zRot = Math.acos( vel.dot(v(0,0,1)));
+
+ this.mesh.rotation.x = -xRot;
+ this.mesh.rotation.y = yRot;
+ this.mesh.rotation.z = -zRot;
+}
+
var fps;
function init() {
@@ -29,11 +56,8 @@ function init() {
scene = new THREE.Scene();
- geometry = new THREE.CylinderGeometry( 10, 0, 20 );
- material = new THREE.MeshBasicMaterial( { color: 0x0000ff, wireframe: false } );
- mesh = new THREE.Mesh( geometry, material );
- console.log(mesh);
- scene.add( mesh );
+ ship1 = new Ship();
+ ship2 = new Ship();
addOriginLines();
addBoxOutline();
@@ -49,8 +73,7 @@ function init() {
fps.movementSpeed = 250;
fps.lookSpeed = 0.25;
fps.freeze = true;
- //fps.target = new THREE.Vector3(0,0,-1.0);
-
+ //fps.target = new THREE.Vector3(0,0,-1.0);
}
function animate() {
@@ -59,39 +82,22 @@ function animate() {
requestAnimationFrame( animate );
fps.update(clock.getDelta());
- mesh.position.x = sim[index].pos.x;
- mesh.position.y = sim[index].pos.y;
- mesh.position.z = sim[index].pos.z;
-
- var vel = v(sim[index].vel.x, sim[index].vel.y, sim[index].vel.z);
- vel.normalize();
+ ship1.update(sim[index]["s1"]);
+ ship2.update(sim[index]["s2"]);
- var xRot = Math.acos( vel.dot(v(1,0,0)));
- var yRot = Math.acos( vel.dot(v(0,1,0)));
- var zRot = Math.acos( vel.dot(v(0,0,1)));
-
- mesh.rotation.x = -xRot;
- mesh.rotation.y = yRot;
- mesh.rotation.z = -zRot;
-
- //mesh.rotation.y = Math.PI;
if(index < (sim.length-1))
index++;
- //mesh.rotation.x += 0.01;
- //mesh.rotation.y += 0.02;
renderer.render( scene, camera );
- //setTimeout(animate,1000);
}
socket.on('data', function (data) {
console.log(data);
sim = data;
init();
- setTimeout(animate,1000);
- //animate();
+ animate();
});
function v(x,y,z) {
View
7,499 web/test.json
4,999 additions, 2,500 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

0 comments on commit afff6f7

Please sign in to comment.