Skip to content

Instantly share code, notes, and snippets.

@ImanHosseini
Created July 17, 2020 13:28
Show Gist options
  • Save ImanHosseini/f76f03a25d4adb475dd983685f56b158 to your computer and use it in GitHub Desktop.
Save ImanHosseini/f76f03a25d4adb475dd983685f56b158 to your computer and use it in GitHub Desktop.
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import time
import math
B = 6000
L = 3000
H = 400
g = 1000.0
k = 0.005
theta = 50.0*math.pi/180.0
phi = 0.0
v_phi = 180.0
v_theta = -0.0001
theta_d = v_theta / L
phi_d = v_phi / (L*math.sin(theta))
C0 = math.sin(theta)*math.sin(theta)*phi_d
t0 = time.time()
tr = time.time() - t0
t = 0.0
def polar_to_xy():
rho = float(L)*math.sin(theta)
return float(B)/2.0 - rho*math.cos(phi), float(B)/2.0 - rho*math.sin(phi)
last_x, last_y = polar_to_xy()
L_SCALE = 1.0/100.0
def square():
global t,theta,phi,theta_d,phi_d,last_x,last_y,tr
# Time keeping
t_new = time.time() - t0
dt = (t_new - tr)*6
tr = t_new
t = t + dt
# Physics
theta += theta_d * dt
phi += phi_d * dt
phi_d = C0*math.exp(-t*k) / (math.sin(theta)*math.sin(theta)+0.0000001)
# print(f"{theta_d}|{phi_d}")
theta_dd = -k*theta_d - g*(1.0/float(L))*math.sin(theta) + (phi_d*phi_d*math.sin(theta)*math.cos(theta))
# theta_dd = - g*(1.0/float(L))*math.sin(theta) + (phi_d*phi_d*math.sin(theta)*math.cos(theta))
theta_d += theta_dd*dt
# Drawing
x_new, y_new = polar_to_xy()
glBegin(GL_LINE_STRIP)
glVertex2f(last_x, last_y)
glVertex2f(x_new,y_new)
glEnd()
last_x,last_y = x_new,y_new
def iterate():
glViewport(0, 0, 1000, 1000)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, B, 0.0, B, 0.0, 1.0)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity()
def showScreen():
# glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
iterate()
glColor3f(1.0, 0.0, 3.0)
square()
glutSwapBuffers()
glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(1000, 1000)
glutInitWindowPosition(0, 0)
wind = glutCreateWindow("OpenGL Coding Practice")
glutDisplayFunc(showScreen)
glutIdleFunc(showScreen)
glutMainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment