<p>
Click to start.
</p>
<p>
Adjust thrust, starting position, fuel.
</p>
<p>
Dots are drawn once per second. Green when undergoing thrust, red when coasting.
</p>
<br>
<br>
<br>
<br>
<br>
CSS
canvas { border: 1px solid black; }
JavaScript
// Ship vars
var fuel = 30;
var position = {x:0, y: 300};
var velocity = {x:0, y:0};
var thrust = {x:17.4, y:0};//x=15
// Relative Directions
var nav = {
// vertical
down: {x:null, y:null,},
up: {x:null, y:null,},
// horizontal
clockwise: {x:null, y:null,},
anticlockwise: {x:null, y:null,},
// ship-relative
prograde: {x:null, y:null,},
retrograde: {x:null, y:null,},
};
// Planet vars
var gravityVector = {x:null, y:null};
var gravityForce = -1;
// Viewport Vars
var world = document.querySelector(".world");
var worldWidth = 1000;
var worldHeight = 1000;
var dotW = 1;
var dotH = 1;
var dotColor = "green";
var count = 0;
var step = 1000;
function tick() {
//console.log(position.x);
//console.log(position.y);
if(position.x==null||position.y==null){
}
calculateVelocity();
renderTick();
//if(++count%step<step-1){
//console.log(count%step)
setTimeout(tick, 0);
//}
}
var calculateVelocity = function () {
// no thrust if not enough fuel
var totalThrust = thrust.x + thrust.y;
if (fuel > totalThrust) {
fuel = fuel - totalThrust;
} else {
dotColor = "gray";
thrust = {x:0, y:0};
}
gravityVector = getGravityVector();
// update velocity
velocity.x = velocity.x + thrust.x + gravityVector.x;
velocity.y = velocity.y + thrust.y + gravityVector.y;
// update position
position.x = position.x + velocity.x;
position.y = position.y + velocity.y;
};
var calculateThrust = function () {
debugger;
// no thrust if not enough fuel
var totalThrust = thrust.x + thrust.y;
if (fuel > totalThrust) {
fuel = fuel - totalThrust;
} else {
dotColor = "gray";
return {x:0, y:0};
}
nav.down = getDownVector();
nav.up = getOppositeVector(nav.down.x);
nav.clockwise = getPerpindicularVectorRight(up);
nav.anticlockwise = getPerpindicularVectorLeft(up);
gravityVector = getGravityVector(down);
// update velocity
velocity.x = velocity.x + thrust.x + gravityVector.x;
velocity.y = velocity.y + thrust.y +...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.