Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

How to move circles repetitively

$
0
0

@jadie_p1 wrote:

Hi,

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{

	public:
		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(){
	maple.load("maple_leaf.png");
	time = 0;
	ofSetFrameRate(100);
	yPos = 1;
	ofSetBackgroundColor(54,81,94,100);
	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++) {
	ball(i);

	}
}

void ofApp::ball(int i) {
	ofPushMatrix();

	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);
	ofSetColor(255);
	ofCircle(ofRandom(4, ofGetWidth() - 4), yPos, ofRandom(2,8));
	ofSetColor(0);
	//ofSetColor(255);
	ofPopMatrix;
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles