+2 votes

vel.x = (basis.z * input.y + basis.x * input.x).x

in Engine by (224 points)

1 Answer

0 votes
Best answer

They're setting the x component of a velocity vector by mutliplying the y input (forward/backwards on a controller stick) with the forward vector (basis.z, although normally minus z is used as forward), and the x input (sideways motion on a controller stick) with the "sideways" vector, then only keeping the x component. Seems to me like basis.z * input.y is useless, since it will always result in an x of 0.

It would sure help to see the whole script though.

by (1,194 points)
selected by

Thank you your answer. English is hard for me but i try.
(basis.z * input.y + basis.x * input.x).x

Interpreting the syntax of the code is a problem for me. I have never seen such a way to create a vector. Could you explain that this is because there is nothing about it in the documentation. Pls help.
Full code https://godottutorials.pro/fps-godot-tutorial/#Scripting_the_Player
but i only syntax is misunderstan for me only that one line what i writted my question. Vel.x = (x + y).x what is that syntax.

I think I see why you're confused, and I can't blame you.

basis describes a 3d space and has 3 unit vectors. Those are confusingly named x, y and z. IMO they should have been named something like right, up and forward, because each of those is a Vector3, not a float.

The input object looks like a Vector2. So it would look something like: { x: 1.0, y: -0.5 }
In this example, the user is pressing full right and half down on the joystick.
But the basis object looks like this:

    x: {
        x: 1.0,
        y: 0.0,
        z: 0.0
    y: {
        x: 0.0,
        y: 1.0,
        z: 0.0
    z: {
        x: 0.0,
        y: 0.0,
        z: 1.0

Source: https://docs.godotengine.org/en/stable/classes/class_basis.html#class-basis
See what I mean when I say the naming of the unit vectors is confusing? To get the z component of the forward vector you must write: basis.z.z

But the basis I shown is just the identity, meaning it's equal to the "normal" axes. The basis is used also to represent rotation. So I was wrong when I said basis.z * input.y is useless, since if the camera is rotated it won't result in 0 all the time.

See these pages if you need a refresher on vectors and bases: https://docs.godotengine.org/en/stable/tutorials/math/vector_math.html


OMG thank you I understand now you are the best!

I like this idea

That's how Unity does it. Instead of doing transform.basis.z you just do transform.forward where forward is like an alias for basis.z.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.