Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import dom
  2. import html5_canvas
  3. import colors
  4. import math
  5.  
  6. proc requestAnimationFrame(function: proc()) {.inline.} =
  7. {.emit: ["requestAnimationFrame(", function, ");"].}
  8.  
  9. type
  10. Vec2d = ref object
  11. x, y: float
  12. Particle = object #of RootObj
  13. pos: Vec2d
  14. vel: Vec2d
  15. mass: float
  16.  
  17. proc updateForce(self: Particle, other: Particle) {.inline.} =
  18. let
  19. dx = other.pos.x - self.pos.x
  20. dy = other.pos.y - self.pos.y
  21. distSq = dx * dx + dy * dy
  22. dist = sqrt(distSq)
  23. F = self.mass * other.mass / distSq
  24. self.vel.x += dx / dist * F * other.mass
  25. self.vel.y += dy / dist * F * other.mass
  26. other.vel.x -= dx / dist * F * self.mass
  27. other.vel.y -= dy / dist * F * self.mass
  28.  
  29. proc update(self: Particle, ms: float) {.inline.} =
  30. self.pos.x += self.vel.x * ms
  31. self.pos.y += self.vel.y * ms
  32. var
  33. spaces: seq[Particle]
  34. let
  35. canvas = dom.document.getElementById("canvas-planet")
  36. ctx = canvas.Canvas.getContext2D()
  37.  
  38. proc drawTest(x: float, y: float, r: float) {.inline.} =
  39. ctx.fillStyle = "orange"
  40. #ctx.shadowBlur = 75
  41. ctx.beginPath()
  42. ctx.arc(x mod 1900, y mod 1000, r, 0, 2 * PI, false)
  43. ctx.fill()
  44.  
  45. proc uiLoop() =
  46. ctx.clearRect(0, 0, 1900, 1000)
  47. for i in 0..<spaces.len:
  48. for j in i+1..<spaces.len:
  49. updateForce(spaces[i], spaces[j])
  50. spaces[i].update(1)
  51. drawTest(spaces[i].pos.x, spaces[i].pos.y, 10.0)
  52. #paces[spaces.len - 1].update(1)
  53. #rawTest(spaces[spaces.len - 1].pos.x, spaces[spaces.len - 1].pos.y, 10.0)
  54. requestAnimationFrame(uiLoop)
  55. dom.window.onload = proc(e: dom.Event) =
  56. spaces.add(Particle(pos: Vec2d(x: 30.0, y: 30.0), vel: Vec2d(x: 1.0, y: 0.0), mass:10))
  57. spaces.add(Particle(pos: Vec2d(x: 200.0, y: 500.0), vel: Vec2d(x: 0.0, y: 0.0), mass:15))
  58. #spaces.add(Particle(pos: Vec2d(x: 900.0, y: 100.0), vel: Vec2d(x: -2.0, y: 0.2), mass:10))
  59. #spaces.add(Particle(pos: Vec2d(x: 1700.0, y: 800.0), vel: Vec2d(x: 0.0, y: 0.0), mass:25))
  60. echo "STARTED"
  61. uiLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement