0 votes

I do know that you can use Physics2DDirectSpaceState.intersect_ray() to check if there are collisions between two points. But is there a way so that the 2nd vector will extend until it hits something?

in Engine by (19 points)

1 Answer

0 votes
Best answer

If you mean that you need to have an infinite ray cast, you can simulate it by casting a very long segment, you'll need to calculate a normalized direction vector first:

extends Node2D

var direction = Vector2()
var length = 0


func _physics_process(_delta):
    # Simulate infinite ray cast.
    # In this case, we derive the size from the viewport size.
    length = get_viewport().size.length_squared()

    var from = $character.global_position
    var to = get_global_mouse_position()

    # Returns normalized direction vector.
    direction = from.direction_to(to)

    # Extend the ray in the given direction.
    # Also convert to global coordinates.
    to = (direction * length) + from

    # "Infinite" ray cast!
    var space = get_world_2d().direct_space_state
    var result = space.intersect_ray(from, to)
    var collided = not result.empty()
    print(collided)

    update()


func _draw():
    # For debugging purposes.
    var from = $character.global_position
    var to = $character.global_position + (direction * length)
    draw_line(from, to, Color.green)

But bear in mind that casting long vectors like this every frame can severely impact the performance. Instead of calculating the viewport size, you can also base this on maximum level size, for instance.

Alternatively, you can also "grow" the length of such a ray cast dynamically (from zero length to level size length, with some optimal step) and do multiple queries on each increase in length, if you find this could improve performance.

by (1,356 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.