Collision problem

Hi, I’m trying to make a game where you grab an object that disappears when it touches the player, and then appears in a random place, similar to the snake game. The collision would be between two images, or it can be between the player image and a circle, but I’m not sure if I should make a particle system or if there’s an easier way without any add-on since I can’t figure out how to make the particle system work. Unfortunately the player has to use the arrow keys to move so using getMouseX and getMouseY is not possible. Any help?

Hi, You can use ofxBox2d but that might be a bit of overkill for your purpose.

if it is as simple as you describe, use the ofRectangle class. it is as simple as a rectangle with an x and y coordinates that are the upper left corner, and a width and height of it, but it has a lot of really useful funcitons for modifying it and checking interactions with it. Check its documentation ofRectangle | openFrameworks

So say you have this


ofImage playerImg;
ofRectangle player;
if(playerImg.load("pathtoImage")){
player.set(0,0, img.getWidth(), img.getHeight()):
}
else{
// if the image was not loaded assign the rectangle some size. or do whatever else you thing is useful
player.set(0,0,100,100);
}
//to move around the player, use 
player.translateX(10);// or whatever ammount
player.translateY(10);// or whatever ammount

//or to set its position you can use 
player.x = 0;
player.y = 0;

//or the following will set the position of fisrt  2 numbers to be the center of the rectangle 
// I find it really useful
player.setFromCenter(0,0, player.width, player.height);

Do the same for the object you want to check with.

Then to check collisions.


if(player.intersects(anotherRectangle)){
anotherRectangle is another instance of ofRectangle;
//this are intersecting or colliding. do something
}

if(player.inside(anotherRectangle)){
//this happens only when another rectangle is completely inside player. 
}
if(player.inside(ofGetMouseX(), ofGetMouseY()){
// this happens when the mouse is inside the rectangle.
}

//then to draw just use 

playerImg.draw(player);

or 
ofDrawRectangle(player);

hope this helps