Skip to content

Instantly share code, notes, and snippets.

@davepape
Created September 19, 2013 12:38
Show Gist options
  • Save davepape/6622817 to your computer and use it in GitHub Desktop.
Save davepape/6622817 to your computer and use it in GitHub Desktop.
interpolation demo
# interp.py
# by Dave Pape, for DMS 423
#
# Moves two squares by interpolation of their X position
# The timing used in the interpolation is different -
# the red square moves purely linearly; the blue square
# uses a slow-in-slow-out
import time
from pyglet.gl import *
window = pyglet.window.Window(500,500)
pos1 = 100
pos2 = 200
startTime = time.time()
square = pyglet.graphics.vertex_list(4, ('v2f', [-10,-10, 10,-10, -10,10, 10,10]))
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
glPushMatrix()
glTranslatef(pos1,350,0)
glColor3f(1,0,0)
square.draw(GL_TRIANGLE_STRIP)
glPopMatrix()
glPushMatrix()
glTranslatef(pos2,150,0)
glColor3f(0,0,1)
square.draw(GL_TRIANGLE_STRIP)
glPopMatrix()
def lerp(start,end,a):
return (1-a) * start + a * end
def slowInSlowOut(a):
return -2*a*a*a + 3*a*a
def update(dt):
global pos1, pos2, startTime
a = (time.time() - startTime) / 5
if a > 1:
a = 0
startTime = time.time()
pos1 = lerp(100,400,a)
pos2 = lerp(100,400,slowInSlowOut(a))
pyglet.clock.schedule_interval(update,1/60.0)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment