# P5 Questions! How to change direction of object

**URL:** https://discourse.processing.org/t/p5-questions-how-to-change-direction-of-object/8209
**Category:** Coding Questions
**Created:** [February 8, 2019, 5:21am UTC](https://discourse.processing.org/t/p5-questions-how-to-change-direction-of-object/8209 "2019-02-08T05:21:37Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ixd](https://avatars.discourse-cdn.com/v4/letter/i/41988e/32.png) [@ixd](https://discourse.processing.org/u/ixd)
#### Post date: [February 8, 2019, 5:21am UTC](https://discourse.processing.org/t/p5-questions-how-to-change-direction-of-object/8209/1 "2019-02-08T05:21:37Z")

</div>

Hi,

I’m new to javascript and am having a hard time figuring out how to move the balls up and down instead of left to right.

Thanks

```auto
function setup() {
  createCanvas(600, 200);
}

var ball = 0;
var ballSpeed = 10;
var ballChangeX = ballSpeed;

var ball2 = 100;
var ballSpeed2 = 5;
var ballChangeX2 = ballSpeed2;

function draw() {
  background(220);
  fill(200, 200, 0);
  ellipse(ball, height/2, 100);
  ellipse(ball2, height/4, 75);
  ball = ball + ballChangeX;
  ball2 = ball2 + ballChangeX2;

  // Check if ball is off right hand side of screen
  if (ball >= width) {
    ballChangeX = -ballSpeed;
  }
  if (ball2 >= width) {
    ballChangeX2 = -ballSpeed2;
  }

  // Check if ball is off left hand side of screen
  if (ball <= 0) {
    ballChangeX = ballSpeed;
  }
  if (ball2 <= 0) {
    ballChangeX2 = ballSpeed2;
  }
}

```

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [February 8, 2019, 10:20am UTC](https://discourse.processing.org/t/p5-questions-how-to-change-direction-of-object/8209/2 "2019-02-08T10:20:51Z")

</div>

Take a look at the `ellipse` function, it takes two arguments at the beginning `x` and `y` which are the coordinates where you draw it. As `y` represents the vertical dimension, change this line :

```auto
ellipse(width/2, ball, 100);

//And this one too so the ball is bouncing in height not in width: 

if (ball >= height) {
  ballChangeX = -ballSpeed;
}

```

- [https://p5js.org/reference/#/p5/ellipse](https://p5js.org/reference/#/p5/ellipse)
