#How do I use the normalize(); command?

1 messages ยท Page 1 of 1 (latest)

worldly coyoteBOT
#

<@&987246399047479336> please have a look, thanks.

worldly coyoteBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

to use the normalize() command, it doesn't seem to work correctly.

To use the normalize() command, you need to pass a vector as an argument. The normalize() function will then return a new vector with the same direction as the input vector but with a length of 1. This means that the resulting vector will have the same direction as the original vector but will be scaled down to have a magnitude of 1.

To ensure that diagonal movement is the same speed as vertical or horizontal movement, you can follow these steps:

  1. Calculate the direction vector for your movement. This can be done by subtracting the starting position from the target position.

    Vector2 direction = targetPosition - startPosition;
    
  2. Use the normalize() function to get a normalized direction vector.

    direction = direction.normalize();
    
  3. Multiply this normalized direction vector by your desired speed value.

    float speed = 5.0f; // Adjust this value according to your needs
    Vector2 velocity = direction * speed;
    

Now, you can use this velocity vector for your movement calculations, ensuring that diagonal movement has the same speed as vertical or horizontal movement.

If you're still experiencing issues with using normalize(), make sure you're using it correctly within your programming language or game engine. Additionally, check if there are any specific requirements or limitations for using normalize() in your particular context.

minor kayak
#

that is correct

#

normalizing makes the values between 0-1

#

because it's representing a direction

#

So, you should not normalize the position, but normalize what you are adding to the position

#

(the velocity)

#

and only after normalizing the direction, you multiply it by some speed multiplier

worldly coyoteBOT
#

Vector normalization is the process of scaling a vector to have a unit length or magnitude of 1 while preserving its direction. This is done by dividing each component of the vector by its magnitude.

To normalize a 2D vector with x and y components, follow these steps:

  1. Calculate the magnitude of the vector using the Pythagorean theorem:
    magnitude = sqrt(x^2 + y^2)

  2. Divide each component of the vector by its magnitude:
    normalized_x = x / magnitude
    normalized_y = y / magnitude

For example, let's say we have a 2D vector with components x = 3 and y = 4.

  1. Calculate the magnitude:
    magnitude = sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5

  2. Divide each component by the magnitude:
    normalized_x = 3 / 5 = 0.6
    normalized_y = 4 / 5 = 0.8

So, the normalized form of the vector (3,4) is (0.6,0.8).

minor kayak
#

well no, not directly

#

you need the 2 values (x direction and y direction) together to normalize

#

do you have a Vector class that is used?

#

oh PVector

#

Right, so currently you don't have a direction vector, but we need that

#

What you do have is 2 boolean values, which basically are the direction

#

So what you want is to create a PVector where the x is the horizontal movement, and the y is the vertical movement

#

exactly

#

so moving left and up would result in a vector like (-1, 1)

#

then you can easily normalize that

#

and then add that to the position

#

nope

#

no speed yet

#

just the direction

#

because we only want the direction for normalizing

#

it would technically work with 1 speed value, but it's better to just multiply that later

#

this is fine, until you press 2 keys at once

#

Hold on, I'm cooking up a good example
There's a pretty common way to do this
The issue is that you have to make booleans to int

#

Do you know the ternary operator?

#

int myInt = myBoolean ? 1 : 0;

#

basically a small if statement

#

before the ? is the condition

#

if that is true, then the result will be what's after the ?

#

if it's false then the result will be what's after the :

#

cuz that'll make it a bit smaller

#

int myInt = true ? 1 : 0; myInt will be 1
int myInt = false ? 1 : 0; myInt will be 0

#

This is what we are gonna use to make the booleans to a direction

#

yup

#

and the rest the same way

#

if you add on the movingRight after that, and both keys are pressed, it will be 0

#

so no movement when right and left are pressed

#

And after that it's as simple as

  • Multiply the direction x and y by your speed
  • Add the direction to your position
#

The PVector should probably have a multiply method to multiply both values

#

Or just do it manually

#

great

#

uh

#

is direction null?

#

you should probably make that a local variable

#

or a new one

#

cuz we only need that direction vector in the method

#

also btw we're still missing something

#

try imagine what happens now when left is pressed and right is not

#

go through code ^-^

#

ok hint, = should be +=

#

Look, if movingLeft is true, we set dir.x to -1

#

But then right after, if movingRight is false, we set dir.x to 0 again

#

A solution to this is to add the second one += to the first

#

That means the last won't cancel the first
And it will also have the effect of being 0 when both are true

minor kayak
#

It's the direction we want to move in

#

Or just print ig

#

Epic, have fun coding ^-^