Advertisement
Guest User

Untitled

a guest
Jun 25th, 2021
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.26 KB | None | 0 0
  1. function love.load()
  2. --Object = require "classic"
  3. require "cjrcle"
  4. windowwidth = 1000
  5. windowheight = 1000
  6. love.graphics.setBackgroundColor(0, 0, 0)
  7. sucess = love.window.setMode(windowwidth, windowheight)
  8.  
  9. ctheta = math.pi/2
  10. x = 0
  11. y = 0
  12. b = math.pi/ctheta
  13.  
  14. sinxloc = 0
  15. startposX = 0
  16. discr = 50
  17. hr = 10
  18.  
  19. speed = 1
  20.  
  21. thrown = false -- a variable to check if the hammer has been let go
  22.  
  23. -- hammer position
  24. hxpos = startposX + discr*math.cos(math.pi/2)/math.pi
  25. hypos = y - discr*math.sin(math.pi/2)
  26. nxpos = hxpos
  27. nypos = hypos
  28. rv = 1 -- rotational velocity
  29.  
  30. --m = -hxpos/math.sqrt(50*50 - hxpos*hxpos)/discr
  31. --dYdX = -hxpos/math.sqrt(discr*discr - hxpos*hxpos)/discr
  32. dYdX = ctheta + math.pi/2
  33. end
  34.  
  35. --[[
  36. equation of tangent line y = -x + 50*math.sqrt(2)
  37. ]]
  38.  
  39. function love.keypressed(key)
  40. if key == "space" then
  41. thrown = true
  42. end
  43. if key == "r" then
  44. b = math.pi/2
  45. end
  46. end
  47.  
  48. function love.update(dt)
  49. --[[
  50. user sialek suggestion
  51. you never update dYdX beyond the initial value
  52. it's only at the end of the love.load() function
  53. putting the formula you have in the update function doesn't work either
  54. if you set it to a fixed value that it does actually go in some other direction
  55. i think you need to add a line to update dYdZ in your actual update function and rework how you're calculating it
  56. the one up top is using discr, which never changes, so that's probably why it doesn't help in the update() function
  57.  
  58. maccraft123 suggestion
  59. try representing the position of the hammer in polar coordinates
  60. for rendering it convert it to cartesian
  61. ]]
  62.  
  63. ctheta = ctheta - rv*math.pi*dt/30 -- change in theta
  64. --ntheta = ctheta -- theta now
  65. if ctheta < ctheta - 2*math.pi then
  66. ctheta = math.pi/2
  67. end
  68.  
  69. if not thrown then
  70. ctheta = ctheta -rv*math.pi*dt/30
  71. hxpos = startposX + discr*math.cos(ctheta) -- this snippet of code calculates the hammer position for the next frame... dX/dY
  72. hypos = y - discr*math.sin(ctheta)
  73. ntheta = ctheta -- keep track of the current value of the change is theta with the current value of theta
  74. end
  75. if thrown == true then -- the tangent line is π/2 radians from ctheta
  76. ctheta = ntheta
  77. index = 0
  78. -- trying to get the hammer to go in a straight line from it's current position to 1/cos(ctheta)
  79. if index%2 == 0 then
  80. if hxpos < windowwidth/2 and hypos < windowheight/2 and hxpos > -1*windowwidth/2 and hypos > -1*windowheight/2 then
  81. hxpos = hxpos + 1
  82. --hypos = hypos - dYdX
  83.  
  84. --if hxpos < discr/math.cos(ctheta) then -- if the x coordinate of the hammer is less than the secant of ctheta
  85. --hxpos = hxpos + math.tan(ntheta)
  86. --hypos = hypos/hxpos
  87. hypos = hypos + dYdX -- hypos will never reach 0 because the cot(ctheta) will never be 0
  88. --hypos = hypos + math.tan(ntheta)
  89. end
  90. end
  91. if index%2 ~= 0 then
  92. --if hxpos < discr/math.sin(ctheta) then -- if the x coordinate of the hammer is less than the cosecant of ctheta
  93. if hxpos < windowwidth/2 or hxpos > -windowwidth/2 then
  94. hxpos = hxpos + 1
  95. hypos = hypos + dYdX
  96. --hypos = hxpos + hypos/hxpos
  97. index = index + 1
  98. -- y never gets to 1 because the tangent is never
  99. end
  100. end
  101.  
  102. --[[
  103. hxpos = hxpos + 50*dt*10*math.cos(ntheta)
  104. hypos = hxpos*hxpos/math.sqrt(50^2 + hxpos*hxpos) - 50*math.cos(ntheta) - hypos-- + hxpos -- hypos is now equal to dx/dy at ntheta
  105. cjrcle(10, hxpos, hypos)
  106. ]]
  107. end
  108.  
  109. local m = math.pow(2, dt)
  110. if love.keyboard.isDown("up") then
  111. b = b*m --the period of the sine wave is pi/delthe... i think?
  112. end
  113. if love.keyboard.isDown("down") then
  114. b = b/m
  115. end
  116. end
  117.  
  118.  
  119. function arccirc(r, x, y, delthe)
  120. arcang = math.atan(y + r/x + r)
  121. for theta = 0, arcang, delthe do
  122. love.graphics.line(discr*math.cos(theta) + x, discr*math.sin(theta) + y, discr*math.cos(theta + delthe) + x, discr*math.sin(theta + delthe) + y)
  123. end
  124. end
  125.  
  126. function love.draw()
  127.  
  128. love.graphics.translate(windowwidth/2, windowheight/2)
  129. love.graphics.setColor(1, 1, 0)
  130. love.graphics.print("sine = " .. -hypos/50, -400, -400)
  131. love.graphics.setColor(1, 0, 0)
  132. love.graphics.print("cosine = " .. hxpos/50, -400, -390)
  133. love.graphics.setColor(0, 0, 1)
  134. love.graphics.print("tangent = " .. math.tan(ctheta), -400, -380)
  135. love.graphics.setColor(1,.6,.5)
  136. love.graphics.print("cotangent = " .. 1/math.tan(ctheta),-400, -370)
  137. love.graphics.setColor(0,1,0)
  138. love.graphics.print("secant = " .. 1/math.cos(ctheta), -400, -360)
  139. love.graphics.setColor(1,0,1)
  140. love.graphics.print("cosecant = " .. 1/math.sin(ctheta) , -400, -350)
  141. love.graphics.setColor(1,1,1)
  142. love.graphics.print("b: " .. b, -400, -340)
  143. love.graphics.print("Ø: ".. ctheta .. " radians, or ", -400, -330 )
  144. love.graphics.print("which is also " .. ctheta*180/math.pi .. " degrees!", -400, -300)
  145. love.graphics.print("the derrivative of the quation X^2 + Y^2 = r^2 @ r = 50: ", -400, -280)
  146. love.graphics.print("dY/dX = -x^2/(math.sqrt(50^2 - x) + 2500) = " .. -hxpos*hxpos/(math.sqrt(discr*discr - hxpos) + 2500), -400, -260)
  147. love.graphics.print("tangent line has equation: ", -400, -240)
  148. love.graphics.print("Y = mX + b", -400, -220)
  149.  
  150. love.graphics.print("Y = " .. dYdX .. "*" .. hxpos .. " + B", -400, -200)
  151. love.graphics.print("B = " .. -dYdX, -400, -190)
  152.  
  153. love.graphics.print("Tangent line has equation:", -400, -150)
  154.  
  155. love.graphics.print("Y = " .. dYdX .. "X + " .. -dYdX, -400, -130)
  156.  
  157. love.graphics.print("The value of thrown is: ", -400, -120)
  158. if thrown then
  159. love.graphics.print("thrown!", -200, -120)
  160. else
  161. love.graphics.print("not thrown:(", -200, -120) -- thrown not being updated:()
  162. end
  163.  
  164.  
  165. -- orange cjrcle
  166.  
  167. love.graphics.setColor(.7, .6, .3)
  168. cjrcle(discr, startposX, 0, 32)
  169.  
  170. -- blue cjrcle for player
  171. love.graphics.setColor(1, 1, 1)
  172. cjrcle(10, hxpos, hypos)
  173.  
  174.  
  175. -- violet tangent and cotantgent lines
  176. --orange cotangent...
  177. love.graphics.setColor(1,.6,.5)
  178. love.graphics.line(50*math.cos(ctheta), -50*math.sin(ctheta), 0, -50/math.sin(ctheta))
  179. -- blue tangent
  180. love.graphics.setColor(0, 0, 1)
  181. love.graphics.line(50*math.cos(ctheta), -50*math.sin(ctheta), 50/math.cos(ctheta), 0)
  182.  
  183. --grey continued gray tangent line
  184. love.graphics.setColor(.5,.5,.5)
  185. love.graphics.line(discr/math.cos(ctheta), 0, windowwidth/2, windowwidth/2*(math.sqrt(discr*discr - hxpos*hxpos))/50)
  186.  
  187. love.graphics.setColor(.8,.8,.8)
  188. love.graphics.line(discr/math.cos(ctheta), 0, windowwidth/2*(math.sqrt(discr*discr - hxpos*hxpos))/discr, windowwidth/2)
  189.  
  190. -- secant and cosecant
  191. love.graphics.setColor(1, 0, 1)
  192. love.graphics.line(0,0,0,-50/math.sin(ctheta)) --csc
  193. love.graphics.setColor(1, 1, .1)
  194. love.graphics.setColor(0, 1, 0)
  195. love.graphics.line(0,0,50/math.cos(ctheta),0) --sec
  196.  
  197. --different triangle
  198.  
  199. --red horizontal cosine line
  200. love.graphics.setColor(1, 0, 0)
  201. love.graphics.line(0, 0, hxpos, 0)
  202.  
  203. -- yellow vertical sine line
  204. love.graphics.setColor(1, 1, 0)
  205. love.graphics.line(hxpos, hypos, 50*math.cos(ctheta), 0)
  206.  
  207.  
  208. -- radius
  209. love.graphics.setColor(.5, .3, 0)
  210. love.graphics.line(0, 0, hxpos, hypos)
  211.  
  212.  
  213.  
  214.  
  215. --anglethetaprints
  216.  
  217. love.graphics.print("Ø",hxpos/2, hypos)
  218. for thetarc = math.atan2(hypos,hxpos), math.sin(ctheta), -math.pi/60 do
  219.  
  220. love.graphics.setColor(0,1,0)
  221. if hxpos > 0 and hypos > 0 then
  222. if thetarc <= math.pi/2 and thetarc > 0 then
  223. love.graphics.line(10*math.cos(thetarc), 10*math.sin(thetarc), 10*math.cos(thetarc + math.pi/60), 10*math.sin(thetarc + math.pi/60))
  224. thetarc = thetarc + math.pi/60
  225. --love.graphics.print("Ø",hxpos/2, hypos/2)
  226. end
  227. end
  228.  
  229. if hxpos < 0 and hypos > 0 then
  230. if thetarc <= 0 and thetarc > -math.pi/2 then
  231. love.graphics.line(-10*math.cos(thetarc), -10*math.sin(thetarc), -10*math.cos(thetarc + math.pi/60), -10*math.sin(thetarc + math.pi/60))
  232. thetarc = thetarc + math.pi/60
  233. --love.graphics.print("Ø",hxpos/2, hypos/2)
  234. end
  235. end
  236.  
  237. end
  238.  
  239. --sinecurve
  240.  
  241. sinxloc = 50
  242. cosyloc = 50
  243. love.graphics.setColor(1, 0, 0.4)
  244. for theta = -32*math.pi, 32*math.pi, math.pi/180 do
  245. love.graphics.line(25 + sinxloc, -50*math.sin(b*theta), 25 + sinxloc + math.pi/360, -50*math.sin(b*theta + math.pi/360))
  246. sinxloc = sinxloc + 1
  247. cosyloc = cosyloc + 1
  248. end
  249.  
  250.  
  251. love.graphics.setColor(1,0,0)
  252.  
  253.  
  254. love.graphics.print("x: " .. hxpos/50, hxpos - 50, hypos)
  255. love.graphics.print("y: " .. -hypos/50, hxpos - 50, hypos+10)
  256.  
  257. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement