Created
October 29, 2019 15:59
-
-
Save companje/957a107045d502030b14aa7e53543c83 to your computer and use it in GitHub Desktop.
Mouse On Sphere In Perspective
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float distToCam = 1040; //1900 | |
float w=1200, h=1200, wd2=w/2, hd2=h/2, wdh=w/h; | |
void settings() { | |
fullScreen(P3D); | |
} | |
void setup() { | |
surface.setSize(1200, 1200); | |
surface.setLocation(360, 0); | |
hint(DISABLE_DEPTH_TEST); | |
} | |
void draw() { | |
perspective(atan(hd2/distToCam)*2, wdh, distToCam, 10000); | |
camera(0, 0, -distToCam, 0, 0, 0, 0, 1, 0); | |
scale(-1, 1, 1); //flip horizontal | |
//sphere | |
background(0); | |
fill(100); | |
stroke(255); | |
sphere(hd2); | |
//mouse/box | |
pushMatrix(); | |
PVector p = getMouseOnSphereInPerspective(); | |
translate(p.x, p.y, p.z); | |
fill(255, 0, 0); | |
noStroke(); | |
ellipse(-10, -10, 20, 20); | |
popMatrix(); | |
} | |
PVector getMouseOnSphereInPerspective() { | |
float r = wd2; | |
float cx = wd2; | |
float cy = wd2; | |
float mx = mouseX; | |
float my = mouseY; | |
float d = dist(mx, my, cx, cy); | |
float a = d/r * PI/2; | |
float x1 = sin(a); | |
float y1 = cos(a); | |
float x = (mx - cx) / d * x1; | |
float y = (my - cy) / d * x1; | |
float z = y1; | |
return new PVector(x*r, y*r, z*r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dit werkt bij distToCam is 1040. Maar om goed beeld op de globe te hebben moet distToCam op 1900 staan en dan werkt de muisfunctie niet goed meer.