Object clicked

Hi everyone!

I’m doing an OF application with rectangles moving around a center point (center of the screen). The idea is: when you click on a rectangle, another one appears in a random position.

My problem is to know when one of these rectangles was clicked, because i can’t check the position because the rectangle is always moving.

The rectangles are a simple ofRectangle. I was looking for somekind of method or function to check if the rectangle was clicked.

Any suggestions?

Hi lipo,

maybe you can solve this problem by using ofMSAinteractiveObjects. À Class written by Mehmet Akten. You will find it, when u search for it, there should also Be an example.

Lg

while memo’s addon will be good, you probably will be aided by making an object –

see the OOP tutorial here:

http://wiki.openframeworks.cc/index.php-…-2B-Classes

if you make an object that has a x,y,w,h, then you can check for clicks by:

  
if( x > myObject.x && x < (myObject.x + myObject.width) && y > myObject.y &&  y < (myObject.y + myObject.height) ){  
  // do something here    
}   

hope that helps !
zach

Thanks for the suggestions!

@zach: i have a class very similar as Mikkel Koser’s ofBall class. The problem is to update the position of my rectangle. It’s moving around the center of the screen, something like:

  
  
  glPushMatrix();  
  // Translate coordinate origin   
  glTranslatef(ofGetWidth()/2,ofGetHeight()/2,0);  
    
  // Rotate rectangles around it  
    j -= 0.05;  
    glRotatef(j,0,1,0);  
    // ... here i draw the rectangles  
    glPopMatrix();  
  

I can’t find the way to update the position and check if the rectangle have been clicked (just the same way like you posted). :?
I’m trying to make some maths to update the x position, but the z position it’s important because it modifies the w of the rectangle.

Thanks again!
Lipo

instead of using glRotate/glTranslate, you can use the ofxVectorMath addon to rotate the vertexes.

your class can have a:

  
ofxVec3f vertices[4];  

and then in every transformation apply the same rotation/translation you were doing with opengl but using:

  
vertices.rotate(...)  

then the vertices contain the real coordinates and you can compare the mouse position without problem.

:slight_smile: nice trick

Hi Arturo!

So you say, something like this…

  
  
  ofxVec3f vertices[4];  
  
  vertices[0] = rectangle.x; // x1  
  vertices[1] = rectangle.y; // y1  
  vertices[2] = rectangle.x + rectangle.width; // x2  
  vertices[3] = rectangle.y + rectangle.height; // y2  
  

That’s what you suggest? And rotate vertices.rotate(…) around the axis i want.

I’m gonna try it!!
Thanks Arturo!

Lipo

yes that should be it, indeed you don’t even need the rectangle anymore as now you have the same data in the array of vec3f

I think i got it!! :smiley:

Based on the vectorMathExample, using ofxVec3f as arturo said, i can control at any time the coordinates of my rectangle!! (look at the grid coordinates)

Thanks everybody!

Lipo

Hi again!

Let me explain the situation now…

I have the squares moving around a central axis. I have the position of the squares (x, y and z) around the central axis. But the problem is… the axis of the mouse are respect the screen [center (0,0) on the top left], and the axis of the squares are respect the center of the screen.

I tried to calculate it by substracting the center weight and height to the mouse coordinates, but it doesn’t work.

Do you have any suggestion?

Thanks!
Lipo

I tried to calculate it by substracting the center weight and height to the mouse coordinates, but it doesn’t work.

it should :slight_smile: there should be some wrong calculation in there.

Hi Arturo,

I tried to calculate both ways: the mouse coordinates to the center origin; And the rectangle coordinates from the top left origin…

But i haven’t good results… :S

In addition, i have problems to draw a perfect rectangle. I don’t know why, but i use this to draw the rectangles:

  
glBegin(GL_QUADS);  
            ofSetColor(255, 222, 0, 80);  
            glVertex3f(rect[i].x, rect[i].y, rect[i].z);  
            glVertex3f(rect[i].x+TAM_RECT, rect[i].y, rect[i].z);  
            glVertex3f(rect[i].x, rect[i].y+TAM_RECT, rect[i].z);  
            glVertex3f(rect[i].x+TAM_RECT, rect[i].y+TAM_RECT, rect[i].z);  
         glEnd();  

and i only got a square with a triangle inside of it, i means, like a poligon with 5 points.

Anybody knows why?

Thanks
Lipo

you should use GL_TRIANGLE_FAN for filled rect or GL_LINE_LOOP

but easier to just use:

ofRect(x,y,w,h)

about your problem with the mouse coordinates is difficult to say if you don’t post some code