Math help!

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Ripe
Prole
Posts: 11
Joined: Wed Nov 19, 2008 3:28 pm
Location: Australia

Math help!

Post 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?
User avatar
Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

Re: Math help!

Post 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)
Ripe
Prole
Posts: 11
Joined: Wed Nov 19, 2008 3:28 pm
Location: Australia

Re: Math help!

Post by Ripe »

Thank you! Works perfectly!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], zingo and 0 guests