Page 1 of 1

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

Posted: Wed Mar 29, 2023 12:40 pm
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.

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

Posted: Wed Mar 29, 2023 12:55 pm
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

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

Posted: Wed Mar 29, 2023 1:15 pm
by NoreoAlles
Thank you

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

Posted: Wed Mar 29, 2023 1:20 pm
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 )

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

Posted: Wed Mar 29, 2023 1:25 pm
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.

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

Posted: Wed Mar 29, 2023 1:38 pm
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.

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

Posted: Wed Mar 29, 2023 1:43 pm
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 :)

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

Posted: Thu Mar 30, 2023 12:46 am
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