Why is my ball going downwards?

Hey, can anyone help me to get my ball to “shoot” upwards. Kinda like a canon, so my ball shoots, and i can change the degrees its shoots after.
My code is very simple, cause im new to this program.
We are trying to make it into a “Angry Birds” kinda like game.

My code:

int mouseCounter = 0;

float boldSize = 20;
float gravity = 1;
float friction = 0.90;

float[] xList = new float [1000];
float[] yList = new float [1000];
float[] xSpeedList = new float [1000];
float[] ySpeedList = new float [1000];
float[] redFarve = new float [1000];
float[] greenFarve = new float [1000];
float[] blueFarve = new float [1000];


void setup(){
  size(1680,1005);
  frameRate(60);
  smooth();
  //noStroke();
}

void draw(){
  clear();
  
  
  for(int index = 0; index < xList.length; index++){
    
    float x = xList[index];
    float y = yList[index];
    float xSpeed = xSpeedList[index];
    float ySpeed = ySpeedList[index];
  fill(redFarve[index],greenFarve[index],blueFarve[index]);
  ellipse(x,y,boldSize,boldSize);
  x = x + xSpeed;
  y = y + ySpeed;
  ySpeed = ySpeed + gravity;
  
  if(x >= width - 10 || x < 10){
    xSpeed = -xSpeed;
  }
  
  if (y >= height - 10 || y < 10){
    ySpeed = -ySpeed * friction;
  }
    xList[index] = x;
    yList[index] = y;
    xSpeedList[index] = xSpeed;
    ySpeedList[index] = ySpeed;
  }
}

void mousePressed(){
  xList [mouseCounter] = random(mouseX,mouseX);
  yList[mouseCounter] = random(mouseY,mouseY);
  xSpeedList[mouseCounter] = random(10,20);
  ySpeedList[mouseCounter] = random(10,20);
  redFarve[mouseCounter] = random(0,255);
  greenFarve[mouseCounter] = random(0,255);
  blueFarve[mouseCounter] = random(0,255);

  mouseCounter = mouseCounter + 1;
}
1 Like

Edit your post. Select your code. Hit the format like code button, which looks like this: </>

Hi OliverMcDk,

Remember that the screen coordinate system on processing is as followed:

image

So if you have a positive Y speed you are actually going downward.

1 Like

Hi jb4x!

So what would i have to change in my code to make it shoot directly towards x and then make it fall down becuase of my gravity. Would it be able for the “user” to set a shooting angle via my code?

Here’s a working example for you.

class Projectile {
  boolean sim;
  float px, py, vx, vy, ax, ay; 
  Projectile() {
    px = 20;
    py = 20;
  }
  void draw() {
    if ( sim ) {
      ax = 0;
      ay = 0.32;
      vx += ax;
      vy += ay;
      px += vx;
      py += vy;
    }
    noStroke();
    fill(0, 200, 0);
    ellipse( px, py, 20, 20 );
  }
  void go(float ivx, float ivy) {
    vx = ivx;
    vy = ivy;
    sim = true;
  }
}

Projectile p = new Projectile();
float a;
boolean ku, kd;


void setup() {
  size(600, 400);
}

void draw() {
  background(0);
  a += ( ku ? -0.03 : 0 ) + ( kd ? 0.03 : 0 );
  p.draw();
  stroke(200);
  line(20, 20, 20 + 10 * cos(a), 20 + 10 * sin(a) );
}

void keyPressed() {
  if ( keyCode == UP ) ku = true;
  if ( keyCode == DOWN ) kd = true;
  if ( keyCode == ' ' ) p.go( 10 * cos(a), 10 * sin(a) );
}

void keyReleased() {
  if ( keyCode == UP ) ku = false;
  if ( keyCode == DOWN ) kd = false;
}

Controls are UP and DOWN arrows to pick angle, and SPACEBAR to shoot it.

Notice that the projectile has 6 variables that determine its movement. Two for acceleration, two for velocity, and two for position.

Yo thats sick!
Would it be able to implement it into my own code?
Maybe even implement it so it matches the beginner-level of my code

My code with </>

int mouseCounter = 0;

float boldSize = 20;
float gravity = 1;
float friction = 0.90;

float[] xList = new float [1000];
float[] yList = new float [1000];
float[] xSpeedList = new float [1000];
float[] ySpeedList = new float [1000];
float[] redFarve = new float [1000];
float[] greenFarve = new float [1000];
float[] blueFarve = new float [1000];


void setup(){
  size(1680,1005);
  frameRate(60);
  smooth();
  //noStroke();
}

void draw(){
  clear();
  
  
  for(int index = 0; index < xList.length; index++){
    
    float x = xList[index];
    float y = yList[index];
    float xSpeed = xSpeedList[index];
    float ySpeed = ySpeedList[index];
  fill(redFarve[index],greenFarve[index],blueFarve[index]);
  ellipse(x,y,boldSize,boldSize);
  x = x + xSpeed;
  y = y + ySpeed;
  ySpeed = ySpeed + gravity;
  
  if(x >= width - 10 || x < 10){
    xSpeed = -xSpeed;
  }
  
  if (y >= height - 10 || y < 10){
    ySpeed = -ySpeed * friction;
  }
    xList[index] = x;
    yList[index] = y;
    xSpeedList[index] = xSpeed;
    ySpeedList[index] = ySpeed;
  }
}

void mousePressed(){
  xList [mouseCounter] = random(mouseX,mouseX);
  yList[mouseCounter] = random(mouseY,mouseY);
  xSpeedList[mouseCounter] = random(10,20);
  ySpeedList[mouseCounter] = random(10,20);
  redFarve[mouseCounter] = random(0,255);
  greenFarve[mouseCounter] = random(0,255);
  blueFarve[mouseCounter] = random(0,255);

  mouseCounter = mouseCounter + 1;
}

As I wrote, you just need to change the direction of your speed:

ySpeedList[mouseCounter] = -random(10,20);