Guest User

Godot Raycast2D vs direct space state raycasts benchmark script

a guest
Oct 1st, 2025
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 3.22 KB | Source Code | 0 0
  1. ## how to use:
  2. ## Create a new 2d scene. Add a camera centered at zero with a zoom of (0.05, 0.05)
  3. ## Attach either script to the root node
  4. ## Set the profiler to autostart (in the debugger panel at the bottom of the editor)
  5. ## Run the scene and look at the "Physics time" value to compare both methods
  6.  
  7. ## The Raycast2D node benching script
  8. extends Node2D
  9.  
  10. @export var num_static_bodies := 10000
  11. @export var num_raycast_nodes := 100000
  12. @export var world_size := Vector2(10000.0, 10000.0)
  13.  
  14.  
  15. var _raycasts: Array[RayCast2D] = []
  16. var collision_positions: Array[Vector2] = []
  17.  
  18. func _ready() -> void:
  19.     _spawn_static_bodies()
  20.     _spawn_raycast_nodes()
  21.  
  22.  
  23. func _spawn_static_bodies() -> void:
  24.     for _i in num_static_bodies:
  25.         var body := StaticBody2D.new()
  26.         var col := CollisionShape2D.new()
  27.         var rect := RectangleShape2D.new()
  28.         col.shape = rect
  29.         rect.size = Vector2.ONE * 64.0
  30.         body.add_child(col)
  31.         var sprite := Sprite2D.new()
  32.         var gradient_2d := GradientTexture2D.new()
  33.         gradient_2d.gradient = Gradient.new()
  34.         sprite.texture = gradient_2d
  35.         body.add_child(sprite)
  36.         add_child(body)
  37.         body.collision_layer = 1
  38.         body.position = Vector2(randf() * world_size.x, randf() * world_size.y) - world_size / 2.0
  39.  
  40.  
  41. func _spawn_raycast_nodes():
  42.     for _i in num_raycast_nodes:
  43.         var rc := RayCast2D.new()
  44.         _raycasts.push_back(rc)
  45.         add_child(rc)
  46.         rc.collision_mask = 1
  47.         rc.position = Vector2(randf() * world_size.x, randf() * world_size.y)
  48.         rc.target_position = Vector2.DOWN.rotated(randf() * TAU)
  49.  
  50.  
  51. func _physics_process(_delta: float) -> void:
  52.     collision_positions.clear()
  53.     collision_positions.resize(_raycasts.size())
  54.     for i in _raycasts.size():
  55.         var rc := _raycasts[i]
  56.         if rc.is_colliding():
  57.             collision_positions[i] = rc.get_collision_point()
  58.         else:
  59.             collision_positions[i] = Vector2.ZERO
  60.  
  61.  
  62.  
  63. ## The direct space state intersect query script
  64. extends Node2D
  65.  
  66. @export var num_static_bodies := 10000
  67. @export var num_raycasts_per_frame := 100000
  68. @export var world_size := Vector2(10000.0, 10000.0)
  69.  
  70. var collision_positions: Array[Vector2] = []
  71.  
  72. func _ready() -> void:
  73.     _spawn_static_bodies()
  74.  
  75.  
  76. func _spawn_static_bodies() -> void:
  77.     for _i in num_static_bodies:
  78.         var body := StaticBody2D.new()
  79.         var col := CollisionShape2D.new()
  80.         var rect := RectangleShape2D.new()
  81.         col.shape = rect
  82.         rect.size = Vector2.ONE * 64.0
  83.         body.add_child(col)
  84.         var sprite := Sprite2D.new()
  85.         var gradient_2d := GradientTexture2D.new()
  86.         gradient_2d.gradient = Gradient.new()
  87.         sprite.texture = gradient_2d
  88.         body.add_child(sprite)
  89.         add_child(body)
  90.         body.collision_layer = 1
  91.         body.position = Vector2(randf() * world_size.x, randf() * world_size.y) - world_size / 2.0
  92.  
  93.  
  94. func _physics_process(_delta: float) -> void:
  95.     var direct_space_state := get_world_2d().direct_space_state
  96.     collision_positions.clear()
  97.     collision_positions.resize(num_raycasts_per_frame)
  98.     for i in num_raycasts_per_frame:
  99.         var start_pos :=Vector2(randf() * world_size.x, randf() * world_size.y)
  100.         var end_pos := start_pos + Vector2.DOWN.rotated(randf() * TAU)
  101.         var query := PhysicsRayQueryParameters2D.create(start_pos, end_pos, 1)
  102.         var result := direct_space_state.intersect_ray(query)
  103.         if result:
  104.             collision_positions[i] = result.position
Tags: Godot Gdscript
Advertisement
Add Comment
Please, Sign In to add comment