Advertisement
Guest User

position_of_Area_Godot

a guest
Mar 6th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. # SpaceCell.gd
  2. extends Area
  3.  
  4. var contacts = {}
  5. var force = Vector3(0,0,0) # the force that acts on the cell
  6. var velocity = Vector3(0,0,0) # the velocity of the cell
  7.  
  8. const FORCE_K = 1 # the constant k in F=kx between the cells
  9. const REST_DISTANCE = 1 # the distance between two cells at which the cells are at rest
  10.  
  11. func update_force():
  12. # updates the force that acts on a cell i from all other cells j
  13. # var pos_i = get_pos() # <------- THE PROBLEM
  14. # var pos_i = position # <------- THE PROBLEM
  15. var pos_i = self.position # <------- THE PROBLEM
  16. for cell_j in contacts:
  17. var pos_j = cell_j.position # <------- THE PROBLEM
  18. var x = pos_i - pos_j
  19. var x0 = x.normalized()*REST_DISTANCE
  20. var force_ij = FORCE_K*(x - x0)
  21. force += force_ij
  22.  
  23. func update_velocity(delta):
  24. velocity += force * delta
  25.  
  26. func update_position(delta):
  27. self.position = self.position + velocity*delta
  28.  
  29. func _physics_process(delta):
  30. update_force()
  31. update_velocity(delta)
  32. update_position(delta)
  33.  
  34. func area_entered(area):
  35. contacts[area.cellname] = area
  36.  
  37. func area_exited(area):
  38. contacts.erase(area.cellname)
  39.  
  40.  
  41. # ERROR OUTPUT:
  42. # Invalid get index 'position' (on base: 'Area (SpaceCell.gd)').
  43.  
  44. # SpaceCell SCENE SETUP:
  45. # SpaceCell [Area] <----- Script attached called SpaceCell.gd, see above
  46. # ---- CollisionShape (shaped like sphere)
  47. # ---- ---- CSGMesh (shaped like sphere)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement