fix explosion physics
This commit is contained in:
parent
bf8226481d
commit
8a8a9c8ae3
7 changed files with 54 additions and 24 deletions
|
@ -16,7 +16,7 @@ Renderer.prototype.renderAsteroid = (pallet, body) => {
|
||||||
}
|
}
|
||||||
context.closePath();
|
context.closePath();
|
||||||
context.strokeStyle = '#fff';
|
context.strokeStyle = '#fff';
|
||||||
context.fillStyle = '#090909';
|
context.fillStyle = body.debug ? `rgb(${body.debug}, 9, 9)` : '#090909';
|
||||||
context.fill();
|
context.fill();
|
||||||
context.stroke();
|
context.stroke();
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,11 @@ class Asteroid extends Body {
|
||||||
constructor(data) {
|
constructor(data) {
|
||||||
super(data)
|
super(data)
|
||||||
this.bodyType = 'asteroid';
|
this.bodyType = 'asteroid';
|
||||||
|
this.debug = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateType(data) {
|
||||||
|
this.debug = data[6];
|
||||||
}
|
}
|
||||||
|
|
||||||
tick() {
|
tick() {
|
||||||
|
|
|
@ -9,6 +9,8 @@ class Asteroid extends Body {
|
||||||
this.x = pos.x;
|
this.x = pos.x;
|
||||||
this.y = pos.y;
|
this.y = pos.y;
|
||||||
|
|
||||||
|
this.debug = 0;
|
||||||
|
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
|
||||||
this.type = 'asteroid';
|
this.type = 'asteroid';
|
||||||
|
@ -23,8 +25,13 @@ class Asteroid extends Body {
|
||||||
return [build];
|
return [build];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tickType() {
|
||||||
|
if (this.debug > 0)
|
||||||
|
this.debug--;
|
||||||
|
}
|
||||||
|
|
||||||
packTypeDelta() {
|
packTypeDelta() {
|
||||||
return [];
|
return [this.debug];
|
||||||
}
|
}
|
||||||
|
|
||||||
packFull() {
|
packFull() {
|
||||||
|
|
|
@ -26,6 +26,12 @@ class Body {
|
||||||
b.ApplyForce(new b2Vec2(x, y), c);
|
b.ApplyForce(new b2Vec2(x, y), c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
applyImpulse(x, y, center) {
|
||||||
|
let b = this.b2body;
|
||||||
|
let c = center ? new b2Vec2(center.x, center.y) : b.GetWorldCenter();
|
||||||
|
b.ApplyLinearImpulse(new b2Vec2(x, y), c);
|
||||||
|
}
|
||||||
|
|
||||||
applyTorque(f) {
|
applyTorque(f) {
|
||||||
this.b2body.ApplyTorque(f);
|
this.b2body.ApplyTorque(f);
|
||||||
}
|
}
|
||||||
|
@ -81,8 +87,8 @@ class Body {
|
||||||
|
|
||||||
get vel() {
|
get vel() {
|
||||||
return {
|
return {
|
||||||
xvel: this.b2body.GetLinearVelocity().x,
|
x: this.b2body.GetLinearVelocity().x,
|
||||||
yvel: this.b2body.GetLinearVelocity().y
|
y: this.b2body.GetLinearVelocity().y
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ const Physics = require('./physics.js');
|
||||||
const Ship = require('./ship.js');
|
const Ship = require('./ship.js');
|
||||||
const Spawner = require('./spawner.js');
|
const Spawner = require('./spawner.js');
|
||||||
|
|
||||||
|
const b2Vec2 = require('box2d-html5').b2Vec2;
|
||||||
|
|
||||||
class World {
|
class World {
|
||||||
constructor(room) {
|
constructor(room) {
|
||||||
this.physics = new Physics();
|
this.physics = new Physics();
|
||||||
|
@ -66,15 +68,20 @@ class World {
|
||||||
}
|
}
|
||||||
|
|
||||||
explosion(pos, power) {
|
explosion(pos, power) {
|
||||||
var rays = Array(50).fill().map((_, i) => {
|
var rays = Array(200).fill().map((_, i) => {
|
||||||
let a = Math.PI * i / 25;
|
let a = Math.PI * i / 100;
|
||||||
let x = pos.x + Math.cos(a) * Math.sqrt(power);
|
let rx = Math.cos(a) * Math.sqrt(power);
|
||||||
let y = pos.y + Math.sin(a) * Math.sqrt(power);
|
let ry = Math.sin(a) * Math.sqrt(power);
|
||||||
return this.physics.raycast(pos, { x : x, y: y }, (body, point, dis) => {
|
let x = pos.x + rx;
|
||||||
dis = Math.max(dis, 0.5);
|
let y = pos.y + ry;
|
||||||
let force = power * (1 / dis) * (1 / dis);
|
let closest = this.physics.raycast(pos, { x : x, y: y });
|
||||||
body.applyForce(x * force, y * force, point);
|
if (!closest)
|
||||||
});
|
return;
|
||||||
|
let dis = Math.max(closest.dis, 1);
|
||||||
|
let force = power * (1 / dis) * (1 / dis);
|
||||||
|
closest.body.debug = (255 * (force / power)) | 0;
|
||||||
|
force *= 0.001;
|
||||||
|
closest.body.applyImpulse(rx * force, ry * force, closest.point);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,10 +11,12 @@ class Missile extends Body {
|
||||||
this.xvel = pos.xvel;
|
this.xvel = pos.xvel;
|
||||||
this.yvel = pos.yvel;
|
this.yvel = pos.yvel;
|
||||||
this.r = pos.r;
|
this.r = pos.r;
|
||||||
|
this.xvel += Math.cos(this.r) * 0.2;
|
||||||
|
this.yvel += Math.sin(this.r) * 0.2;
|
||||||
|
|
||||||
this.source = source;
|
this.source = source;
|
||||||
this.player = source.player;
|
this.player = source.player;
|
||||||
this.fuel = 100;
|
this.fuel = 200;
|
||||||
|
|
||||||
this.type = 'missile';
|
this.type = 'missile';
|
||||||
this.frame = [[[0, 0], [10, 0], [10, 3], [0, 3]]];
|
this.frame = [[[0, 0], [10, 0], [10, 3], [0, 3]]];
|
||||||
|
@ -25,18 +27,19 @@ class Missile extends Body {
|
||||||
}
|
}
|
||||||
|
|
||||||
detonate() {
|
detonate() {
|
||||||
this.world.explosion(this.pos, 10);
|
this.world.explosion(this.center, 10);
|
||||||
this.world.removeBody(this);
|
this.world.removeBody(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
tickType() {
|
tickType() {
|
||||||
let power = 0.005;
|
let power = 0.004;
|
||||||
let x = Math.cos(this.b2body.GetAngleRadians()) * power;
|
let x = Math.cos(this.b2body.GetAngleRadians()) * power;
|
||||||
let y = Math.sin(this.b2body.GetAngleRadians()) * power;
|
let y = Math.sin(this.b2body.GetAngleRadians()) * power;
|
||||||
this.applyForce(x, y);
|
this.applyForce(x, y);
|
||||||
|
|
||||||
if(this.fuel-- <= 0)
|
if(this.fuel-- <= 0) {
|
||||||
this.detonate();
|
this.detonate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
packTypeDelta() {
|
packTypeDelta() {
|
||||||
|
|
|
@ -43,7 +43,7 @@ class Physics {
|
||||||
bodyDef.angle = body.r || 0;
|
bodyDef.angle = body.r || 0;
|
||||||
bodyDef.fixedRotation = false;
|
bodyDef.fixedRotation = false;
|
||||||
bodyDef.active = true;
|
bodyDef.active = true;
|
||||||
bodyDef.linearVelocity = new b2Vec2(body.xvel / s || 0, body.yvel / s || 0);
|
bodyDef.linearVelocity = new b2Vec2(body.xvel || 0, body.yvel || 0);
|
||||||
bodyDef.angularVelocity = body.rvel || 0;
|
bodyDef.angularVelocity = body.rvel || 0;
|
||||||
bodyDef.bullet = body.type == 'missile';
|
bodyDef.bullet = body.type == 'missile';
|
||||||
bodyDef.linearDamping = body.type == 'asteroid' ? 0.003 : 0.01;
|
bodyDef.linearDamping = body.type == 'asteroid' ? 0.003 : 0.01;
|
||||||
|
@ -69,13 +69,15 @@ class Physics {
|
||||||
//if (body.type == 'ship') console.log(b2body.GetLocalCenter());
|
//if (body.type == 'ship') console.log(b2body.GetLocalCenter());
|
||||||
}
|
}
|
||||||
|
|
||||||
raycast(start, end, callback) {
|
raycast(start, end) {
|
||||||
let p1 = new b2Vec2(start.x, start.y);
|
let p1 = new b2Vec2(start.x, start.y);
|
||||||
let p2 = new b2Vec2(end.x, end.x);
|
let p2 = new b2Vec2(end.x, end.y);
|
||||||
let dx = p1.x - p2.x;
|
let dx = p1.x - p2.x;
|
||||||
let dy = p1.y - p2.y;
|
let dy = p1.y - p2.y;
|
||||||
let dis = Math.sqrt(dx * dx + dy * dy);
|
let dis = Math.sqrt(dx * dx + dy * dy);
|
||||||
let closest = {};
|
let closest = {
|
||||||
|
fraction: 1
|
||||||
|
};
|
||||||
let i = 0;
|
let i = 0;
|
||||||
this.world.RayCast((fixture, point, normal, fraction) => {
|
this.world.RayCast((fixture, point, normal, fraction) => {
|
||||||
let body = fixture.GetBody().GetUserData();;
|
let body = fixture.GetBody().GetUserData();;
|
||||||
|
@ -83,13 +85,13 @@ class Physics {
|
||||||
closest = {
|
closest = {
|
||||||
body: body,
|
body: body,
|
||||||
fraction: fraction,
|
fraction: fraction,
|
||||||
point: point
|
point: point,
|
||||||
|
dis: dis * fraction
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//console.log(fixture.GetNext());
|
|
||||||
callback(body, point, dis * fraction);
|
|
||||||
return fraction;
|
return fraction;
|
||||||
}, p1, p2);
|
}, p1, p2);
|
||||||
|
return closest.body ? closest : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(body) {
|
remove(body) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue