Created
July 2, 2016 01:35
-
-
Save yuriks/514c92ac5991c278dd97d2e2d517f392 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Eye e1; | |
void setup() { | |
size(640, 360); | |
noStroke(); | |
e1 = new Eye(640/2, 360/2, 64); | |
} | |
void draw() { | |
background(102); | |
e1.update(mouseX, mouseY); | |
e1.display(); | |
} | |
class Eye { | |
int x, y; | |
int size; | |
float angle = 0.0; | |
float mouse_angle = 0.0; | |
float px = 0.0; | |
float pz = 0.0; | |
float delta = 0.0; | |
Eye(int tx, int ty, int ts) { | |
x = tx; | |
y = ty; | |
size = ts; | |
} | |
void update(int mx, int my) { | |
float ex = 0; | |
float ez = 0; | |
px = -(my-y); | |
pz = (mx-x); | |
float vx = px - ex; | |
float vz = pz - ez; | |
mouse_angle = atan2(-vz, vx) / (PI / 180.0); | |
delta = (mouse_angle - angle) % 360.0; | |
if (delta > 180.0) delta -= 360; | |
else if (delta < -180.0) delta += 360; | |
float v = 5.0; | |
float mover = delta > v ? v : delta < -v ? -v : delta; | |
if (mousePressed) angle += mover; | |
} | |
void display() { | |
pushMatrix(); | |
translate(x, y); | |
fill(255); | |
ellipse(0, 0, size, size); | |
rotate(-((angle+90) / 180 * PI)); | |
fill(153, 204, 0); | |
ellipse(size/4, 0, size/2, size/2); | |
popMatrix(); | |
fill(255); | |
//text(String.format("cur angle: %.2f", angle), 8, 16); | |
//text(String.format("mouse angle: %.2f", mouse_angle), 8, 32); | |
//text(String.format("delta angle: %.2f", delta), 8, 48); | |
//text(String.format("mouse pos x,z: %.0f, %.0f", px, pz), 8, 64); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment