Add landing physics
This commit is contained in:
parent
9435e887a4
commit
d85338d9f2
6 changed files with 38 additions and 18 deletions
21
dist/img/celestials/green_0.svg
vendored
21
dist/img/celestials/green_0.svg
vendored
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 47 KiB After Width: | Height: | Size: 47 KiB |
1
dist/improcket.min.js.map
vendored
Normal file
1
dist/improcket.min.js.map
vendored
Normal file
File diff suppressed because one or more lines are too long
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
2
rollup
2
rollup
|
@ -1,2 +1,2 @@
|
|||
#!/bin/bash
|
||||
rollup js/index.mjs --o dist/improcket.min.js --f es --watch
|
||||
rollup js/index.mjs --o dist/improcket.min.js -m --silent --f es --watch
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue