This function creates a ray emerging from a point on the screen plane and perpendicular to it. Since the ray comprises an origin position and a unit direction, it returns two PVector objects. One can pass a PVector array where the two vectors will be stored, and the function returns the array as a result value. If the argument array is null, the function will initialize the array:
PVector[] ray;
void setup() {
fullScreen(P3D);
}
void draw() {
background(0);
ray = getRayFromScreen(mouseX, mouseY, ray);
// ...
}
Alternatively, one can pass two non-null PVector objects where the origin and direction of the ray will be stored:
PVector origin = new PVector();
PVector direction = new PVector();
void setup() {
fullScreen(P3D);
}
void draw() {
background(0);
getRayFromScreen(mouseX, mouseY, origin, direction);
}