This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

Hi all !

I need help to solve a vector 2D problem. In my project, a 2D isometric game, I'm trying to implement the gun fire logic. The player aims using his mouse cursor in a way that when he shoots, the "bullet" follow a line that starts from his position and ends on the mouse position.

For that I use Raycasting technique as explained in and the method Dictionary intersect_ray ( Vector2 from, Vector2 to, Array exclude=[ ], int collision_layer=2147483647 )

For the moment, it works very well, collisions are detected correctly but only between the player position and the mouse position. What I want instead is to find the coordinates of the intersection point between the "line" formed by the player position, the cursor position and the map boundaries. So the second point that I'll pass to intersect_ray will be located somewhere on the boundaries of the map. In this way the collisions will be checked from the player position and until the line reach the map boundaries.

Just to be sure you can check the images bellow to clarify what I'm talking about. Just imagine that the image borders are the map boundaries.

this is what I have currently

this is what I want

I don't know how to approach the problem. Have you idea ? Do you have any ideas ?

in Engine by (22 points)
retagged by

1 Answer

+1 vote
Best answer


For defining the position of end of the RayCast, you need angle and magnitude. So you could get the angle like this (assuming you have the origin position stored in variable from):

var angle = from.angle_to_point(get_global_mouse_position())

Then you can define the ending position by making a looong vector with that angle, and summing it to from vector, like this:

var to = (from + Vector(5000,0).rotated(angle)) #adjust 5000 to maximum distance possible between to points in map

Then just raycast from from to end

This is as much i can imagine without code. If you share code or project maybe i or someone can help you more.

by (3,505 points)
selected by

Thank a lot for your answer ! Your suggestion works well. I just had to subtract 180 degree from the angle because the produced vector was at the opposite side
var to = global_position + Vector2(5000,0).rotated(angle - deg2rad(180))

About my code I will try to post the relevant part.

You are welcome. Is the issue is solved, there is no need to post the code. You may also select the answer if it worked so others can see its solved.

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 Frequently asked questions and 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 [email protected] with your username.