john
john

Reputation: 11

How to make an ellipse move up and down on processing

I want to make an ellipse move up and down So, can you tell me how to make an ellipse move up and down

Upvotes: 1

Views: 11475

Answers (2)

quinton-ashley
quinton-ashley

Reputation: 140

Try using the sin or cosine functions to oscillate the circle up and down!

float angle = 0;

public void draw() {
    if (angle > 359) {
        angle = 0;
    }
    ellipse(0, sin(radians(angle)) * height, 20, 20);
    angle++;
}

Upvotes: 0

Kevin Workman
Kevin Workman

Reputation: 42176

Step 1: Store the state of your scene in some variables. For you, this might just by a circleY variable.

float circleY = 50;

Step 2: Use that variable to draw your scene.

ellipse(50, circleY, 10, 10);

Step 3: Modify that variable over time to change the scene.

circleY++;

Step 4: Add checks to make sure your variables don't go outside the range.

if(circleY > height){
  circleY = 0;
}

Putting it all together, it looks like this:

float circleY = 50;

void draw() {
  background(0);
  ellipse(50, circleY, 10, 10); 
  circleY++;
  if (circleY > height) {
    circleY = 0;
  }
}

falling circle

Of course, this is just an example. You'd have to add extra logic for switching direction instead of teleporting back to the top of the screen. You might use a directionY variable that you switch when the circle hits the top or bottom of the screen.

But the basics are the same: use variables to store your state, change those variables over time, and draw your scene using those variables.

I suggest you try something and post an MCVE in a new question if you get stuck. Good luck.

Upvotes: 1

Related Questions