0
\$\begingroup\$

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:

draw_line(x,y,mouse_x,mouse_y);

Result in game:

enter image description here

Below an image explaining:

enter image description here

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.

\$\endgroup\$
0
\$\begingroup\$

Just use trigonometry. Sine and cosine. Just get the angle/direction between the object and the mouse click.

Click Event:

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

Draw Event:

draw_line(x,y,x+(500*cose),y+(-500*seno));
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.