Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## how to use:
- ## Create a new 2d scene. Add a camera centered at zero with a zoom of (0.05, 0.05)
- ## Attach either script to the root node
- ## Set the profiler to autostart (in the debugger panel at the bottom of the editor)
- ## Run the scene and look at the "Physics time" value to compare both methods
- ## The Raycast2D node benching script
- extends Node2D
- @export var num_static_bodies := 10000
- @export var num_raycast_nodes := 100000
- @export var world_size := Vector2(10000.0, 10000.0)
- var _raycasts: Array[RayCast2D] = []
- var collision_positions: Array[Vector2] = []
- func _ready() -> void:
- _spawn_static_bodies()
- _spawn_raycast_nodes()
- func _spawn_static_bodies() -> void:
- for _i in num_static_bodies:
- var body := StaticBody2D.new()
- var col := CollisionShape2D.new()
- var rect := RectangleShape2D.new()
- col.shape = rect
- rect.size = Vector2.ONE * 64.0
- body.add_child(col)
- var sprite := Sprite2D.new()
- var gradient_2d := GradientTexture2D.new()
- gradient_2d.gradient = Gradient.new()
- sprite.texture = gradient_2d
- body.add_child(sprite)
- add_child(body)
- body.collision_layer = 1
- body.position = Vector2(randf() * world_size.x, randf() * world_size.y) - world_size / 2.0
- func _spawn_raycast_nodes():
- for _i in num_raycast_nodes:
- var rc := RayCast2D.new()
- _raycasts.push_back(rc)
- add_child(rc)
- rc.collision_mask = 1
- rc.position = Vector2(randf() * world_size.x, randf() * world_size.y)
- rc.target_position = Vector2.DOWN.rotated(randf() * TAU)
- func _physics_process(_delta: float) -> void:
- collision_positions.clear()
- collision_positions.resize(_raycasts.size())
- for i in _raycasts.size():
- var rc := _raycasts[i]
- if rc.is_colliding():
- collision_positions[i] = rc.get_collision_point()
- else:
- collision_positions[i] = Vector2.ZERO
- ## The direct space state intersect query script
- extends Node2D
- @export var num_static_bodies := 10000
- @export var num_raycasts_per_frame := 100000
- @export var world_size := Vector2(10000.0, 10000.0)
- var collision_positions: Array[Vector2] = []
- func _ready() -> void:
- _spawn_static_bodies()
- func _spawn_static_bodies() -> void:
- for _i in num_static_bodies:
- var body := StaticBody2D.new()
- var col := CollisionShape2D.new()
- var rect := RectangleShape2D.new()
- col.shape = rect
- rect.size = Vector2.ONE * 64.0
- body.add_child(col)
- var sprite := Sprite2D.new()
- var gradient_2d := GradientTexture2D.new()
- gradient_2d.gradient = Gradient.new()
- sprite.texture = gradient_2d
- body.add_child(sprite)
- add_child(body)
- body.collision_layer = 1
- body.position = Vector2(randf() * world_size.x, randf() * world_size.y) - world_size / 2.0
- func _physics_process(_delta: float) -> void:
- var direct_space_state := get_world_2d().direct_space_state
- collision_positions.clear()
- collision_positions.resize(num_raycasts_per_frame)
- for i in num_raycasts_per_frame:
- var start_pos :=Vector2(randf() * world_size.x, randf() * world_size.y)
- var end_pos := start_pos + Vector2.DOWN.rotated(randf() * TAU)
- var query := PhysicsRayQueryParameters2D.create(start_pos, end_pos, 1)
- var result := direct_space_state.intersect_ray(query)
- if result:
- collision_positions[i] = result.position
Advertisement
Add Comment
Please, Sign In to add comment