How is sin and cos work here?
How is sin and cos work here?
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.
- BrotSagtMist
- Party member
- Posts: 680
- Joined: Fri Aug 06, 2021 10:30 pm
Re: How is sin and cos work here?
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.
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
Re: How is sin and cos work here?
Hello,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.
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

Re: How is sin and cos work here?
Thank you, but it's a matter of principle. I feel, that I'm close to understanding.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.
Re: How is sin and cos work here?
Hello,
you can use these functions to do most of the calculations you need for angles, distances and such:
I don't know how can I use it, but thank you for the answer
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
Re: How is sin and cos work here?
I've made a little video with Löve (attached) explaining sine and cosine.
- Attachments
-
SinCosVideo.love
- (1.2 KiB) Downloaded 33 times
Re: How is sin and cos work here?
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.
Who is online
Users browsing this forum: Bing [Bot] and 1 guest