Page 1 of 1

Math help!

Posted: Fri Nov 21, 2008 11:02 am
by Ripe
Hello guys! I've been playing around with love for a couple of days now I've came into my first problem and I'm hoping someone could help me out. I'm trying to create a function that will draw a line from a given x, y position on set angle for a certain distance. I would imagine it'll look something like this;

Code: Select all

function draw_line_at_angle(x, y, angle, length)
  <calculation>
  love.graphics.line(x, y, xx, yy)
end
I tried doing a sort of right angle triangle calculation by using length as c and angle as A but it didn't work out to well. Does anyone have any ideas?

Re: Math help!

Posted: Fri Nov 21, 2008 11:48 am
by Kaze
Ripe wrote:Hello guys! I've been playing around with love for a couple of days now I've came into my first problem and I'm hoping someone could help me out. I'm trying to create a function that will draw a line from a given x, y position on set angle for a certain distance. I would imagine it'll look something like this;

Code: Select all

function draw_line_at_angle(x, y, angle, length)
  <calculation>
  love.graphics.line(x, y, xx, yy)
end
I tried doing a sort of right angle triangle calculation by using length as c and angle as A but it didn't work out to well. Does anyone have any ideas?
Trigonometry!

Code: Select all

local xx = x + math.cos(math.rad(angle)) * length -- math.rad converts degrees to radians
local yy = y + math.sin(math.rad(angle)) * length
love.graphics.line(x, y, xx, yy)

Re: Math help!

Posted: Fri Nov 21, 2008 12:07 pm
by Ripe
Thank you! Works perfectly!