Jump to content


Photo

Problem With Drawing Line Based On Direction


  • Please log in to reply
2 replies to this topic

#1 Nijg

Nijg

    Caleb Cuzner

  • GMC Member
  • 1118 posts
  • Version:GM6

Posted 14 August 2009 - 04:16 PM

I am trying to draw a line that takes the direction from one point to another, and then draws a line in that direction, with the line being 800 pixels long. Here is the basic code of what I have tried:
draw_line(Protagonist.x,Protagonist.y,Protagonist.x+800*
arccos(point_direction(Protagonist.x,Protagonist.y,Prot_aimer.x,Prot_
aimer.y)),Protagonist.y+800*arcsin(point_direction(Protagonist.x,Protagonist.y,Prot_aimer.x,Prot_
aimer.y)));
This is trying to draw the line from Protagonist's origin in the direction of Prot_aimer's orgin, extending for 800 pixels. Prot_aimer is a moving object so its origin varies, but its direction from Protagonist is always between 0 degrees and 180 degrees.

Whenever I use this code, the line is 800 pixels long as planned, but it is always going in the wrong direction, usually favoring towards the bottom left. I have tried switching the Protagonist coordinates and Prot_aimer coordinates with each other in point_direction. I have tried adding 90, adding 180, or subtracting 180 from the direction. I have tried switching the directions to negatives. I have tried using if statements declaring if the direction is supposed to be generally towards the left or generally towards the right. I have tried making the length shorter. None of these fixed the direction problem, and they usually ended up in the direction being towards the bottom left.

I have searched if there is code to do this on GMLscripts.com, and could not find any. Please help if you can.

Edited by Nijg, 14 August 2009 - 05:52 PM.

  • 0

#2 turgon

turgon

    VP Studios

  • GMC Member
  • 444 posts
  • Version:GM:Studio

Posted 14 August 2009 - 04:23 PM

Perhaps I'm misunderstanding your question, but from what I understand, you're looking for something like this:

the_direction=point_direction(Protagonist.x,Protag
onist.y,Prot_aimer.x,Prot_aimer.y)

draw_line(Protagonist.x,Protagonist.y,Protagonist.
x+leng
thdir_x(800,the_direction),
Protagonist.y+lengthdir_y(800,the_direction))

Good luck; I hope this helps!

Edited by turgon, 14 August 2009 - 04:25 PM.

  • 0

#3 Nijg

Nijg

    Caleb Cuzner

  • GMC Member
  • 1118 posts
  • Version:GM6

Posted 14 August 2009 - 05:41 PM

Wow, I can't believe I overlooked that lengthdir stuff in the help file.

All this time I have been trying to use trigonometry to figure out that type of stuff, and all along there was a built-in function that could do it for me...

Well it works now, thanks for pointing that out.

Edited by Nijg, 14 August 2009 - 05:41 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users