reduce physics cpu usage

This commit is contained in:
Asraelite 2016-03-29 22:35:08 +01:00
parent 8a059b92d5
commit 8ea6c2d937
10 changed files with 2435 additions and 34 deletions

2364
processed.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -23,6 +23,7 @@ class Net {
this.socket.on('world', data => { this.socket.on('world', data => {
game.world.clear(); game.world.clear();
console.log(data);
game.world.bounds = data.bounds; game.world.bounds = data.bounds;
for (var b of data.bodies) { for (var b of data.bodies) {
game.world.add(b); game.world.add(b);

View file

@ -77,8 +77,8 @@ class Renderer {
this.pallet.opacity(0.05); this.pallet.opacity(0.05);
for (var x = gridx - cw / 2 - 50; x < cw + 50; x += 50) { for (var x = gridx - cw / 2 - 50; x < cw + 50; x += 50) {
for (var y = gridy - ch / 2 - 50; y < ch + 50; y += 50) { for (var y = gridy - ch / 2 - 50; y < ch + 50; y += 50) {
var wx = (-cx + x) / SCALE; var wx = ((-cx + x) / SCALE) | 0;
var wy = (-cy + y) / SCALE; var wy = ((-cy + y) / SCALE) | 0;
var b = game.world.bounds; var b = game.world.bounds;
if (wx > b.right || wx < b.left || wy > b.bottom || wy < b.top) { if (wx > b.right || wx < b.left || wy > b.bottom || wy < b.top) {
this.pallet.opacity(0.2); this.pallet.opacity(0.2);

View file

@ -83,6 +83,9 @@
"Tamarillo", "Tamarillo",
"Tamarind", "Tamarind",
"Ugli Fruit", "Ugli Fruit",
"Umbu",
"Ximenia",
"Xylocarp",
"Yuzu", "Yuzu",
"Ziziphus" "Ziziphus"
], ],
@ -90,7 +93,9 @@
"a": [ "a": [
"Awesome", "Awesome",
"Amazing", "Amazing",
"Awkward" "Awkward",
"Absurd",
"Allergic"
], ],
"b": [ "b": [
"Bubbly", "Bubbly",
@ -107,7 +112,8 @@
], ],
"d": [ "d": [
"Dizzy", "Dizzy",
"Dangerous" "Dangerous",
"Dazzling"
], ],
"e": [ "e": [
"Energetic", "Energetic",
@ -116,7 +122,10 @@
], ],
"f": [ "f": [
"Funny", "Funny",
"Fancy" "Fancy",
"Fat",
"Filthy",
"Fizzy"
], ],
"g": [ "g": [
"Giggly", "Giggly",
@ -134,15 +143,21 @@
"Introverted" "Introverted"
], ],
"j": [ "j": [
"Jolly" "Jolly",
"Jealous",
"Joyful",
"Jittery"
], ],
"k": [ "k": [
"Crazy", "Keen",
"Cool" "Kind",
"Kingly"
], ],
"l": [ "l": [
"Little", "Little",
"Lazy" "Lazy",
"Lovely",
"Lax"
], ],
"m": [ "m": [
"Magnificent", "Magnificent",
@ -151,13 +166,16 @@
], ],
"n": [ "n": [
"Nosy", "Nosy",
"Noisy" "Noisy",
"Nasty",
"Naive"
], ],
"o": [ "o": [
"Oblivious", "Oblivious",
"Omnipotent", "Omnipotent",
"Omnipresent", "Omnipresent",
"Okay" "Okay",
"Obvious"
], ],
"p": [ "p": [
"Pretty", "Pretty",
@ -175,7 +193,10 @@
], ],
"r": [ "r": [
"Radical", "Radical",
"Racist" "Racist",
"Red",
"Rare",
"Risky"
], ],
"s": [ "s": [
"Stupid", "Stupid",
@ -190,7 +211,8 @@
], ],
"u": [ "u": [
"Unbelievable", "Unbelievable",
"Ugly" "Ugly",
"Ultimate"
], ],
"v": [ "v": [
"Voluptuous", "Voluptuous",
@ -201,11 +223,13 @@
"Weird" "Weird"
], ],
"x": [ "x": [
"Xanthous",
"Xenophobic" "Xenophobic"
], ],
"y": [ "y": [
"Yellow", "Yellow",
"Useful" "Yucky",
"Yummy"
], ],
"z": [ "z": [
"Zany", "Zany",

View file

@ -49,7 +49,7 @@ class Body {
} }
applyDelta() { applyDelta() {
this.world.applyDelta(this.packDelta()); this.world.applyDelta(this.packDelta(), this.pos);
} }
applyForce(x, y, center) { applyForce(x, y, center) {

View file

@ -8,8 +8,10 @@ class Grapple extends Projectile {
super(world, pos); super(world, pos);
this.r = pos.r; this.r = pos.r;
this.x = pos.x * 32; this.x = pos.x;
this.y = pos.y * 32; this.y = pos.y;
this.xvel = pos.xvel;
this.yvel = pos.yvel;
this.xvel += Math.cos(this.r) * 0.25; this.xvel += Math.cos(this.r) * 0.25;
this.yvel += Math.sin(this.r) * 0.25; this.yvel += Math.sin(this.r) * 0.25;

View file

@ -6,8 +6,8 @@ class Missile extends Projectile {
constructor(world, pos, source) { constructor(world, pos, source) {
super(world); super(world);
this.x = pos.x * 32; this.x = pos.x;
this.y = pos.y * 32; this.y = pos.y;
this.xvel = pos.xvel; this.xvel = pos.xvel;
this.yvel = pos.yvel; this.yvel = pos.yvel;
this.r = pos.r; this.r = pos.r;

View file

@ -15,6 +15,7 @@ class Ship extends Body {
// Body data. // Body data.
this.x = pos.x || 0; this.x = pos.x || 0;
this.y = pos.y || 0; this.y = pos.y || 0;
this.r = player.team == 'b' ? Math.PI : 0;
this.type = 'ship'; this.type = 'ship';
this.class = build.ship; this.class = build.ship;
@ -64,6 +65,7 @@ class Ship extends Body {
if (this.grapple) { if (this.grapple) {
this.grapple.retract(); this.grapple.retract();
} else { } else {
let s = this.world.scale;
this.launchGrapple(this.inputs.mx, this.inputs.my); this.launchGrapple(this.inputs.mx, this.inputs.my);
} }
} }

View file

@ -22,21 +22,23 @@ class World {
this.tpsCount = 0; this.tpsCount = 0;
this.tpsStart = Date.now(); this.tpsStart = Date.now();
this.scale = 32;
this.tickCount = 0; this.tickCount = 0;
this.bounds = { this.bounds = {
left: -5, left: 0,
right: 50, right: 250,
top: -15, top: 0,
bottom: 15 bottom: 30
} }
} }
addPlayer(player) { addPlayer(player) {
this.players.add(player); this.players.add(player);
let pos = { let pos = {
x: player.team == 'b' ? 200 : 0, x: player.team == 'b' ? this.bounds.right - 5 : 5,
y: 0 y: this.bounds.bottom / 2
}; };
let ship = new Ship(this, pos, player); let ship = new Ship(this, pos, player);
player.ship = ship; player.ship = ship;
@ -86,9 +88,14 @@ class World {
this.room.broadcast('effect', copula.packFull()); this.room.broadcast('effect', copula.packFull());
} }
applyDelta(data) { applyDelta(data, bodyPos) {
data = data.map(v => +(v.toFixed(3))); data = data.map(v => +(v.toFixed(3)));
this.players.forEach(player => player.applyDelta(data)); this.players.forEach(player => {
let dx = player.ship.pos.x - bodyPos.x;
let dy = player.ship.pos.y - bodyPos.y;
if (dx * dx + dy * dy < 900)
player.applyDelta(data)
});
} }
explosion(pos, power) { explosion(pos, power) {
@ -110,12 +117,12 @@ class World {
} }
populate() { populate() {
for (var i = 0; i < 5; i++) { for (var i = 0; i < 50; i++) {
let pos = { let pos = {
x: Math.random() * 2000 - 200, x: Math.random() * this.bounds.right,
y: Math.random() * 500 - 250 y: Math.random() * this.bounds.bottom
}; };
this.spawner.spawnAsteroid(pos.x, pos.y,Math.random() * 50 + 10); this.spawner.spawnAsteroid(pos.x, pos.y, Math.random() * 50 + 10);
} }
} }
@ -171,8 +178,9 @@ class World {
this.tps = this.tpsCount / 5 | 0; this.tps = this.tpsCount / 5 | 0;
this.tpsCount = 0; this.tpsCount = 0;
this.tpsStart = Date.now(); this.tpsStart = Date.now();
if(this.tps < 50) if(this.tps < 50) {
wingbase.warning(`${this.room.name} TPS: ${this.tps}`); wingbase.warning(`${this.room.name} TPS: ${this.tps}`);
}
} }
this.tpsCount++; this.tpsCount++;

View file

@ -44,7 +44,7 @@ class Physics {
let s = SCALE; let s = SCALE;
let bodyDef = new Box2D.b2BodyDef(); let bodyDef = new Box2D.b2BodyDef();
bodyDef.userData = body; bodyDef.userData = body;
bodyDef.position = new b2Vec2(body.x / s || 0, body.y / s || 0); bodyDef.position = new b2Vec2(body.x || 0, body.y || 0);
bodyDef.angle = body.r || 0; bodyDef.angle = body.r || 0;
bodyDef.fixedRotation = false; bodyDef.fixedRotation = false;
bodyDef.active = true; bodyDef.active = true;
@ -55,7 +55,7 @@ class Physics {
bodyDef.angularDamping = body.type == 'asteroid' ? 0.003 : 0.06; bodyDef.angularDamping = body.type == 'asteroid' ? 0.003 : 0.06;
bodyDef.type = body.type == 'structure' ? bodyDef.type = body.type == 'structure' ?
Box2D.b2BodyType.b2_staticBody : Box2D.b2BodyType.b2_dynamicBody; Box2D.b2BodyType.b2_staticBody : Box2D.b2BodyType.b2_dynamicBody;
if (body.player || true) bodyDef.allowSleep = false; if (body.player) bodyDef.allowSleep = false;
let b2body = this.world.CreateBody(bodyDef); let b2body = this.world.CreateBody(bodyDef);
let fixtureDef = new Box2D.b2FixtureDef(); let fixtureDef = new Box2D.b2FixtureDef();