fix explosion physics

This commit is contained in:
Asraelite 2016-03-25 16:41:15 +00:00
parent bf8226481d
commit 8a8a9c8ae3
7 changed files with 54 additions and 24 deletions

View file

@ -16,7 +16,7 @@ Renderer.prototype.renderAsteroid = (pallet, body) => {
}
context.closePath();
context.strokeStyle = '#fff';
context.fillStyle = '#090909';
context.fillStyle = body.debug ? `rgb(${body.debug}, 9, 9)` : '#090909';
context.fill();
context.stroke();

View file

@ -2,6 +2,11 @@ class Asteroid extends Body {
constructor(data) {
super(data)
this.bodyType = 'asteroid';
this.debug = 0;
}
updateType(data) {
this.debug = data[6];
}
tick() {

View file

@ -9,6 +9,8 @@ class Asteroid extends Body {
this.x = pos.x;
this.y = pos.y;
this.debug = 0;
this.size = size;
this.type = 'asteroid';
@ -23,8 +25,13 @@ class Asteroid extends Body {
return [build];
}
tickType() {
if (this.debug > 0)
this.debug--;
}
packTypeDelta() {
return [];
return [this.debug];
}
packFull() {

View file

@ -26,6 +26,12 @@ class Body {
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) {
this.b2body.ApplyTorque(f);
}
@ -81,8 +87,8 @@ class Body {
get vel() {
return {
xvel: this.b2body.GetLinearVelocity().x,
yvel: this.b2body.GetLinearVelocity().y
x: this.b2body.GetLinearVelocity().x,
y: this.b2body.GetLinearVelocity().y
}
}
}

View file

@ -4,6 +4,8 @@ const Physics = require('./physics.js');
const Ship = require('./ship.js');
const Spawner = require('./spawner.js');
const b2Vec2 = require('box2d-html5').b2Vec2;
class World {
constructor(room) {
this.physics = new Physics();
@ -66,15 +68,20 @@ class World {
}
explosion(pos, power) {
var rays = Array(50).fill().map((_, i) => {
let a = Math.PI * i / 25;
let x = pos.x + Math.cos(a) * Math.sqrt(power);
let y = pos.y + Math.sin(a) * Math.sqrt(power);
return this.physics.raycast(pos, { x : x, y: y }, (body, point, dis) => {
dis = Math.max(dis, 0.5);
let force = power * (1 / dis) * (1 / dis);
body.applyForce(x * force, y * force, point);
});
var rays = Array(200).fill().map((_, i) => {
let a = Math.PI * i / 100;
let rx = Math.cos(a) * Math.sqrt(power);
let ry = Math.sin(a) * Math.sqrt(power);
let x = pos.x + rx;
let y = pos.y + ry;
let closest = this.physics.raycast(pos, { x : x, y: y });
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);
});
}

View file

@ -11,10 +11,12 @@ class Missile extends Body {
this.xvel = pos.xvel;
this.yvel = pos.yvel;
this.r = pos.r;
this.xvel += Math.cos(this.r) * 0.2;
this.yvel += Math.sin(this.r) * 0.2;
this.source = source;
this.player = source.player;
this.fuel = 100;
this.fuel = 200;
this.type = 'missile';
this.frame = [[[0, 0], [10, 0], [10, 3], [0, 3]]];
@ -25,18 +27,19 @@ class Missile extends Body {
}
detonate() {
this.world.explosion(this.pos, 10);
this.world.explosion(this.center, 10);
this.world.removeBody(this);
}
tickType() {
let power = 0.005;
let power = 0.004;
let x = Math.cos(this.b2body.GetAngleRadians()) * power;
let y = Math.sin(this.b2body.GetAngleRadians()) * power;
this.applyForce(x, y);
if(this.fuel-- <= 0)
if(this.fuel-- <= 0) {
this.detonate();
}
}
packTypeDelta() {

View file

@ -43,7 +43,7 @@ class Physics {
bodyDef.angle = body.r || 0;
bodyDef.fixedRotation = false;
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.bullet = body.type == 'missile';
bodyDef.linearDamping = body.type == 'asteroid' ? 0.003 : 0.01;
@ -69,13 +69,15 @@ class Physics {
//if (body.type == 'ship') console.log(b2body.GetLocalCenter());
}
raycast(start, end, callback) {
raycast(start, end) {
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 dy = p1.y - p2.y;
let dis = Math.sqrt(dx * dx + dy * dy);
let closest = {};
let closest = {
fraction: 1
};
let i = 0;
this.world.RayCast((fixture, point, normal, fraction) => {
let body = fixture.GetBody().GetUserData();;
@ -83,13 +85,13 @@ class Physics {
closest = {
body: body,
fraction: fraction,
point: point
point: point,
dis: dis * fraction
}
}
//console.log(fixture.GetNext());
callback(body, point, dis * fraction);
return fraction;
}, p1, p2);
return closest.body ? closest : false;
}
remove(body) {