Ober3550

Turtle Physics

Mar 8th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.34 KB | None | 0 0
  1. from time import sleep
  2. from turtle import *
  3. from random import randint
  4. from numpy import array
  5. from math import atan, tan, sin, cos, pi
  6. setup()
  7. bgcolor("Black")
  8.  
  9. #Setup the crew
  10. turtle = Turtle()
  11. turtle2 = Turtle()
  12. turtle3 = Turtle()
  13. screen = Screen()
  14.  
  15.  
  16. Objects = [[[310,-310],[310,-200],[-300,-310]]] #Slope
  17. Objects += [[[-200,-300],[-200,300],[-190,300],[-190,-300]]] #Rectangle
  18.  
  19. #Setup the ball
  20. turtle.color("blue")
  21. turtle.shape("circle")
  22. tracer(False)
  23. turtle.speed("fastest")
  24.  
  25. #Setup the walls
  26. turtle2.color("white")
  27. turtle2.penup()
  28. turtle2.hideturtle()
  29.  
  30. turtle3.color("Red")
  31. turtle3.shape("circle")
  32. turtle3.speed("fastest")
  33. turtle3.hideturtle()
  34.  
  35. class Ball:
  36. #Physics stuff
  37. pos = [0,0]
  38. speed = [5,5]
  39. elas = 0.5
  40.  
  41. #Move the ball to start position
  42. turtle.penup()
  43. turtle.goto(Ball.pos)
  44. turtle.pendown()
  45.  
  46. #Draw the canvases walls
  47. def Walls():
  48. for i in range(len(Objects)):
  49. turtle2.begin_fill()
  50. for j in Objects[i]:
  51. turtle2.goto(j)
  52. turtle2.pendown()
  53. if j == Objects[i][-1]:
  54. turtle2.goto(Objects[i][0])
  55. turtle2.end_fill()
  56. turtle2.penup()
  57. bounding = True
  58. def intersection(a,b,c,d):
  59. ## print(a,b)
  60. ## print(c,d)
  61.  
  62. if bounding == True:
  63. turtle3.showturtle()
  64. turtle3.penup()
  65. turtle3.goto(a)
  66. turtle3.pendown()
  67. turtle3.goto(b)
  68. turtle3.penup()
  69. turtle3.goto(c)
  70. turtle3.pendown()
  71. turtle3.goto(d)
  72. turtle3.penup()
  73. turtle3.penup()
  74. turtle3.goto(a[0],a[1])
  75. turtle3.pendown()
  76. turtle3.goto(b[0],a[1])
  77. turtle3.goto(b[0],b[1])
  78. turtle3.goto(a[0],b[1])
  79. turtle3.goto(a[0],a[1])
  80. turtle3.penup()
  81.  
  82. #y=mx+c
  83. ##
  84. ## if min(a[0],b[0])!=a[0]:
  85. ## temp = a
  86. ## a = b
  87. ## b = temp
  88. if min(c[0],d[0])!=c[0]:
  89. temp = c
  90. c = d
  91. d = temp
  92.  
  93. I = [min(a[0],b[0]),max(a[0],b[0]),min(c[0],d[0]),max(c[0],d[0])]
  94. J = [min(a[1],b[1]),max(a[1],b[1]),min(c[1],d[1]),max(c[1],d[1])]
  95. if J[0] > J[3] or J[1] < J[2] or I[0] > I[3] or I[1] < I[2]:
  96. return
  97.  
  98. #If both x are 0 return
  99. if a[0]-b[0]==0 and c[0]-d[0] == 0:
  100. return
  101.  
  102. #Return intersection where a,b's x is zero
  103. if a[0]-b[0]==0:
  104. #Find gradient
  105. m1 = 0
  106. m2 = (d[1]-c[1])/(d[0]-c[0])
  107. c2 = c[1]-m2*c[0]
  108.  
  109. #Find intersect
  110. x = a[0]
  111. y = m2*x+c2
  112.  
  113. deflection = rebound(a,b,c,d,x,y,m1,m2)
  114. return deflection
  115.  
  116. #Return intersection where c,d's x is zero
  117. if c[0]-d[0]==0:
  118. m1 = (b[1]-a[1])/(b[0]-a[0])
  119. c1 = a[1]-m1*a[0]
  120. m2 = 0
  121. x = c[0]
  122. y = m1*x+c1
  123. deflection = rebound(a,b,c,d,x,y,m1,m2)
  124. return deflection
  125.  
  126. if a[0]-b[0]!=0 and c[0]-d[0] != 0:
  127. #Gradients and C values
  128. m1 = (b[1]-a[1])/(b[0]-a[0])
  129. c1 = a[1]-m1*a[0]
  130.  
  131. m2 = (d[1]-c[1])/(d[0]-c[0])
  132. c2 = c[1]-m2*c[0]
  133.  
  134. #y=mx+c
  135. #m1x+c1=m2x+c2
  136. #m1x-m2x=c2-c1
  137. #x(m1-m2)=c2-c1
  138. #x = (c2-c1)/(m1-m2)
  139.  
  140. #Calculate x,y with maths
  141. x = (c2-c1)/(m1-m2)
  142. y = m1*x+c1
  143.  
  144. if bounding == True:
  145. turtle3.goto(x,y)
  146.  
  147. #Parallel check
  148. if m1==m2:
  149. return
  150.  
  151. ## print("m1,m2:",m1,m2)
  152. ## print("c1,c2:",c1,c2)
  153. ## print(c,d)
  154. ## print(x,y)
  155. ## print(a,b)
  156.  
  157.  
  158. if x < I[1] and x > I[0] and y < J[1] and y > J[0]:
  159. deflection = rebound(a,b,c,d,x,y,m1,m2)
  160. return deflection
  161. else:
  162. ###print(x,y)
  163. ###print(a,b)
  164. ###print(c,d)
  165. ###print(I1[0],I2[0])
  166. return
  167.  
  168. def rebound(a,b,c,d,x,y,m1,m2):
  169. s = [b[0]-a[0],b[1]-a[1]]
  170. mag = 0.9*(abs(s[0]**2+s[1]**2)**0.5)
  171. reflection = 0
  172. if a[0]-b[0]==0:
  173. if s[1]<0:
  174. reflection = atan(m2)+pi/2
  175. else:
  176. reflection = atan(m2)-pi/2
  177. if c[0]-d[0]==0:
  178. if s[0]<0:
  179. reflection = -atan(m1)
  180. else:
  181. reflection = pi-atan(m1)
  182. if a[0]-b[0]!=0 and c[0]-d[0]!=0:
  183. if s[1]<0:
  184. if s[0]<0:
  185. if m2<0:
  186. #Negative x,y intersect downward slope 2
  187. reflection = pi/2+atan(m1)+atan(m2)
  188. else:
  189. #Negative x,y intersect upward slope 4
  190. reflection = pi+atan(m1)-atan(m2)
  191. else:
  192. if m2<0:
  193. #Negative y intersect downward slope 1
  194. reflection = (-pi/2)-atan(m2)-atan(m1)
  195. else:
  196. #Negative y intersect upward slope 3
  197. reflection = (pi/2)+atan(m2)+atan(m1)
  198. else:
  199. if s[0]<0:
  200. if m2<0:
  201. #Positive y intersect downward slope 6
  202. reflection = (pi/2)-atan(m2)-atan(m1)
  203. else:
  204. #Positive y intersect upward slope 8
  205. reflection = -atan(m2)+atan(m1)
  206. else:
  207. if m2<0:
  208. #Positive x,y intersect downward slope 5
  209. reflection = (-pi/2)-atan(m2)-atan(m1)
  210. else:
  211. #Positive x,y intersect upward slope 7
  212. reflection = (pi/2)-atan(m1)-atan(m2)
  213.  
  214.  
  215.  
  216.  
  217. ## if s[1]<0: #Not hitting the ceiling
  218. ## if m2 > 0:
  219. ## if s[0]>0:
  220. ## print(1)
  221. ## reflection = atan(m2)-atan(m1)
  222. ## else:
  223. ## print(2)
  224. ## reflection = pi-atan(m1)+atan(m2)
  225. ## else:
  226. ## if s[0]>0:
  227. ## print(3)
  228. ## reflection = (pi/2)+atan(m2)+atan(m1)
  229. ## else:
  230. ## print(4)
  231. ## reflection = -pi+atan(m2)
  232. ## else:
  233. ## if m2 > 0:
  234. ## print(3)
  235. ## reflection = -(pi/2)+atan(m2)-atan(m1)
  236. ## else:
  237. ## print(4)
  238. ## reflection = (pi/2)-atan(m1)+atan(m2)
  239. #turtle.goto(x,y)
  240. deflection = [cos(reflection)*mag,sin(reflection)*mag]
  241. if bounding == True:
  242. turtle3.goto(x,y)
  243. turtle3.pendown()
  244. turtle3.color("green")
  245. turtle3.goto(b)
  246. turtle3.penup()
  247. turtle3.color("red")
  248. turtle3.goto(x,y)
  249. turtle3.pendown()
  250. turtle3.color("yellow")
  251. turtle3.goto(x+deflection[0],y+deflection[1])
  252. turtle3.color("red")
  253. turtle3.penup()
  254. ## print("Speed vector:",s)
  255. ## print("Intersecting angle:",atan(m1))
  256. ## print("Slope of platform:",atan(m2))
  257. ## print("Reflection off platform:",reflection)
  258. ## print("Magnitude:",mag)
  259. ## print("Resultant vector:",deflection)
  260. return deflection
  261.  
  262. #print(intersection([100,100],[0,-100],[0,0],[300,150]))
  263. #print(intersection([0,0],[200,-300],[0,-300],[300,-200]))
  264. #print(intersection([0,-100],[0,-300],Objects[0][2],Objects[0][1]))
  265. #print(intersection([-100,0],[200,-300],[0,-300],[300,-200]))
  266. #print(intersection([100,100],[50,0],[0,0],[300,150]))
  267.  
  268. #Slopes
  269. print(intersection([-320, 200],[-250,100],[-400,200],[-200,100]))
  270. print(intersection([0, 200],[-150,100],[-200,200],[0,100]))
  271. print(intersection([0, 200],[150,100],[0,100],[200,200]))
  272. print(intersection([350, 200],[250,100],[400,200],[200,100]))
  273.  
  274. print(intersection([-390, 0],[-250,100],[-400,100],[-200,0]))
  275. print(intersection([-90, 0],[-150,100],[-200,100],[0,0]))
  276. print(intersection([90, 0],[150,100],[0,0],[200,100]))
  277. print(intersection([350, 0],[250,100],[400,100],[200,0]))
  278.  
  279. #print(intersection([0,100],[300,-300],Objects[0][2],Objects[0][1]))
  280. #print(intersection([0,-100],[300,-300],Objects[0][2],Objects[0][1]))
  281. #print(intersection([0,0],[-200,-300],Objects[0][2],Objects[0][1]))
  282. #print(intersection([0,-100],[-300,-50],[0,-200],[-300,100]))
  283. #print(intersection([200,0],[100,-200],[-100,0],[200,-200]))
  284. #print(intersection([0,0],[100,-200],[-100,0],[200,-200]))
  285. #print(intersection([-150,50],[-50,-100],[0,-100],[-200,0]))
  286.  
  287. #Ceiling deflections
  288. #print(intersection([0,0],[-200,200],[-100,0],[-110,300]))
  289. #print(intersection([0,0],[-200,200],[-100,0],[0,300]))
  290. #print(intersection([200,200],[0,0],[0,300],[100,0]))
  291. #print(intersection([-50,0],[-200,200],[-100,0],[0,90]))
  292.  
  293. #Wall deflections
  294. #print(intersection([0,0],[-200,200],[-100,0],[-100,300]))
  295. #print(intersection([-200,0],[200,200],[-100,0],[-100,300]))
  296. #print(intersection([0,200],[-200,0],[-100,0],[-100,300]))
  297. #print(intersection([0,0],[0,200],[-100,100],[100,150]))
  298.  
  299. def Physics(thing):
  300. for i in range(len(Objects)):
  301. for j in range(-1,len(Objects[i])-1):
  302. temp = intersection(thing.pos, [thing.pos[0] + thing.speed[0]]+[thing.pos[1] + thing.speed[1]-1], Objects[i][j], Objects[i][j+1])
  303. if temp:
  304. thing.speed = temp
  305. thing.pos[0] = thing.pos[0] + thing.speed[0]
  306. thing.pos[1] = thing.pos[1] + thing.speed[1]
  307.  
  308. #Applies downward force (gravity)
  309. thing.speed[1] = thing.speed[1] - 1
  310.  
  311. ## #Bounce off the floor and ceiling
  312. ## if thing.pos[1] < -300:
  313. ## if thing.speed[1]<0 and thing.speed[1]>=-2:
  314. ## thing.speed[1] = 0
  315. ## else:
  316. ## print("Before:",thing.speed)
  317. ## thing.speed[1] = -1*((thing.speed[1]+1)*thing.elas)
  318. ## thing.pos[1] = -300
  319. ## print("After:",thing.speed)
  320. ##
  321. ## #Bounce off the walls
  322. ## if thing.pos[0] <= -500 or thing.pos[0] >= 500:
  323. ## thing.speed[0] = thing.speed[0]*-1
  324.  
  325. return
  326.  
  327. def drag(x,y):
  328. print(x,y)
  329. turtle3.clear()
  330. turtle.clear()
  331. Ball.speed[1] = randint(0,30)
  332. Ball.speed[0] = randint(-10,10)
  333. #Ball.speed[0] = 0
  334. Ball.elas = randint(0,1000)/1000
  335. turtle.penup()
  336. turtle.goto(0,0)
  337. turtle.pendown()
  338. Ball.pos = [0,0]
  339. return
  340.  
  341. #Walls()
  342. #The main loop
  343. while True:
  344. #1/60th of a second
  345. sleep(0.01666)
  346. #Physics(Ball)
  347. turtle.goto(Ball.pos)
  348. update()
  349. screen.onscreenclick(drag)
  350.  
  351.  
  352. #Exit Gracefully
  353. hideturtle()
  354. done()
Add Comment
Please, Sign In to add comment