CPU_phys.gd 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. extends Node2D
  2. var elem
  3. var iTime=0.0
  4. var static_obj=Array()
  5. var static_orb_radius=Array()
  6. # GDScript can not calculate too much loops look like
  7. # same logic on C can display 500+ body very easy on my CPU(60fps)
  8. # when GDScript can not more then 100
  9. var static_size=6
  10. var dyn_grav_size=10*1
  11. var dyn_size=10*6
  12. var dyn_nograv_size=10*2
  13. var mouse_block=false
  14. func _ready():
  15. # preload
  16. elem=preload("res://CPU_object.tscn")
  17. var iResolution=Vector2(1280,720)
  18. self.position=iResolution/2
  19. # static gravity objects
  20. spawn_1()
  21. spawn_2()
  22. spawn_3()
  23. spawn_4()
  24. get_node("../Control/Label").text+=str(static_size+dyn_grav_size+dyn_size+dyn_nograv_size)+" body"
  25. func _process(delta):
  26. iTime+=delta
  27. process_input(delta)
  28. process_static_move(delta)
  29. mouse_block=false
  30. func spawn_1():
  31. for a in range(static_size):
  32. var s=elem.instance()
  33. s.radius=512
  34. s.mass=10000
  35. s.is_static=true
  36. s.can_collide=true
  37. var an=PI/8*a
  38. static_orb_radius.append(600*a+25*a*a)
  39. s.position=(static_orb_radius[static_orb_radius.size()-1]*Vector2(sin(an),cos(an)))
  40. static_obj.append(s)
  41. self.call_deferred("add_child",s)
  42. func spawn_2():
  43. for a in range(dyn_grav_size):
  44. var s=elem.instance()
  45. s.radius=256
  46. s.mass=10000
  47. s.is_static=false
  48. s.can_collide=true
  49. s.react_gravity=false
  50. s.self_gravity=true
  51. s.bval=1
  52. var an=a*((PI*2)/dyn_grav_size)
  53. s.position=(800+200*a)*Vector2(sin(an),cos(an))
  54. self.call_deferred("add_child",s)
  55. func spawn_3():
  56. for a in range(dyn_size):
  57. var s=elem.instance()
  58. s.radius=64
  59. s.mass=1
  60. s.is_static=false
  61. s.can_collide=true
  62. s.self_gravity=false
  63. s.bval=0.98
  64. var an=a*((PI*2)/dyn_size)
  65. s.position=(650+10*a)*Vector2(sin(an),cos(an))
  66. self.call_deferred("add_child",s)
  67. func spawn_4():
  68. for a in range(dyn_nograv_size):
  69. var s=elem.instance()
  70. s.radius=32
  71. s.mass=1
  72. s.is_static=false
  73. s.can_collide=false
  74. s.self_gravity=false
  75. s.bval=0.98
  76. var an=a*((PI*2)/dyn_nograv_size)
  77. s.position=(750+10*a)*Vector2(sin(an),cos(an))
  78. self.call_deferred("add_child",s)
  79. func process_input(delta):
  80. if(Input.is_action_pressed("zoom_out")||(Input.is_action_pressed("l_click")&&(!mouse_block))):
  81. self.scale=self.scale+Vector2(delta*2.0,delta*2.0)*self.scale
  82. self.scale=Vector2(min(self.scale.x,1),min(self.scale.y,1))
  83. if(Input.is_action_pressed("zoom_in")||(Input.is_action_pressed("r_click")&&(!mouse_block))):
  84. self.scale=self.scale-Vector2(delta*2.0,delta*2.0)*self.scale
  85. self.scale=Vector2(max(self.scale.x,0.01),max(self.scale.y,0.01))
  86. func angle2d(c,e):
  87. var theta = atan2(e.y-c.y,e.x-c.x)
  88. return theta
  89. # move on circle
  90. func process_static_move(delta):
  91. for a in range(static_size):
  92. var angl=-PI/2+angle2d(Vector2(),static_obj[a].position)
  93. angl+=(0.81-0.7*smoothstep(0,static_size,a))*delta
  94. var tva=static_orb_radius[a]*Vector2(sin(-angl),cos(-angl))
  95. static_obj[a].heading=-static_obj[a].position+tva