cart253-2018

Physics / CART 253 / Fall 2018 / Pippin Barr

Polar coordinates and velocity


In this module


Positions on the Cartesian plane


Positions on the Cartesian plane


Velocity on the Cartesian plane


Velocity on the Cartesian plane


Polar coordinates


Polar coordinates


Polar to Cartesian


Polar to Cartesian


Polar to Cartesian


Using polar for velocity!


Using polar for velocity!


“Simple” Spaceship!

See 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();
}

Fin.