Understanding Vector2 in Godot

So I finished the 2D Godot course and decided to work though a book I got from a humble bundle. One of the projects is an asteroids clone. Right now the project is just a Player Ship(rigidbody2D) and a bullet that is an Area2D.
Here is the functioning bullet script :
var bulletSpeed = 1000
var velocity = Vector2.ZERO

func start(_transform):
transform = _transform
velocity = transform.x * bulletSpeed

func _process(delta):
position += velocity * delta
When I first wrote this I was getting an error because I instinctively typed:
velocity.x = transform.x * bulletSpeed

And I’m just curious why that doesn’t work? Wouldn’t they all be floats within a Vector2? Also how does it know to put the float here: Vector2(transform.x, 0) But not here: Vector2(0, transform.x)
I’ve already experimented by adding : velocity = transform.y * bulletSpeed
And that over rides the the previous code creating this : Vector2(0, transform.y) causing the bullet to shoot out of the right side of the ship intead of forward.
Also if I add this : velocity += transform.y * bulletSpeed
Now the bullet shoots out at an angle and gives me this: Vector2(transform.x, transform.y)
But again how does it know?
And if I add this : velocity.y = transform.y * bulletSpeed I get a crash error. Why?
And last when I add velocity.y = 500 I don’t get any errors, but the bullet always shoots south in random directions.

honestly, i cant see how that works myself.

i can see how the usual suspects are there.
declaring the velocity and setting it to Vector2.ZERO
but then i would have normally set that to a direction of travel for the bullet, like Vector2.LEFT etc.
then multiply that direction of travel by speed and delta.

whats confusing me is the use of ’ transform '.

this in iself is a 2x3 Matrix, that comprises of three Vector2 types, X ,Y and origin and used for tracking all the various transforms, be that position, scale, rotation etc.
but its waay abouve my head to go manipulating them directly.

the default Transform2D matrix gives the following
if i do print(transform)
[X: (1, 0), Y: (0, 1), O: (581, 329)]

so with that, you can see the three matrix components that are in turn of type Vector2, i just dont understand why they would be manipulated directly to be honest.

but for bullets that are fired i tend to do like you did there, is altering the transform.position (with just position)

when you get into the realms of 3D, things take a hike up with a Transform3D matrix
as an example it gives a 3x4 matrix.
[X: (1, 0, 0), Y: (0, 0.422618, -0.906308), Z: (0, 0.906308, 0.422618), O: (0, 14.5, 9.5)]

1 Like

This is from the book “Godot 4 game development projects 2nd edition” and I think the Vector2.Zero seems to just be a preference of his. Yea, I don’t see any difference between Vector2.ZERO and Vector2.LEFT. Once the function runs velocity gets changed to the transform of the object.

To clarify more, This script is on the bullet scene and the start function is called from a shoot function on the player ship script. That’s where the transform is set, which in this case is a 2D marker that is a child of the player ship. I think you need the object’s transform because this is a ship that can rotate and fire in all 360 degrees.

I just didn’t really understand why trying to change velocity.x or .y directly gave me errors.

Thank you for the indepth reply.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms