David David - 1 month ago 17
Java Question

Processing - ScreenToWorldCoordinate function?

I'm working on a project with Processing that requires the ability to determine whether or not the mouse is inside of a circle. Therefore, I need to obtain the position of the circle and the position of the mouse. However, the position of the circle has been modified using matrix functions such as

. For example:

float circle_x;
float circle_y;
float circle_radius;

void setup() {
circle_x = 10.0;
circle_y = 17.0;
circle_radius = 15.0;

void draw() {

/* ... arbitrary number of calls to modify the matrix ... */
translate(THING, THING);
/* ... */

/* draw the circle */
ellipse(circle_x, circle_y, circle_radius, circle_radius);

/* now I want to detect whether or not my mouse is inside of the
circle. In order to do that, I need to modify the coordinates
of the mouse in the same fashion as circle_x and circle_y. I'm
hoping to do something like this: */
float world_x = screenToWorld_X(mouseX);
float world_y = screenToWorld_Y(mouseY);

/* ... check if [world_x, world_y] is inside the circle ... */



Is there any way to do this in Processing? I was looking at the documentation but I couldn't seem to find any functions. If not, how can I achieve my goal?



Check out the coordinates section of the reference.

Specifically, you're looking for the modelX() and modelY() functions. Or you could use the screenX() and screenY() functions to go the other way and convert your world coordinates into screen coordinates.