Creat a ofCircle every x numbers of pixels

Hello everyone,

I can’t figured out how to create an ofBall every x numbers of pixels in the mouseDragged method. I create a ofBall vector and write:

void ofApp::mouseDragged(int x, int y, int button){
for (int i = 0; i < _ofBall.size(); i++){
    
    ofBall _tempBall;
    _tempBall.draw(100,100,40)
    _ofBall.push_back(_tempBall);
}

 }

So, I want to create x number of balls every 20 pixels for example. What I have to do?.

Thank you so much.

You’ll need variables to hold the positions of the mouse the last time the “balls” were added to the vector. Let’s say each 20 pixels, you want to add 5 balls to your vector.

In testApp.h, create the variables to hold the position:

float lastBallAddX;
float lastBallAddY;

In your mouse dragged function, write an if statement using ofDist function to see if the distance from the last time you added a ball, it is at least 20 pixels of distance away. Don’t forget to update your variables to the new position to be tested.

void ofApp::mouseDragged(int x, int y, int button){

if (ofDist (x, y, lastBallAddX, lastBallAddY) > 20) {

	for (int i = 0; i < 5; i++){
	    ofBall _tempBall;
	    _ofBall.push_back(_tempBall);
	}

	lastBallAddX = x;
	lastBallAddY = y;

    }
}

Hi @bschorr, thank you so much for your quickly reply. This is not exactly what I want to do. I want to create an infinity number of balls while i’m dragging the mouse along the screen. If I dragged from the left side to the middle I would create n number of balls (for example, 1 ball every 20 pixels). I think I have to use the ofDist function but inside when I create the for what I have to write instead of number 5?.

Thank you so much.

Hey @noobie, I’m not quite sure what you want to do… by “create”, you mean “draw to the screen”? Are the balls a separate class? There’s no such thing as an ofBall in OpenFrameworks… Do you mean ofCircle?

By infinity number of balls, you mean to draw them in a random part of the screen?.. Or do you mean under the mouse? Do you expect your balls to animate once they are “created”?.. Please be more specific and I can try to help.

Good luck in these beginnings! It gets better soon :wink:

My apologies @bschorr.

Yes, i have a class that draw a ball (ofCircle) but my class is named ofBall like the classes used in the tutorial page. So, what I want to do is create an n numbers of ball every 20 pixels between them along the x axis while you are dragging the mouse along the screen.

For example: If I dragg the mouse along the screen on the x = 300 axis the first ball will appear at 300, the next one at 320, the next one at 340 … all them will have the same y, until I release the mouse.

Thank you so much.

Some help?

Bump!

I’m not entirely sure what you want to achieve…

If you want to create some Balls starting when you press the mouse, along the y-as, with an 20px spacer between them, until you release the mouse you could do something like :

ofApp.h ::

#pragma once

#include "ofMain.h"

class testApp : public ofBaseApp{
public:
    void setup(){
        ofBackground(0);
    }
    void update(){
        if ((ofGetMousePressed())&&(ofGetFrameNum()%dropAmount==0)) {
            circles.push_back(lastMousePressed);
            lastMousePressed.x+=jumpAmount;
        }
    }
    
    
    void draw(){
        ofSetColor(ofColor::white);
        for (auto it(circles.begin()); it != circles.end();) {
            (*it).y += fallDownAmount;
            if ((*it).y >= ofGetHeight()) {
                it = circles.erase(it);
            } else {
                ofCircle((*it), circleRadius);
                it++;
        }   }   }
		
    void mousePressed(int x, int y, int button){
        lastMousePressed = ofPoint(x,y);
    }
    
private:
    ofPoint lastMousePressed;
    vector<ofPoint> circles;
    const int jumpAmount = 20;
    const int dropAmount = 20;
    const int circleRadius = 10;
    const int fallDownAmount = 1;
};

Or if you want the “balls” to drop from your current mouse x (instad of the fixed “jumpAmount”) you could change the update function to:

void update(){
        if ((ofGetMousePressed())&&(ofGetFrameNum()%dropAmount==0)) {
            circles.push_back(ofPoint(ofGetMouseX(), lastMousePressed.y));
        }
    }

hope this helps…