How to move circles repetitively


I am trying to create snowballs falling down the screen continuously. I gotten them to move down, but I need them to offset so that they all move down differently. I know this is a bit of a silly question, but I am only just starting to program in C++ could someone help please?

here is my h. file:

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

		void setup();
		void update();
		void draw();

		void ball(int i);
		ofImage maple;
		float time;
		float xPos, yPos, startingX, startingY, spacingX, spacingY;


Here is my cpp. file:

#include "ofApp.h"

void ofApp::setup(){
	time = 0;
	yPos = 1;
	startingY = 4;
	spacingY = ofGetHeight() / startingY;


void ofApp::update(){
	time += 0.05;
	yPos += 2;
	if (ofGetHeight() < yPos) {
		yPos = 5;

void ofApp::draw(){
	for (int i = 0; i < 100; i++) {

void ofApp::ball(int i) {

	float locY = spacingY * i + startingY;
	float offset = ofMap(ofDist(0, locY, ofGetWidth(), ofGetHeight() / 3), 0, ofGetWidth(), locY, 0); // the second to last value changes the offset, which changes the mapping of each row of circles
	float osc = ofMap(sin(time + offset), -1, 1, 0, 1); // oscillation controls movement of the cirles, controlled by sin which uses time to control speed and offset to control the timing of movement 
	ofTranslate(0, osc);
	ofCircle(ofRandom(4, ofGetWidth() - 4), yPos, ofRandom(2,8));

You can use ofRandom() to assign random speeds and offsets to the snowballs


How would I make each circle go down using ofRandom? the problem I am having is that one snowball is going down at the moment and when I have tried to increment the amount to add more, they draw in the same place, How would I change this?


you should at least use some vector<float> to keep track of different balls positions (and speeds) or better create a Ball class to make this in an objected oriented way (it could seems more difficult for a beginner but it will make your job really easier)

also (if you have more time) you should check out “The Nature of Code” book
(there is also an openFrameworks porting of the examples)

1 Like

Ideally you want to create a class for the ball. The code will looks something like that.

in the .h file

#pragma once
#include "ofApp.h"
class ball {
      int x;
      int y;
      int radius;

      void ball::draw();

in the .cpp file

#include "person.h"
        ball::ball() {
        	x = ofRandom(10,1000);
        	y = ofRandom(10,1000);
                radius = ofRandom(10,100);
        void ball::draw() {
        	ofCircle(x, y, radius);

And then inside the ofApp.h class scope, you can create a vector, which is basically a dynamic array (you can add/remove members dynamically in runtime, without the need to know in advanced how many) to hold how ever many balls you need to create.

in ofApp.h
vector<ball> manyBalls;

And finally in the ofApp.cpp you can create new ball and add it to the array like the code below. You probably want to do this not in the draw function, but in the setup, or a keyboard/mouse event. Otherwise you will be adding a new ball on every frame.

ball myNewBall;

And then inside the ofApp::draw() function draw all of them by iterating through the array

for( int i=0; i<manyBalls.size(); i++ ) {

You can use a similar iteration to run other functionalities of the ball class that you may need to add.

I haven’t tested the above code, as I am not home right now so it may not work, but it may be a good start for what you need to look and research further.

@npisanti This is such a great resource for general graphics knowledge. Thanks for sharing!!!