Shooting ball from a moving object

Complete noob to processing here. Writing in Java and doing a uni project on shooting aliens with a cannon.

I want the cannonball to fire from a cannon, which is moving up and down based on a spaceship’s coordinates. The spaceship automatically moves up and down the y axis.

When I try to base the cannonball’s location on the ship’s coordinates, the ball no longer fires from the cannon I believe because its coordinates are now permanently attached to the ship’s.

The ball has forces applied to it which makes it more complicated.

I’m wondering how to shoot the ball from its current location as the cannon is moving up and down along the y axis.

This is the code I think is relevant:

class CannonBall { 
  PVector location;
  PVector velocity;
  PVector acceleration;
  float mass;

  boolean hit;
  float currentX;
  float currentY;

  // Size
  float r = 10;

  float topspeed = 10;

  void update() { 

  void applyForce(PVector force) {

  void display() { 
    translate(spaceship.x, spaceship.y);
    ellipse(0, 0, r*2, r*2);

  CannonBall(float m, float x, float y) {
    mass = m;
    location = new PVector(x, y);
    velocity = new PVector();
    acceleration = new PVector();

  void collide(Alien a) {
    float dis = dist(location.x, location.y - 50, a.location.x, a.location.y);//calculate the distance between alien and cannonball
    float overlap = dis - mass/2 - a.mass/2;//calculate if they are overlapping 

    if (overlap < 0 && !a.isDead) { //if they are overlaping and if the alien is still alive
      a.mass -= 2;
      if (a.mass <=50) {
        hit = true;
    if (overlap > 0 && hit == true) {
      hit = false;
    if (a.mass <= 50) {
      a.isDead = true;

class Ship {
  float x;
  float y;
  float xspeed;
  float yspeed;
  float xsize, ysize;

  Ship(float _xsize, float _ysize, float _xspeed, float _yspeed, float xloc, float yloc) {
    y = yloc;
    x = xloc;

    xspeed = _xspeed;
    yspeed = _yspeed;
    xsize = _xsize;
    ysize = _ysize;

  void updateLocation() {
    //update both x and y location
    y = y + yspeed;
    //the ship bounces back when it reaches the edge of the window

    if ((y > height - ysize) || (y < 0)) {
      yspeed = yspeed * -1;

  void display() {
    image(ship, x, y, xsize, ysize);
spaceship = new Ship(120, 60, 0, 2, location.x, location.y);
ball = new CannonBall(10, location.x, location.y);


//draw the cannon
translate(spaceship.x, spaceship.y);
fill(100, 100, 100);
rect(0, -5, 50, 10);

if (shot) {
  PVector gravity = new PVector(0, 0.2);

//if ball goes off canvas, ball disappears
if (ball.location.y > height) {
  ball = new CannonBall(10, location.x, location.y);  
  shot = false;

How does the code appearing below your Cannonball class relate to your sketch? Where does it appear? It is much easier to give feedback if you create a cut down working example that can be tested – aka MCVE.

More generally – do you want the ship velocity to be added to the bullet velocity? Or is the bullet velocity the same regardless of which way the ship goes?