How is sin and cos work here?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
sunsh1ne
Prole
Posts: 4
Joined: Mon Feb 03, 2025 9:48 pm

How is sin and cos work here?

Post by sunsh1ne »

Hello, everyone, I'm new to love2d and for game development, but recently I started to following this tutorial and even understood many of things. But today I began read 16 chapter and faced with cosine and sine. I don't know about trigonometry(already began to learn), and part, when we using them to move our circle to cursor seems too hard for me. I can't just understand how is it really works(how can cosine and sine help with moving circle to right position). But I really want. So, I spend all evening and after all decided to ask here. Can you help me with materials to learn from, or maybe you can explain me somehow? I'd be very grateful.
User avatar
BrotSagtMist
Party member
Posts: 680
Joined: Fri Aug 06, 2021 10:30 pm

Re: How is sin and cos work here?

Post by BrotSagtMist »

This page is one of the best explanations of how geometry works youll find on the entire internet.
If that doesnt help, nothing else can.

But here is the thing that the tutorial doesnt tell you: you dont need all that math, this is game dev, not engineering.
If you just slowly adjust the x/y values of the circle towards the mouse each frame you get more or less the same movements with just basic + - * math.
obey
User avatar
dusoft
Party member
Posts: 765
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How is sin and cos work here?

Post by dusoft »

sunsh1ne wrote: Mon Feb 03, 2025 10:03 pm Hello, everyone, I'm new to love2d and for game development, but recently I started to following this tutorial and even understood many of things. But today I began read 16 chapter and faced with cosine and sine. I don't know about trigonometry(already began to learn), and part, when we using them to move our circle to cursor seems too hard for me. I can't just understand how is it really works(how can cosine and sine help with moving circle to right position). But I really want. So, I spend all evening and after all decided to ask here. Can you help me with materials to learn from, or maybe you can explain me somehow? I'd be very grateful.
Hello,

you can use these functions to do most of the calculations you need for angles, distances and such:

Code: Select all

-- angle between two points / vectors
function math.angle(x1, y1, x2, y2)
    return math.round(math.deg(math.atan2(y2 - y1, x2 - x1)))
end

-- find point from x,y in distance length under angle
function math.distancePoint(x, y, length, angle)
    local new_x = math.round(x + length * math.cos(math.rad(angle)))
    local new_y = math.round(y + length * math.sin(math.rad(angle)))
    return new_x, new_y
end

-- distance between two points
function math.distance(x1, y1, x2, y2)
    return math.round(math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2))
end

-- clamp angle to 0-360 degrees
function math.clampAngle(angle)
    angle = angle % 360
    return math.round(angle)
end
sunsh1ne
Prole
Posts: 4
Joined: Mon Feb 03, 2025 9:48 pm

Re: How is sin and cos work here?

Post by sunsh1ne »

BrotSagtMist wrote: Tue Feb 04, 2025 3:46 am This page is one of the best explanations of how geometry works youll find on the entire internet.
If that doesnt help, nothing else can.

But here is the thing that the tutorial doesnt tell you: you dont need all that math, this is game dev, not engineering.
If you just slowly adjust the x/y values of the circle towards the mouse each frame you get more or less the same movements with just basic + - * math.
Thank you, but it's a matter of principle. I feel, that I'm close to understanding.
sunsh1ne
Prole
Posts: 4
Joined: Mon Feb 03, 2025 9:48 pm

Re: How is sin and cos work here?

Post by sunsh1ne »

Hello,

you can use these functions to do most of the calculations you need for angles, distances and such:

Code: Select all

-- angle between two points / vectors
function math.angle(x1, y1, x2, y2)
    return math.round(math.deg(math.atan2(y2 - y1, x2 - x1)))
end

-- find point from x,y in distance length under angle
function math.distancePoint(x, y, length, angle)
    local new_x = math.round(x + length * math.cos(math.rad(angle)))
    local new_y = math.round(y + length * math.sin(math.rad(angle)))
    return new_x, new_y
end

-- distance between two points
function math.distance(x1, y1, x2, y2)
    return math.round(math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2))
end

-- clamp angle to 0-360 degrees
function math.clampAngle(angle)
    angle = angle % 360
    return math.round(angle)
end
I don't know how can I use it, but thank you for the answer
User avatar
pgimeno
Party member
Posts: 3709
Joined: Sun Oct 18, 2015 2:58 pm

Re: How is sin and cos work here?

Post by pgimeno »

I've made a little video with Löve (attached) explaining sine and cosine.
Attachments
SinCosVideo.love
(1.2 KiB) Downloaded 33 times
sunsh1ne
Prole
Posts: 4
Joined: Mon Feb 03, 2025 9:48 pm

Re: How is sin and cos work here?

Post by sunsh1ne »

pgimeno wrote: Tue Feb 04, 2025 5:17 pm I've made a little video with Löve (attached) explaining sine and cosine.
thank you! This looks very easy and clear for understanding what is sine and cosine! In these days I have figured this out much better. And the code no longer seems like some kind of magic.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest