Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Little example - following eyes

edited June 2012 in General Posts: 50

Inspired by the *nix xeyes widget ( and to bring back a bit of vector math, I wrote this little example. It might be useful as a beginners example.
"Look into my eyes, look into my eyes, the eyes, the eyes, not around the eyes, don't look around the eyes, look into my eyes (snaps fingers) you're under." ;-)


-- Use this function to perform your initial setup
function setup()
    center = vec2(WIDTH/2, HEIGHT/2)
    leftEyeCenter = vec2(center.x - 70, center.y)
    rightEyeCenter = vec2(center.x + 70, center.y)    
    leftEye = leftEyeCenter
    rightEye = rightEyeCenter

-- This function gets called once every frame
function draw()
    background(3, 95, 253, 255)
    -- draw the white part of the eyes
    stroke(0, 0, 0, 255)
    fill(255, 255, 255, 255)
    ellipse(leftEyeCenter.x, leftEyeCenter.y, 130, 300)
    ellipse(rightEyeCenter.x, rightEyeCenter.y, 130, 300)
    -- draw the 'irisses'
    fill(0, 0, 0, 255)
    ellipse(leftEye.x, leftEye.y, 50)
    ellipse(rightEye.x, rightEye.y, 50)

function touched(touch) 
    -- calculate direction from the left and right eye to the new position
    local newpos = vec2(touch.x, touch.y)
    local direction = (newpos-leftEyeCenter):normalize()
    -- determine whether the touch is on the eyes by 
    -- measuring the distance for x and y values of each eye.
    -- if so, take the smallest value
    local delta = vec2(math.min(40, math.abs(leftEyeCenter.x - newpos.x)),
    math.min(120, math.abs(leftEyeCenter.y - newpos.y)))
    leftEye = leftEyeCenter + vec2(direction.x * delta.x, direction.y * delta.y)
    -- do the same for the right eye  
    direction = (newpos-rightEyeCenter):normalize()
    delta = vec2(math.min(40, math.abs(rightEyeCenter.x - newpos.x)),
    math.min(120, math.abs(rightEyeCenter.y - newpos.y)))
    rightEye = rightEyeCenter + vec2(direction.x * delta.x, direction.y * delta.y)



Sign In or Register to comment.