If you’re using the x and y somewhere where they need to be integers cast them there not at root? In fact if that’s you argument why not just access the variables directly and cast them and do away with the getters altogether? It’s confusing to expose getters and setters that degrade the precision especially if you offer the code to others.
Thank you for this I understand it better now
Yea it works xD i Just made new x,y floats and and works sometimes i just don’t understand computer/java thanks everybody:D
Normalization is all about scaling a value to be between 0 and 1. This is useful for many things, but most game developers only use this to remove the magnitude of a vector and only keep it’s direction. You can achieve normalization by dividing a number with it’s maximum possible value. So if you want to normalize numbers that can go from 0 to 50 to the range 0 and 1 you have to divide your numbers by the max value, that is 50.
Same thing with vectors, although not as easy because vectors have multiple components. If a 2D vector only travels on the X axis for example, e.g. a vector that is [50; 0] than the vector’s length is 50, so you have to divide all components with 50 resulting in a 2D vector [1; 0] which correctly represent the direction of the original vector. Things get a bit more complicated when the vector travels on both the X and Y axis, e.g. when you have a 2D vector [50; 18]. In this case you have to calculate the vector’s length which is ~53.1413 in this case and divide the components with that number, resulting in ~ [0.9409; 0.3387] which again correctly represent the direction of the original vector. Hope things are a bit clearer now.
That’s cool, in my world people usually represent directions like forward and right using 2D vectors (or with a single 3x3 or 4x4 matrix, for that matter) and not with angles because it’s more efficient to do it this way instead of computing the direction every single time you want to use it. If we want to rotate something we just use rotation matrices.
No need to be arrogant or sarcastic, I was never doing that to you either. :point:
So am I right in thinking this:
I have a vector which is 4, 4 and find the magnitude by doing 4x4 + 4x4 = 32 and the square root is 5.6 which is the magnitude of the vector. I then do 4 / 5.6, 4 / 5.6 which is 0.714285, 0.714285. This keeps the vector on the same angle as before but now has a magnitude of 1 if we do:
0.714285 x 0.714285, 0.714285 x 0.714285 = 1.0204.
Then when I want something like bullets to travel towards the player I do: playerXPos += direction.x * speed. This makes the bullet travel on the angle towards the player at the desired speed.
Am I right in thinking that the direction.x knows it has a magnitude of 1 and the angle it is going? I’m a little confused about what the direction.x represents. For example, when I multiply the speed onto the direction, is it using the magnitude so the direction.x might equal 4 (if the speed was 4)? This is the only bit I am finding confusing right now and if someone could clear me up on what information the direction.x is holding I would appreciate it
sqrt(32) = 5.656
EDIT-
If you use this, you will get that magnitude = 0.999, which is 1 technically.
The square root of 32 is 5.656854249… and more, it’s irrational, that is it can’t be represented by a finite number of digits, so the maths isn’t going to be perfect and you will likely not get back to exactly 1.0 as you expect. Finding your magnitude again you haven’t show that you square root but you should. As trollwarrior1 has said 0.9… is arbitrarily close to 1.0 and for all intent and purpose can be considered to be 1.0, it’s not perfect but things like this rarely are.
The only way you will get this to be perfect is by evaluating it symbolically, i.e:
sqrt(32) = sqrt(2 * 16) = sqrt(2 * 2 * 8) = sqrt(2 * 2 * 2 * 4) = sqrt(2 * 2 * 2 * 2 * 2) = 4 * sqrt(2)
Then use this as you can go no further. Clearly you can’t use this in your game so it’s of no help.
You might consider looking at this http://www.mathsisfun.com/algebra/vectors.html it’s basic but I think will help as it has diagrams to explain.
I think I understand it all and thanks for the link, it helped a lot due to the diagrams. I’m still confused about what part of the vector is actually added during this code:
playerPosX += velocity.x * speed
Is it the magnitude(1 unit) * speed? Or is it the x value from normalizing the vector? I’m slightly confused on what info is actually being passed to the player position. I have a feeling that the magnitude is * by the speed and the vector then just goes it the direction?
The variable naming there is confusing. The ‘velocity’ shouldn’t be called velocity but something like ‘direction’ instead, and it’s supposed to be a normalized vector.
When you multiply a normalized vector with a value then essentially you’re manipulating it’s magnitude.
If we would use my previous example here [50; 18] which got normalized to ~ [0.9409; 0.3387] and let’s say that speed is 20 then we would get the following:
playerPos.x = 0.9409f * 20f;
playerPos.y = 0.3387f * 20f;
If we would calculate playerPos’s length now the result would be 20, or at least very close to it.
This is because floating point rounding errors, and you have to watch out for this because it can be a pain at times.
Last time when I was developing the AI for our game I’ve spent 2 days debugging only to find out that I have one of these nasty errors.
Ahh I think understand now ;D the vector still retains it’s direction and then we are simply manipulating the magnitude with the speed so that when it is added to the position, it still retains the direction but moves by the magnitude length? Which is why the speed is useful for keeping things moving at the same speed