its working fine!
achieved what i set out to do. many thanks patriciogonzalezvivo. really appreciate it.
hey nathan, im appending the enitire code below for all my files. that should help you. feel free to revert if youre not understanding what ive done anywhere.
heres the ofBall.h file.
#ifndef _OF_BALL
#define _OF_BALL
#include "ofMain.h"
class ofBall{
public:
//methods
void setup();
void update();
void draw();
//constructor
ofBall(int posX, int posY, int radius );
//variables
int speedX;
int speedY;
int posX;
int posY;
int radius;
void collide(ofBall* inBall);
};
#endif
heres the ofBall.cpp
#include "ofBall.h"
//--------------------------------------------------------------
ofBall::ofBall(int _posX , int _posY , int _radius ){
posX = _posX;
posY = _posY;
radius = _radius;
speedX = ofRandom(1, 5);
speedY = ofRandom(1, 5);
}
//--------------------------------------------------------------
void ofBall::update(){
posX = posX + speedX;
posY = posY + speedY;
////////////////////////////////////
if (posY > ofGetHeight()-radius) {
posY = ofGetHeight()-radius;
speedY = speedY * -1;
}
////////////////////////////////
if (posX > ofGetWidth()-radius) {
posX = ofGetWidth()-radius;
speedX = speedX * -1;
}
///////////////////////////////////
if (posY < radius) {
posY = radius;
speedY = speedY * -1;
}
////////////////////////////////////
if (posX < radius) {
posX = radius;
speedX = speedX * -1;
}
///////////////////////////////////
string str = "Ball X,Y positions are X ";
str += ofToString(posX, 0) + " and Y ";
str += ofToString(posY, 0);
str += " Screen width is ";
str += ofToString(ofGetWidth(), 0) + " and height is ";
str += ofToString(ofGetHeight(), 0) + ".";
ofSetWindowTitle(str);
///////////////////////////////////
}
//--------------------------------------------------------------
void ofBall::draw(){
ofFill();
ofSetColor(0, 255, 0);
ofCircle(posX, posY , radius);
ofSetColor(0, 255, 255);
ofDrawBitmapString("my first of app", 100,100);
}
//--------------------------------------------------------------------------------------------
void ofBall::collide(ofBall* inBall){
if (ofDist(inBall->posX, inBall->posY, posX, posY) < radius*2) {
ofDrawBitmapString("crash", 150,150);
speedX*=-1; //change X direction
speedY*=-1; //change Y direction
}
}
heres my testApp.h
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofBall.h"
class testApp : public ofBaseApp{
public:
//methods
void setup();
void update();
void draw();
//declaring the object
ofBall **myBall;// creating an array of pointers
int nBalls;
};
#endif
and heres my testApp.cpp
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
ofBackground(0, 0, 0);
//ofSetFrameRate(60);
ofSetVerticalSync(true); //sets fps to your monitor refresh rate. makes animation smoother.
//ofSetWindowTitle("simple bouncing ball");
nBalls = 10; // the number of ball objects we want to create
myBall = new ofBall*[nBalls]; // an array of pointers for the objects
for (int i = 0; i < nBalls; i++){ //assigning values to posX and posY and radius
int posX = ofRandom(0, 1024);
int posY = ofRandom(0, 768);
int radius = 10;
myBall[i] = new ofBall(posX,posY,radius); //create each object from the array
}
}
//--------------------------------------------------------------
void testApp::update(){
for (int i=0; i < nBalls; i++) {
myBall[i]->update();
for (int j=0; j < nBalls; j++){
if (i != j) {
myBall[i] -> collide(myBall[j]);
}
}
}
}
//--------------------------------------------------------------
void testApp::draw(){
for (int i=0; i < nBalls; i++) {
myBall[i]->draw();
}
}
that bit in the testApp update where im calling the collide method i created in the ofBall.cpp is where i was getting stuck.
good luck man.
cheers.