So basically I want a box to be drawn right under the mouse and to be on the edge of a sphere. I've been trying to do this for a bit now and just can't get it right, I'd love it if someone could work this out for me.
- float srad = 0;
- float boxX = 0;
- float boxY = 0;
- float boxZ = 0;
- void setup() {
- size(1000,500,P3D);
- }
- void draw() {
- background(0);
- pushMatrix();
- translate(width/2,height/2,-500);
- noStroke();
- fill(200);
- sphere(height/2);
- srad = floor(screenX(height/2,0,0) - width/2);
- boxX = (mouseX - width/2) * ((height/2)/srad);
- boxY = (mouseY - height/2) * ((height/2)/srad);
- boxZ = sqrt((height/2)*(height/2) - boxX*boxX - boxY*boxY);
- translate(boxX,boxY,boxZ);
- fill(255,0,0);
- box(height/100);
- popMatrix();
- }
- void mouseClicked() {
- println(mouseX - width/2 + " * (" + height/2 + "/(" + srad + ")) = " + boxX);
- println(mouseY - height/2 + " * (" + height/2 + "/(" + srad + ")) = " + boxY);
- }
This is the furthest I've gotten on it and it's close, but the box is still not directly under the mouse. I believe this is because when the z is pushed forward to accommodate the x and y it is no longer drawn under the mouse because of how the camera sees it in 3D space. I'm just not sure how to compensate for this.
1