From 95e0f6b71032d00770ea799a6940802f2371f1a0 Mon Sep 17 00:00:00 2001 From: Asraelite Date: Mon, 21 Mar 2016 20:44:15 +0000 Subject: [PATCH] add start of box2d physics --- package.json | 2 +- public/img/ships/01/hull.png | Bin 0 -> 390 bytes public/img/ships/01/teama.png | Bin 0 -> 148 bytes public/img/ships/01/teamb.png | Bin 0 -> 151 bytes public/img/ships/01/thrust0.png | Bin 0 -> 133 bytes public/img/ships/01/thrust1.png | Bin 0 -> 124 bytes public/img/ships/01/thrust2.png | Bin 0 -> 125 bytes public/img/ships/01/thrust3.png | Bin 0 -> 123 bytes public/img/ships/01/thrust4.png | Bin 0 -> 141 bytes public/img/ships/01/thrust5.png | Bin 0 -> 151 bytes public/img/ships/01/thrust6.png | Bin 0 -> 181 bytes public/img/ships/01/thrust7.png | Bin 0 -> 197 bytes public/img/ships/01/thrust8.png | Bin 0 -> 212 bytes public/img/ships/01/thrust9.png | Bin 0 -> 208 bytes public/img/turrets/01large.png | Bin 0 -> 229 bytes public/img/turrets/01medium.png | Bin 0 -> 194 bytes public/img/turrets/01small.png | Bin 0 -> 148 bytes server/game/room/world/body.js | 4 +- server/game/room/world/fixture.js | 13 ++++++ server/game/room/world/index.js | 7 ++- server/game/room/world/physics.js | 48 ++++++++++++++++++++ server/game/room/world/ship.js | 9 +++- server/game/room/world/structure.js | 0 server/game/room/world/traits/defaults.json | 8 ++++ server/game/room/world/traits/hulls.json | 12 +++++ 25 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 public/img/ships/01/hull.png create mode 100644 public/img/ships/01/teama.png create mode 100644 public/img/ships/01/teamb.png create mode 100644 public/img/ships/01/thrust0.png create mode 100644 public/img/ships/01/thrust1.png create mode 100644 public/img/ships/01/thrust2.png create mode 100644 public/img/ships/01/thrust3.png create mode 100644 public/img/ships/01/thrust4.png create mode 100644 public/img/ships/01/thrust5.png create mode 100644 public/img/ships/01/thrust6.png create mode 100644 public/img/ships/01/thrust7.png create mode 100644 public/img/ships/01/thrust8.png create mode 100644 public/img/ships/01/thrust9.png create mode 100644 public/img/turrets/01large.png create mode 100644 public/img/turrets/01medium.png create mode 100644 public/img/turrets/01small.png create mode 100644 server/game/room/world/fixture.js delete mode 100644 server/game/room/world/structure.js create mode 100644 server/game/room/world/traits/defaults.json create mode 100644 server/game/room/world/traits/hulls.json diff --git a/package.json b/package.json index 269e46c..a285cea 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "author": "Markus Scully ", "license": "ISC", "dependencies": { - "box2dweb": "^2.1.0-b", + "box2d-html5": "^0.1.230", "colors": "^1.1.2", "express": "^4.13.4", "recursive-readdir": "^1.3.0", diff --git a/public/img/ships/01/hull.png b/public/img/ships/01/hull.png new file mode 100644 index 0000000000000000000000000000000000000000..cbba37bf787f3c27ac04964b95860b1e6527f692 GIT binary patch literal 390 zcmeAS@N?(olHy`uVBq!ia0y~yU{C;I4mJh`hT^KKFANL}EX7WqAsieW95oy%9SjVN zww^AIArY-_!>sw54R}gd$u-Oj-mv*cTF30vm*+HR>}<52vf$608<{h1sW0?mw71`H z;m24fyUeh!>f=@p1(!4XHe6d}xs~b0leJ26UW|7a7A}a|+kA|5WrNDknNOHL?>qHh zgv;ZEcol2*>Y2iVLLM?4tIn%Fckzf>VX9bu&s3vmg`eZfDu#yEIM4hm0k0UF`fD9o zSI-RPwCQ^jR={+lA#+yO&272CodNTi)EvVFTDA!*9m@{w^YNLcoagj$OZLH&vK<2R zne=!3%INw~y+G>5VObZJ?6+4ZoNJ6;d%m-wSi4Dg1@FW{nf3SYJgz^icD-iFgZTA6+S9(dYc-t^v$M3 uC%5W2e3a^A-ND-TLi*0p-|q5z7*8x~8-a^p)c4lI3pTsiVE z)AlD*Ppj_mnyNGF=XEA+`|hfJ46GUsj11>XW0*L<9QeV&z`)??>gTe~DWM4f D8+b58 literal 0 HcmV?d00001 diff --git a/public/img/ships/01/thrust0.png b/public/img/ships/01/thrust0.png new file mode 100644 index 0000000000000000000000000000000000000000..7ade62bf18945667fc7dabd19001e4316ac61bd6 GIT binary patch literal 133 zcmeAS@N?(olHy`uVBq!ia0y~yU{C;I4mJh`hT^KKFANL}EX7WqAsieW95oy%9SjT% zHl8kyArY;~2@zopr0J9k;DgXcg literal 0 HcmV?d00001 diff --git a/public/img/ships/01/thrust3.png b/public/img/ships/01/thrust3.png new file mode 100644 index 0000000000000000000000000000000000000000..a504c30920849ecacb795821b3affbe73bf3aa9d GIT binary patch literal 123 zcmeAS@N?(olHy`uVBq!ia0y~yU{C;I4mJh`hT^KKFANL}EX7WqAsieW95oy%9SjT% zMxHK?ArY-_&lqwsDDW^Jxc2}5PQSdaro4N;iY+b-3=Dtt+*fo&@f2N~f9vnI*~JwL anZrt1Sh8$hM=>xkFnGH9xvXq5WYxZ5x7IP|pxxR;o0poKv=jaRm2)}IP+?(UU|`r0JEz%e z;?98VEuYrktGetp@7>PL<+oN(TRQFRdfR`mT5mIme-i!i$XRC#NVTV{pUXO@geCw; Ct28SB literal 0 HcmV?d00001 diff --git a/public/img/ships/01/thrust6.png b/public/img/ships/01/thrust6.png new file mode 100644 index 0000000000000000000000000000000000000000..3bfae914402c3c34b7c014bc47cf98f1a6219264 GIT binary patch literal 181 zcmeAS@N?(olHy`uVBq!ia0y~yU{C;I4mJh`hT^KKFANL}EX7WqAsieW95oy%9SjT% zIi4<#ArY-_&l~bNI0&>ptZ#N&c<0dCm}Kqrz@2T$o?*)sTrxa7R8&^%SwG7&E$)+p z3kW=Uf5xmQ)sorg8k0Z;zr&L`meqS+Ynk8S`(47Iuuhs`S84q#t?w7st?jDZuO9ht g#oy~YKXtMC?NWRlWxqv{fq{X+)78&qol`;+0BZ3Ajv%N*`gos{x?ScmyFxjT5zOQQbP8U#wXjz tXaDRCUjP5ky!l`7;iRe=)?XMI)_m5NEQ^+6W?*1o@O1TaS?83{1OPyvLz(@s3@fA!!uPg@Tw!2f OVDNPHb6Mw<&;$ULq*LSo literal 0 HcmV?d00001 diff --git a/public/img/ships/01/thrust9.png b/public/img/ships/01/thrust9.png new file mode 100644 index 0000000000000000000000000000000000000000..ed757673fbef50148151502d67371433680b26bc GIT binary patch literal 208 zcmeAS@N?(olHy`uVBq!ia0y~yU{C;I4mJh`hT^KKFANL}EX7WqAsieW95oy%9SjT% z?Vc`q}jWwIjEO$J}b#>3V zdv`B-ad&{hiKbl7KHkL}6>m@af9`D3^Dk@<7&pJS{_#}*qUXC^4W}C~Fh0BPIDNT( z!I|2#4cbXJx9xqStyEHYLGFC)-&OOAEU!EXP{03g-JKM9wqr5gp=>`37#J8BJYD@< J);T3K0RV|mQ;Yxr literal 0 HcmV?d00001 diff --git a/public/img/turrets/01large.png b/public/img/turrets/01large.png new file mode 100644 index 0000000000000000000000000000000000000000..eb77f5c92fbbffd1b1dabcbba6a1ed4ebaea6719 GIT binary patch literal 229 zcmeAS@N?(olHy`uVBq!ia0y~yV2}V|4mJh`h6m-gKNuJoSc;uILpV4%IBGajIv5xj z=6Jd|hIn+oopO-3L4n70|1n01R>55}6OG*xM9fyOA7o+Cw3U2pUHIVpcW1^QKW$DQ ziF;OS?|QCtYcbnWk0r6{JRy6IHFR=tpDR{WVtC;0nl!mgD7j_pwGRBY`4#Wmw^p&uNssg5SQxoJ^}yaf_a!c_XP?PtoZsy_*WqsM hGQW?r?uVRZ%s!&MancGg83qOh22WQ%mvv4FO#m6?RWtwq literal 0 HcmV?d00001 diff --git a/public/img/turrets/01medium.png b/public/img/turrets/01medium.png new file mode 100644 index 0000000000000000000000000000000000000000..547e2273fe81e5cf372148fdaaf41b494e28a194 GIT binary patch literal 194 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s7Sc;uILpV4%IBGajIv5xj zDm`5sLo9l)PCCeYK!Jz(@+xKnFSi8aBWXPcG;S)?e$X^A?AZ4zU#57Y!mqu22Eo27 zXBL(1wOC+2^PVvCxzdG-hoY4d(vqIlHVGV>!ob#XqO3uR#mh}M%>UKh*zDT3J&aPx wB@1SGY2LkOe4tixVu|dUEq1Ry`|n|VxL;yH_^+0k3=9kmp00i_>zopr0F16humAu6 literal 0 HcmV?d00001 diff --git a/public/img/turrets/01small.png b/public/img/turrets/01small.png new file mode 100644 index 0000000000000000000000000000000000000000..de89bcec1752248299f7a2dc256d7881517a3af1 GIT binary patch literal 148 zcmeAS@N?(olHy`uVBq!ia0y~yVBi2@4mJh`h9ms@x)~T4Sc;uILpV4%IBGajIv5xj zyggkULnNlQ_8sIsV8FrLuf=T8W!|$%QTn3$RgI9|N8%E{S!7a9&$;$^Ohf;dH*cIVGOlW(lVkR$Z(7K6F=U+&2)di(kr7#J8lUHx3vIVCg!0OL9= A(EtDd literal 0 HcmV?d00001 diff --git a/server/game/room/world/body.js b/server/game/room/world/body.js index e6405c3..a237b9c 100644 --- a/server/game/room/world/body.js +++ b/server/game/room/world/body.js @@ -2,7 +2,9 @@ class Body { constructor() { - + this.b2body = false; + this.type = 'dynamic'; + this.health = 1; } } diff --git a/server/game/room/world/fixture.js b/server/game/room/world/fixture.js new file mode 100644 index 0000000..313eeb7 --- /dev/null +++ b/server/game/room/world/fixture.js @@ -0,0 +1,13 @@ +'use strict'; + +const Body = require('./body.js'); + +class Fixture extends Body { + constructor(player) { + super(); + + this.type = 'static'; + } +} + +module.exports = Ship; diff --git a/server/game/room/world/index.js b/server/game/room/world/index.js index 7f0477d..d6bf7d6 100644 --- a/server/game/room/world/index.js +++ b/server/game/room/world/index.js @@ -1,10 +1,11 @@ 'use strict'; +const Physics = require('./physics.js'); const Ship = require('./ship.js'); class World { constructor() { - this.box2d = false; + this.physics = new Physics(); this.bodies = new Set(); this.structures = new Set(); this.asteroids = new Set(); @@ -14,7 +15,9 @@ class World { addPlayer(player) { this.players.add(player); - this.ships.set(player, new Ship(player)); + let ship = new Ship(player); + this.ships.set(player, ship); + this.physics.createBody(ship); } removePlayer(player) { diff --git a/server/game/room/world/physics.js b/server/game/room/world/physics.js index e69de29..7bcf64d 100644 --- a/server/game/room/world/physics.js +++ b/server/game/room/world/physics.js @@ -0,0 +1,48 @@ +'use strict'; + +// This file is very similar, but not identical to its client counterpart +// so most changes done to it should be mirrored there to keep consistent +// physics between client and server. + +const Box2D = require('box2d-html5'); + +const b2Vec2 = Box2D.b2Vec2; + +class Physics { + constructor() { + this.world = new Box2D.b2World(new b2Vec2(0, 0), false); + } + + createBody(body) { + let bodyDef = new Box2D.b2BodyDef(); + bodyDef.userData = body; + bodyDef.position = new b2Vec2(body.x || 0, body.y || 0); + bodyDef.fixedRotation = false; + bodyDef.active = true; + bodyDef.linearVelocity = new b2Vec2(body.xvel || 0, body.yvel || 0); + bodyDef.angularVelocity = body.rvel || 0; + bodyDef.type = body.type == 'static' ? + Box2D.b2Body.b2_staticBody : Box2D.b2Body.b2_dynamicBody; + let b2body = this.world.CreateBody(bodyDef); + + let fixtureDef = new Box2D.b2FixtureDef(); + fixtureDef.density = 10; + fixtureDef.friction = 1; + fixtureDef.restitution = 0; + + for (var poly of body.structure) { + poly.map(vertex => new b2Vec2(vertex[0], vertex[1])); + fixtureDef.shape = new Box2D.b2PolygonShape(); + fixtureDef.shape.SetAsArray(poly, poly.length); + b2body.CreateFixture(fixtureDef); + } + + body.b2body = b2body; + } + + step() { + this.world.Step(1, 5, 1 / 60); + } +} + +module.exports = Physics; diff --git a/server/game/room/world/ship.js b/server/game/room/world/ship.js index 5b18ec8..9ea679c 100644 --- a/server/game/room/world/ship.js +++ b/server/game/room/world/ship.js @@ -1,12 +1,17 @@ 'use strict'; +const defaults = require('./traits/defaults.json'); +const hulls = require('./traits/hulls.json'); + const Body = require('./body.js'); class Ship extends Body { - constructor(player) { + constructor(player, build) { super(); - + + this.build = build || defaults.spawnShip.build; this.player = player; + this.structure = hulls[this.build.hull]; } } diff --git a/server/game/room/world/structure.js b/server/game/room/world/structure.js deleted file mode 100644 index e69de29..0000000 diff --git a/server/game/room/world/traits/defaults.json b/server/game/room/world/traits/defaults.json new file mode 100644 index 0000000..00e3d96 --- /dev/null +++ b/server/game/room/world/traits/defaults.json @@ -0,0 +1,8 @@ +{ + "spawnShip": { + "build": { + "hull": "01", + "turrets": [0] + } + } +} diff --git a/server/game/room/world/traits/hulls.json b/server/game/room/world/traits/hulls.json new file mode 100644 index 0000000..8d254e5 --- /dev/null +++ b/server/game/room/world/traits/hulls.json @@ -0,0 +1,12 @@ +{ + "01": [ + [ + [1, 28], + [30, 28], + [30, 19], + [18, 2], + [13, 2], + [1, 19] + ] + ] +}