# Why is my ball going downwards?

**URL:** <https://discourse.processing.org/t/why-is-my-ball-going-downwards/5822>\
**Category:** Coding Questions\
**Created:** [November 22, 2018, 8:48am UTC](https://discourse.processing.org/t/why-is-my-ball-going-downwards/5822 "2018-11-22T08:48:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![OliverMcDk](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OliverMcDk](https://discourse.processing.org/u/OliverMcDk)\
**Post date:** [November 22, 2018, 8:48am UTC](https://discourse.processing.org/t/why-is-my-ball-going-downwards/5822/1 "2018-11-22T08:48:49Z")

</div>

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:

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 22, 2018, 8:49am UTC](https://discourse.processing.org/t/why-is-my-ball-going-downwards/5822/2 "2018-11-22T08:49:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 22, 2018, 9:23am UTC](https://discourse.processing.org/t/why-is-my-ball-going-downwards/5822/3 "2018-11-22T09:23:53Z")

</div>

Hi OliverMcDk,

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

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6b828e0264f1efa1c9a5e16c46cf3b6099dd5b1f.png)

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

---

<div class="post-metadata">

**Author:** ![OliverMcDk](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OliverMcDk](https://discourse.processing.org/u/OliverMcDk)\
**Post date:** [November 22, 2018, 10:36am UTC](https://discourse.processing.org/t/why-is-my-ball-going-downwards/5822/4 "2018-11-22T10:36:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 22, 2018, 11:03am UTC](https://discourse.processing.org/t/why-is-my-ball-going-downwards/5822/5 "2018-11-22T11:03:06Z")

</div>

Here’s a working example for you.

```auto
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.

---

<div class="post-metadata">

**Author:** ![OliverMcDk](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OliverMcDk](https://discourse.processing.org/u/OliverMcDk)\
**Post date:** [November 22, 2018, 11:13am UTC](https://discourse.processing.org/t/why-is-my-ball-going-downwards/5822/6 "2018-11-22T11:13:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![OliverMcDk](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OliverMcDk](https://discourse.processing.org/u/OliverMcDk)\
**Post date:** [November 22, 2018, 11:16am UTC](https://discourse.processing.org/t/why-is-my-ball-going-downwards/5822/7 "2018-11-22T11:16:45Z")

</div>

My code with \</\>

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 22, 2018, 4:39pm UTC](https://discourse.processing.org/t/why-is-my-ball-going-downwards/5822/8 "2018-11-22T16:39:20Z")

</div>

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

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

```
