(Contains SSK2 Calls) What is the point of normalizing?

I added I few print statements to an experiment I was doing with SSK2, and I came across something interesting.

local vec = ssk.math2d.angle2Vector(angle, true) print("Before normalizing: ", vec.x, vec.y) vec = ssk.math2d.normalize(vec) print("After normalizing: ", vec.x, vec.y)

The output showed that the values were equal:

Before normalizing: 0.24868988716485 -0.96858316112863 After normalizing: 0.24868988716485 -0.96858316112863

Either I did something wrong here or there is another reason for normalizing vectors. What would that reason be? If anyone could explain, that would be great!

I mean I understand what it is doing…

It seems to apply distance formula on the two values and divides each value by that distance. But what are the advantages that come with doing that?

A reasonable implementation of vector-from-angle is just going to use cosine and sine, giving you a vector on the unit circle, which is already normalized, so “normalizing” it does nothing.

The nice thing about unit vectors (a more common name for a normalized vector), is that advancing by that vector actually moves you a distance of 1, so it’s basically like x = x + 1 or y = y + 1 , only in a given direction. Or you can rescale this to some distance you actually want: “get this segment, normalize it, then stretch it out to 5 units”.

@sdktester15

Hi!  

First, angle2Vector() already produces a normalized vector, so you don’t need to normalize it.  

Note: The docs say this… :slight_smile:

 

>> Converts a (screen) angle into a normalized direction vector of the form < vx, vy >.

Second, normalizing converts a vector to a unit-length vector and is useful before applying scaling, et al.

Third, consider two examples:

Example 1 - No need to normalize

local bullet = display.newCircle( 10, 10, 10 ) physics.addBody( bullet ) -- 'Fire' bullet in direction 45 degrees from current position -- with velocity 500 pixels per second. local angle = 45 local vec = ssk.math2d.angle2Vector( angle, true ) -- No need to normalize vec = ssk.math2d.scale( vec, 500 ) bullet:setLinearVeocity( vec.x, vec.y )

Example 2 - Normalizing Needed

local bullet = display.newCircle( 10, 10, 10 ) physics.addBody( bullet ) -- 'Fire' bullet in direction of 'target' with velocity 500 pixels per second. local vec = ssk.math2d.diff( bullet, target ) vec = ssk.math2d.normalize( vec ) -- If we didn't do this, velocity would be wrong vec = ssk.math2d.scale( vec, 500 ) bullet:setLinearVeocity( vec.x, vec.y )

Hmm… this explains it a bit better. 

That’s why the angle2Vector values were the same before and after normalizing.

Thanks for explaining.

I mean I understand what it is doing…

It seems to apply distance formula on the two values and divides each value by that distance. But what are the advantages that come with doing that?

A reasonable implementation of vector-from-angle is just going to use cosine and sine, giving you a vector on the unit circle, which is already normalized, so “normalizing” it does nothing.

The nice thing about unit vectors (a more common name for a normalized vector), is that advancing by that vector actually moves you a distance of 1, so it’s basically like x = x + 1 or y = y + 1 , only in a given direction. Or you can rescale this to some distance you actually want: “get this segment, normalize it, then stretch it out to 5 units”.

@sdktester15

Hi!  

First, angle2Vector() already produces a normalized vector, so you don’t need to normalize it.  

Note: The docs say this… :slight_smile:

 

>> Converts a (screen) angle into a normalized direction vector of the form < vx, vy >.

Second, normalizing converts a vector to a unit-length vector and is useful before applying scaling, et al.

Third, consider two examples:

Example 1 - No need to normalize

local bullet = display.newCircle( 10, 10, 10 ) physics.addBody( bullet ) -- 'Fire' bullet in direction 45 degrees from current position -- with velocity 500 pixels per second. local angle = 45 local vec = ssk.math2d.angle2Vector( angle, true ) -- No need to normalize vec = ssk.math2d.scale( vec, 500 ) bullet:setLinearVeocity( vec.x, vec.y )

Example 2 - Normalizing Needed

local bullet = display.newCircle( 10, 10, 10 ) physics.addBody( bullet ) -- 'Fire' bullet in direction of 'target' with velocity 500 pixels per second. local vec = ssk.math2d.diff( bullet, target ) vec = ssk.math2d.normalize( vec ) -- If we didn't do this, velocity would be wrong vec = ssk.math2d.scale( vec, 500 ) bullet:setLinearVeocity( vec.x, vec.y )

Hmm… this explains it a bit better. 

That’s why the angle2Vector values were the same before and after normalizing.

Thanks for explaining.