Advertisement
Guest User

GDscript projectile motion - attempt 2

a guest
Mar 23rd, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. extends CharacterBody3D
  2.  
  3. var sensitivity := Vector2(0.2, 0.2)
  4.  
  5. @export var camera : Camera3D
  6. @export var debug : MeshInstance3D
  7. @onready var marker := $Marker3D
  8.  
  9. # Player related controls
  10. const SPEED = 5.0
  11. const JUMP_VELOCITY = 4.5
  12.  
  13. # Projectile prediction parameters
  14. const SHAPE_RAYCAST_RADIUS := 0.3
  15. const LINE_POINT_COUNT : int = 50
  16. const TIME_BETWEEN_POINTS : float = .45
  17.  
  18. const PROJECTILE_POWER = 8
  19. const STARTING_POSITION_MULTIPLIER = 5
  20.  
  21. # Get the gravity from the project settings to be synced with RigidBody nodes.
  22. var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
  23.  
  24.  
  25. func _physics_process(delta: float) -> void:
  26. calculate_projectile()
  27. # ... player movement code
  28.  
  29. var query = PhysicsShapeQueryParameters3D.new()
  30. func _ready() -> void:
  31. query.shape_rid = PhysicsServer3D.sphere_shape_create()
  32. query.exclude = [self]
  33. PhysicsServer3D.shape_set_data(query.shape_rid, SHAPE_RAYCAST_RADIUS)
  34.  
  35.  
  36. func calculate_projectile():
  37. # (v0=initial velocity Vector3, t=time/simulated time passed, and g=gravity)
  38. # x = v0.x * t * cos(theta),
  39. # y = v0.y * t * sin(theta) - 1/2 * g * t^2
  40. # z = v0.z * t * cos(theta),
  41.  
  42. var starting_pos : Vector3 = marker.position
  43. # If in 3rd person, translate camera.transform to player's transform + 1 to the y, then get the basis.z
  44. var v0 := camera.basis.z * PROJECTILE_POWER
  45. var theta := starting_pos.angle_to(v0)
  46.  
  47. print(starting_pos, " ",
  48. v0, " ",
  49. theta)
  50. var p : Vector3
  51. var time := 0.0
  52. for t in LINE_POINT_COUNT:
  53. # From a Unity tutorial which didn't work:
  54. # var new_point : Vector3 = starting_pos + (time * starting_velocity)
  55. # new_point.y = starting_pos.y + (starting_velocity.y * time) - (0.5 * gravity + pow(time, 2)
  56.  
  57. var new_point := Vector3(
  58. v0.x * t * cos(theta),
  59. v0.y * t * sin(theta) - 1/2 * -GRAVITY * pow(t, 2),
  60. v0.z * t * cos(theta)
  61. )
  62. p = new_point
  63. # As new_point is local to Player, we move the query's location by the players position + new_point
  64. query.transform = global_transform.translated_local(new_point)
  65.  
  66. # We use a query that's already been instantiated so we don't constantly recreate
  67. if not get_world_3d().direct_space_state.intersect_shape(query, 1).is_empty():
  68. # get the point where the shape first intersected. Should never be an empty dict.
  69. var collision := get_world_3d().direct_space_state.get_rest_info(query)
  70. if !collision.is_empty():
  71. debug.global_position = collision.point
  72. break
  73.  
  74. debug.global_position = p
  75. time += TIME_BETWEEN_POINTS
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement