Processing for Android

EN ES

intersectsSphere()

This function returns true if the given ray intersects a sphere of radius r with a placement determined by the current geometry transformations applied to the scene. The ray can be automatically computed from the screen coordinates as in the following example:


void setup() {
  fullScreen(P3D);
}

void draw() {
  background(200, 0, 150);
  translate(width/2, height/2);  
  if (mousePressed && intersectsSphere(70, 0, 0)) fill(0, 0, 255);
  else fill(255, 0, 0);
  sphere(70);
}
                

A ray with arbitrary origin and direction can also be provided:


PVector origin = new PVector();
PVector direction = new PVector();

void setup() {
  fullScreen(P3D);
}

void draw() {
  origin.set(width/2, height/2, 0);
  direction.set(-width/2, -height/2).normalize();
  
  background(200, 0, 150);
  translate(width/2, height/2);  
  if (mousePressed && intersectsSphere(70, origin, direction)) fill(0, 0, 255);
  else fill(255, 0, 0);
  sphere(70);
}