Hey all, first time interacting with the community here. I am following along with Chris Bradfield’s Godot 4 Game Development Projects book. I am on the Space Rocks project, which is a 2D asteroids-like game with physics. I’m currently going back and thoroughly studying everything in the game (scripts, nodes, animations, etc.) as I am about to attempt to rebuild it without using the book or any references. I am however, confused about what’s going on here:
vel = Vector2.RIGHT.rotated(randf_range(0, TAU)) * randf_range(50, 125)
For more context, we are assigning a blank parameter which starts with the value of null a new Vector2 value to later be used to set a RigidBody2D’s linear_velocity to enable movement. I’m confused as to how the y axis in Vector2.RIGHT gets changed to a number other than 0, as Vector2.RIGHT should be (1, 0) by default. I tried to do this same rotated action to a Vector2.ZERO and it returned as expected (0, 0). Why does the y axis in Vector2.RIGHT return as anything other than 0 when it should always return as 0 when multiplied by any number? Another confusing aspect of this to me is how the rotated() method returns a radians as a result of rotating the Vector2 by angle. angle() in a Vector2.RIGHT vector should return as 0, as stated in the documentation:
For example, Vector2.RIGHT.angle() will return zero, Vector2.DOWN.angle() will return PI / 2 (a quarter turn, or 90 degrees), and Vector2(1, -1).angle() will return -PI / 4 (a negative eighth turn, or -45 degrees).
Can anybody enlighten me on the inner workings of these methods/ calculations? Any help is greatly appreciated.
(And just an FYI the game runs exactly as intended with this line of code. It returns a random float in both the x and y axes, which we later pass to a linear_velocity property and the body moves as intended.)
Take a “right” pointing vector and rotate it by a random angle. (Between 0 and TAU (2π) radians, or 0° - 360° degrees). Then multiply by a scaler value between 50 and 125.
Hey, thanks for taking time out of your day to help me out. It’s appreciated. After reading your response, there is only one thing I’m unsure of, and would like some further clarification: when we get a random angle, and rotate Vector2.RIGHT by it, does the Vector move in the process? Or is it still (1, 0) until the scaler value is factored? If the latter is true, wouldn’t the y axis stay at 0? If it does move in the act of rotating, what is the axis it rotates around? I know in Godot rotations happen clockwise, I saw this in the matrices and transforms documentation, would it be safe to assume the axis for Vector2.RIGHT in this case would be (0, 0)? Sorry if this seems like a stupid question, I’m a highschool dropout with exactly zero math skills lol, trying to learn now that I can actually put it to use making games.
Direction vectors have no concept of position, and are always assumed to be located at (0,0). They represent direction and length. Vector2.RIGHT, when rotated, will change its x and y components along the unit circle as is appropriate for the rotation, and will maintain a length of 1.
So how does the scaler value get me numbers like -21.18796625 and 0.37894286? Wouldn’t the Vector2.RIGHT have to have values other than 0 to get these figures? When I use Vector2.ZERO in this formula, it gets me exactly what I would expect (0, 0), but then use (1, 0), like in this example, or even (0, 1) and it gets me these floating-point numbers. I’m really confused about where these numbers are coming from.
After the rotation of Vector2.RIGHT, which will always result in x and y components between -1.0 and 1.0, the rotated unit vector is multiplied by a number (a scalar). Multiplying a vector and a scalar results in a new vector.
I highly recommend getting a book on introductory linear algebra. It will answer a lot of questions.
I will add that when you scale a vector you change its length while the direction stays the same.
the math behind how a scaling works is an element wise multiplication.
if i scale a right vector <1,0> by 7, I do this math
< 1 , 0 > * 7 = < 7 * 1 , 7 * 0 > = < 7, 0 >
I would also suggest learning physics which can get vector`y, if analytical math, like linear algebra, is boring to you. (i will say matrix multiplications are very useful in game dev though.)
but yea vectors always start at zero (the origin) and point at a position on the Cartesian plane, although this is arbitrary and your starting point can be from any origin in space. checkout 3 Blue 1 Brown for visual examples.
@pennyloafers@soapspangledgames Thank you guys for the help, the main confusion for me was understanding how the vectors were being manipulated in the rotated() method, as I was unclear on whether or not the vector moved in the process. It was kind of obvious it had to, as like I said, if the vector stayed at (1, 0) and got scaled by 50, it would be (50, 0). But I was getting floating-point numbers in the y axis every time. I should have been able to figure this out on my own if I had just put in a few different radians in the rotated() method and had the editor print the value of the variable after the rotation. Thank you guys nonetheless, and I appreciate the video link. I will probably watch the series and jump into linear algebra in the near future.
Notice that the right pointing vector has a length of 1, and a rotated vector still has a length of 1, even though the individual components of the vector are less than 1. Length of a vector uses trigonometry to find it. (Trig: a²+b²=c², vector: x²+y²=length²)
√( 0.7071² + 0.7071² ) ~= 1.0 # some precision loss as √2/2 is irrational.