• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How to draw a line based on an angle with a defined size?

I have a game where the player shoots a projectile based on the mouse direction when clicking, which travels 500 pixels.

I wish I could draw a line exactly where the projectile will be destroyed.

What I can do is draw a line from where the projectile is created to where the mouse is when it is clicked.

That is, the line may be longer or shorter than the path taken by the projectile.

The code is something like this:

Code:
draw_line(x,y,mouse_x,mouse_y);
Result in game:


645.png

Below an image explaining:

644.png

These blue dots would be the click of the mouse. That no matter where it went, the line drawn (green) would be the same (ending in pink x).

I know how to get the direction and how to calculate the distance, but I don't know how to implement it in line drawing.
 
C

Catastrophe

Guest
lengthdir_x
lengthdir_y

dir = point_direction(x,y,mouse_x,mouse_y)
draw_line(x,y,x + lengthdir_x(500,dir), y+ lengthdir_y(500,dir))

should do the trick
 

Yal

🐧 *penguin noises*
GMC Elder
lengthdir_x
lengthdir_y

dir = point_direction(x,y,mouse_x,mouse_y)
draw_line(x,y,x + lengthdir_x(500,dir), y+ lengthdir_y(500,dir))

should do the trick
It might look more natural to end the line at mouse coordinates if it's less than 500, so it's effectively capped off at 500 but will otherwise go to the mouse but not any longer:
dist = point_distance(x,y,mouse_x,mouse_y)
if(dist > 500){
dir = point_direction(x,y,mouse_x,mouse_y)
draw_line(x,y,x + lengthdir_x(500,dir), y+ lengthdir_y(500,dir))
}
else{
draw_line(x,y,mouse_x,mouse_y)
}

(You might wanna replace all the "500"s with the name of the variable you store the distance in, of course... just using a 500 here since that's what the distance is in your gorgeous explanatory graph)
 
Last edited:

CloseRange

Member
It might look more natural to end the line at mouse coordinates if it's less than 500, so it's effectively capped off at 500 but will otherwise go to the mouse but not any longer:
dist = point_distance(x,y,mouse_x,mouse_y)
if(dist > 500){
dir = point_direction(x,y,mouse_x,mouse_y)
draw_line(x,y,x + lengthdir_x(500,dir), y+ lengthdir_y(500,dir))
}
else{
draw_line(x,y,mouse_x,mouse_y)
}
it would look much better but neither are exactly what they asked. He wants the point where the projectile is destroyed. This means at most 500 pixels away and at least when it hits an object. That means implementing ray casting, now's the time where I put in my code on how to do that:
Code:
nah, I'm too lazy
 
C

Catastrophe

Guest
xD Well TBF he didn't strictly ask for that, he just talked about effective range.

In that case, you'll need to do collision_line_list (ordered set to true) on anything that your bullet can collide with and then just the line to the first thing. Easy if everything a bullet collides with is parented to a single object, annoying if not.
 

Yal

🐧 *penguin noises*
GMC Elder
He does say this at the very end of the post, though.
I know how to get the direction and how to calculate the distance, but I don't know how to implement it in line drawing.
I suppose clamping the line to 500 instead of a variable was taking the images too literally (I'll go fix that now), but he's actually asking about only the line-drawing bit.
 
Using trigonometry I was able to solve my problem.

Calcle:

Code:
seno=dsin(point_direction(x,y,mouse_x,mouse_y));
cose=dcos(point_direction(x,y,mouse_x,mouse_y));
Draw:

Code:
draw_line(x,y,x+(500*cose),y+(-500*seno));
 

Joe Ellis

Member
You can just use a vector instead of needing to deal with angles\trigonometry:

Code:
var d = 500 / point_distance(x, y, mouse_x, mouse_y);

draw_line(x, y, x +( (mouse_x - x) * d), y + ((mouse_y - y) * d)
The vector is: mouse_pos - pos

Then you can get the normal by normalizing it (dividing the vector by the length\distance of it)

Then multiply it by 500 or however long you want the line to be.

(The 500 / point_distance part is shortening the process, cus normally it would be 1 / length of vector, but would then get multiplied by 500 after, so you can just do it then and there instead)
 
Top