Add landing physics
This commit is contained in:
parent
9435e887a4
commit
d85338d9f2
6 changed files with 38 additions and 18 deletions
|
@ -37,9 +37,9 @@ export default class Body {
|
|||
return [nx - cx, ny - cy];
|
||||
}
|
||||
|
||||
rotateVector(x, y, r) {
|
||||
return [(x * Math.cos(this.r) - y * Math.sin(this.r)),
|
||||
(y * Math.cos(this.r) - x * Math.sin(this.r))];
|
||||
rotateVector(x, y, r = this.r) {
|
||||
return [(x * Math.cos(r) - y * Math.sin(r)),
|
||||
(y * Math.cos(r) - x * Math.sin(r))];
|
||||
}
|
||||
|
||||
relativeVector(x, y) {
|
||||
|
@ -65,9 +65,12 @@ export default class Body {
|
|||
|
||||
distanceTo(body) {
|
||||
let [[ax, ay], [bx, by]] = [this.com, body.com];
|
||||
let result = Math.max(Math.sqrt(((bx - ax) ** 2) +
|
||||
return Math.max(Math.sqrt(((bx - ax) ** 2) +
|
||||
((by - ay) ** 2)), 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
angleTo(ax, ay, bx, by) {
|
||||
return Math.atan2(by - ay, bx - ax);
|
||||
}
|
||||
|
||||
approach(body, distance) {
|
||||
|
@ -83,7 +86,7 @@ export default class Body {
|
|||
}
|
||||
|
||||
applyDirectionalForce(x, y, r) {
|
||||
let [vx, vy] = this.rotateVector(x, y, r);
|
||||
let [vx, vy] = this.rotateVector(x, y);
|
||||
this.xvel += vx;
|
||||
this.yvel += vy;
|
||||
this.rvel += r / this.mass;
|
||||
|
|
|
@ -10,7 +10,7 @@ export function createThrustExhaust(thruster) {
|
|||
xvel: ship.xvel + fx,
|
||||
yvel: ship.yvel + fy,
|
||||
color: '#f4c542',
|
||||
lifetime: 10,
|
||||
lifetime: 5,
|
||||
size: 0.07
|
||||
}));
|
||||
}
|
||||
|
|
|
@ -66,12 +66,25 @@ export default class Ship extends Body {
|
|||
})
|
||||
}
|
||||
|
||||
resolveCelestialCollision(pos, cel) {
|
||||
//debugger;
|
||||
let theta = this.angleTo(...this.com, ...cel.com);
|
||||
let angleToCom = this.angleTo(...this.com, ...pos);
|
||||
let angle = angleToCom - theta;
|
||||
let [force] = this.rotateVector(0, 1, angle);
|
||||
if (Math.abs(angle) < 0.3) {
|
||||
force *= -1;
|
||||
}
|
||||
this.rvel -= force * 0.015;
|
||||
}
|
||||
|
||||
checkModuleCollision(module, body) {
|
||||
let p = this.getWorldPoint(...module.localCom)
|
||||
let p = this.getWorldPoint(...module.localCom);
|
||||
let dis = body.distanceTo({ com: p });
|
||||
if (dis < body.radius + 0.5) {
|
||||
this.approach(body, dis - (body.radius + 0.5));
|
||||
this.halt();
|
||||
this.resolveCelestialCollision(p, body);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue