What does .norm() do to a Vector in C++ and how do i do it in lua?

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
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

What does .norm() do to a Vector in C++ and how do i do it in lua?

Post by NoreoAlles »

I apolegize if anyone has ever asked a similar question but i just cant figure it out. I was looking at C++ tutorials for the DDA algorithm in a Tile based world in 2d and cant really follow it since it uses the .norm() function on a Vector looking something like this:

Code: Select all

dir = (Vector2 - Vector 1).norm();
I think i know how you subtract one Vector from another (vector1 - vector2 = (vec1x - vec2x, vec1y - vec2y), but please tell me if thats wrong.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
marclurr
Citizen
Posts: 98
Joined: Fri Apr 22, 2022 9:25 am

Re: What does .norm() do to a Vector in C++ and how do i do it in lua?

Post by marclurr »

Vectors have direction and scale, normalising the vector scales it to have a length of 1 while keeping the direction. For example a vector of (1,1) points to the top right, and has a scale of about 1.4142. Normalised the vector would be (0.7072,0.7072), this has the exact same direction but a length of 1.

There are vector libraries people have already nade for Lua, the one I have experience with is hump. The documentation can be found https://hump.readthedocs.io/en/latest/vector.html and the library its self is https://github.com/vrld/hump
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: What does .norm() do to a Vector in C++ and how do i do it in lua?

Post by NoreoAlles »

Thank you
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: What does .norm() do to a Vector in C++ and how do i do it in lua?

Post by Bigfoot71 »

`.norm` is used to normalize a vector, this consists in resizing it so that its length or its norm becomes equal to 1, while preserving its direction. The norm of a vector is its length or magnitude.

Here is what it corresponds to in Lua for normalizing a 2D vector:

Code: Select all

function normalize(x, y)
    local length = math.sqrt(x*x + y*y)
    return x/length, y/length
end
So effectively you should do something like this in your case:

Code: Select all

local dx = x1-x2
local dy = y1-y2
local length = math.sqrt(dx*dx+dy*dy)
local nx = dx/length
local ny = dy/length
So the normalization of the result of the subtraction of the two vectors makes it possible to transform the resulting vector into a unit vector, that is to say a vector which has a length of 1 unit, which can be useful for example for calculating directions . You can do the same kind of calculation to get the correct velocity when moving a 2D point diagonally so that the horizontal and vertical velocity simply doesn't add up.

You can also use a vector module like hump.vector, here is the doc: https://hump.readthedocs.io/en/latest/vector.html

I hope this is well explained ^^

(oops too slow :3 )
My avatar code for the curious :D V1, V2, V3.
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: What does .norm() do to a Vector in C++ and how do i do it in lua?

Post by NoreoAlles »

Bigfoot71 wrote: Wed Mar 29, 2023 1:20 pm
I hope this is well explained ^^

(oops too slow :3 )
You explained it very good and i couldnt get hump to work since it kept throwing "attempting to index local self" errors at me so the function you wrote is very much apprieciated. :)

edit: so, you werent to slow, that was the point i tried to get accross.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: What does .norm() do to a Vector in C++ and how do i do it in lua?

Post by Bigfoot71 »

Strange as a problem, when you call normalize on a vector you do it well like this

Code: Select all

nv = vector:normalized()
And not like this:

Code: Select all

nv = vector.normalized()
Do not hesitate to share the error because it is clearly not normal, otherwise nothing prevents you from writing your own vector module, it is really desirable to maintain a code correctly, it can quickly become hell when you returned to it later.
My avatar code for the curious :D V1, V2, V3.
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: What does .norm() do to a Vector in C++ and how do i do it in lua?

Post by NoreoAlles »

Bigfoot71 wrote: Wed Mar 29, 2023 1:38 pm Strange as a problem, when you call normalize on a vector you do it well like this

Code: Select all

nv = vector:normalized()
And not like this:

Code: Select all

nv = vector.normalized()
...
That was the issue, i was doing v.normalized instead of v:normalized. :crazy:
Thank you :)
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: What does .norm() do to a Vector in C++ and how do i do it in lua?

Post by zorg »

One thing many might have taken for granted so no one pointed out; the code for norm really should be protecting against the case where length would be zero, since then you'd be doing two division by zeroes at the end, which is never a fun time. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 17 guests