We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Mouse collision (newbie alert)
Page Index Toggle Pages: 1
Mouse collision (newbie alert) (Read 550 times)
Mouse collision (newbie alert)
May 24th, 2009, 11:20pm
 
Hi all,

I am new to processing, however am familiar with programming and understand the basics of OOP.  I am wanting to know if there is existing syntax that would allow me to determine if a user has clicked on a shape or not.

For example, I have an array of objects (the object has x, y & size values) which I am using to plot a series of ellipses.  I am then wanting to modify the properties (x/y coordinates) of the clicked ellipse such that it will move.

Has anyone done something similar to this before that might assist me?  I am suspecting I am likely to need to use the mouse input operators but that is as far as I have got so far.  Am I going to have to scan through my array of objects to determine if there is a collision?

Any help would be greatly appreciated.

Cheers
DW
Re: Mouse collision (newbie alert)
Reply #1 - May 25th, 2009, 1:31am
 
dww wrote on May 24th, 2009, 11:20pm:
Am I going to have to scan through my array of objects to determine if there is a collision

Yes.

Processing is a bit low level (compared to JavaFX for example), which is good for performance and control of behavior, but need a bit more of work.
Re: Mouse collision (newbie alert)
Reply #2 - May 25th, 2009, 4:37am
 
Quote:
Am I going to have to scan through my array of objects to determine if there is a collision?

No you can get Processing to do it for you.

One of the neat features of Processing is being able to make objects of user defined classes e.g. your ellipse reposnsible for its own drawing and mouse handling.

The code below demonstrates this and should be reasonably easy to follow.

Code:


Ball[] balls = new Ball[10];

int tx, ty;
Ball selected;

void setup(){
 size(600,600);
 for(int i = 0; i < 10; i++){
   balls[i] = new Ball();
   registerDraw(balls[i]);
   registerMouseEvent(balls[i]);
 }
}

void draw(){
 background(0);
}


public class Ball{
 int x,y, radius, diameter;
 int fillCol, strokeCol;
 
 Ball(){
   int r,g,b;
   r = 128+(int)random(127);
   g = 128+(int)random(127);
   b = 128+(int)random(127);
   fillCol = color(r,g,b);
   strokeCol = color(r/2,g/2,b/2);
   x = 50 + (int)random(400);
   y = 50 + (int)random(400);
   diameter = 50 + (int)random(100);
   radius = diameter/2;
 }
 
 boolean isOver(int mx, int my){
   return (mx-x)*(mx-x) + (my-y)*(my-y) < radius*radius;
 }
 
 // This method is called automatically every loop
 // becasue we have registerDraw(this object)
 void draw(){
   ellipseMode(CENTER);
   fill(fillCol);
   stroke(strokeCol);
   strokeWeight(2);
   ellipse(x,y,diameter,diameter);
 }
 
 // This method is called automatically every loop
 // becasue we have registerMouseEvent(this object)
 void mouseEvent(MouseEvent event){
   if((selected == null && isOver(mouseX, mouseY))
                                     || selected == this){
     switch(event.getID()){
       case MouseEvent.MOUSE_PRESSED:
         selected = this;
         break;
       case MouseEvent.MOUSE_DRAGGED:
         x = x + mouseX - pmouseX;
         y = y + mouseY - pmouseY;
         break;
       case MouseEvent.MOUSE_RELEASED:
         selected = null;
         break;
     }
   }
 }
}
Re: Mouse collision (newbie alert)
Reply #3 - May 25th, 2009, 4:59am
 
Quark wrote on May 25th, 2009, 4:37am:
No you can get Processing to do it for you.

Technically, that's still you making the code (the classes, the handling) instead of Processing wrapping and hiding the plumbing in ready-made generic objects.

Now, using the undocumented (but publicly/officially available) registerXxx methods is a smart move to avoid looping on objects, indeed, I haven't tried them yet.
Re: Mouse collision (newbie alert)
Reply #4 - May 25th, 2009, 7:43am
 
Quote:
Technically, that's still you making the code (the classes, the handling) instead of Processing wrapping and hiding the plumbing in ready-made generic objects.


True the ellipse(), rect() etc provide ready made 2D shapes but they aren't objects. Although the Ball class simply wraps up the ellipse() function to make circles the use of classes/objects when linked with Processing's event handling routines is a very powerful technique and could be for creating user defined shapes  e.g. a car by modifying the draw() and isOver() methods.

Quote:
Now, using the undocumented (but publicly/officially available) r...

Yes they are undocumented in the Reference section (I only just realized this when I read your post) but they are in the developers section specifically under libraries. if anyone is interested in finding out more about them then go here http://dev.processing.org/libraries/basics.html

I included the example because dww is familiar with the basics of OO and I hope it shows that Processing has more than one way to handled events.
Page Index Toggle Pages: 1