Object detection, or other methode

What I am trying to do is the following. I draw a grid of rectangles / squares with numbers of columns (n) and rows (m). Now i would like a certain shape to be under that not visible, but interacting with the rows and columns squares / squares. Now when the shape under is touching one or multiple of these they should do something, like change the color.

I have no problem creating the array of shapes, I was wondering if there is a lib for doing something like that, or should I use Box2D for the collision detection, if that is possible.

Any suggestion would really be helpful, I am kind of in a black hole how i should approach the problem.

I think some sample code or a visual illustration would probably help us understand what you are trying to achieve – it’s a little difficult to understand … :wink:

Sure, here is a sample, the boxes are drawn from the middle, there is also a circle, what I would like to do, whenever the circle touches a rectangle / square they should perform a certain function (example go red and fade out).

I am stuck at the part how and what i should add to the code to detect that interaction. and how i would get the correct ID from the rectangle / square so i can call the function on it.

The code has some ofxUI in it so i could change the size of the grid and change the radius of the circle.

#include "ofApp.h";
int numX;
int numY;
int xSize;
int ySize;
int circleDiameter;
int circleStroke;


//--------------------------------------------------------------
void ofApp::setup(){
	gui = new ofxUICanvas();        //Creates a canvas at (0,0) using the default width

	gui->addIntSlider("NUMBER BOXES X", 1, ofGetWidth() / 20, &numX);
	gui->addIntSlider("NUMBER BOXES Y", 1, ofGetWidth() / 20, &numY);
	gui->addIntSlider("Circle Diameter", 1, ofGetWidth() / 1.7, &circleDiameter);
	gui->addIntSlider("Circle Stroke", 1, 100, &circleStroke);

	gui->addToggle("FULLSCREEN", false);
	gui->autoSizeToFitWidgets();
	ofAddListener(gui->newGUIEvent, this, &ofApp::guiEvent);
	gui->loadSettings("settings.xml");
}

//--------------------------------------------------------------
void ofApp::update(){
	ofBackground(0, 0, 0);
	xSize = (ofGetWidth() / numX) * 1;
	ySize = (ofGetHeight() / numY) * 1;

}

//--------------------------------------------------------------
void ofApp::draw(){
	ofPushMatrix();
	ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2, 0);
	ofSetCircleResolution(50);
	ofCircle(0, 0, circleDiameter);
	for (int i = 0; i < numX; i++) {
		for (int j = 0; j < numY; j++) {
			ofNoFill();
			ofRect(xSize * i, ySize * j, xSize * 1, ySize * 1);
			ofRect(-xSize * i, ySize * j, -xSize * 1, ySize * 1);
			ofRect(xSize * i, -ySize * j, xSize * 1, -ySize * 1);
			ofRect(-xSize * i, -ySize * j, -xSize * 1, -ySize * 1);
		}
	}
	ofPopMatrix();
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

}

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

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}

void ofApp::exit()
{
	gui->saveSettings("settings.xml");
	delete gui;
}

void ofApp::guiEvent(ofxUIEventArgs &e)
{
	if (e.getName() == "FULLSCREEN")
	{
		ofxUIToggle *toggle = e.getToggle();
		ofSetFullscreen(toggle->getValue());
	}
}

Anything? Still looking for a solution how to handle this.

While you might try to code this yourself, I’d recommend using ofxBox2D. This will allow you to attach collision callbacks (e.g. http://www.iforce2d.net/b2dtut/collision-callbacks) or (https://github.com/vanderlin/ofxBox2d/tree/master/example-ContactListener for an example).

Awesome thank you for this link, this solves my problem complete, love the call back idea with.

1 Like