x
and y
position (in pixels)vx
and vy
, representing movement with velocityx,y
and x',y'
by adding the velocityx
and y
componentsx
and y
representationx
and y
are basically how our canvas/screen is “really” set upSee presenter notes
???
var ship = {
angle: 0,
speed: 0,
x: 0,
y: 0,
}
function setup() {
createCanvas(500,500);
ship.x = width/2;
ship.y = height/2;
}
function draw() {
handleInput();
moveShip();
drawShip();
}
function handleInput() {
if (keyIsDown(LEFT_ARROW)) {
ship.angle -= 0.1;
}
else if (keyIsDown(RIGHT_ARROW)) {
ship.angle += 0.1;
}
if (keyIsDown(UP_ARROW)) {
ship.speed = 5;
}
else if (keyIsDown(DOWN_ARROW)) {
ship.speed = 0;
}
}
function moveShip() {
// The magic lines for calculating velocity!
var vx = ship.speed * cos(ship.angle);
var vy = ship.speed * sin(ship.angle);
ship.x += vx;
ship.y += vy;
}
function drawShip() {
push();
translate(ship.x,ship.y);
rotate(ship.angle);
ellipse(0,0,25,25);
line(0,0,25,0);
pop();
}