2
\$\begingroup\$

To get used to how Godot transforms work, I am attempting to write an orbit control. The goal is for it to behave similarly to the Godot viewport.

So far I have the positioning of my camera working. However, when I try to point it at the center of orbit (0,0,0), the LookingAt() function doesn't seem to work. I have printed the values to the console and just proves beyond a doubt that there isn't any change.

I've tried moving the camera around and using different values for the up-axis, but it still doesn't work.

Here are the pertinent functions from my Camera node's script:

private Vector2 rotateStart;
private Vector2 rotateEnd;
private Spherical spherical = new Spherical();
private Vector3 target = new Vector3();

public void HandleRotation(float delta)
{
    var transform = GetGlobalTransform();
    var viewportSize = GetViewport().GetVisibleRect().Size;        

    rotateEnd = GetViewport().GetMousePosition();
    Vector2 RotateDelta = rotateEnd - rotateStart; //Change in rotation
    spherical.SetFromVector(transform.origin); //Spherical = current location

    //Calculate changes to equatorial and vertical rotations.
    spherical.theta += 2 * (float)Math.PI * RotateDelta.x / viewportSize.x;
    spherical.phi += 2 * (float)Math.PI * RotateDelta.y / viewportSize.y;
    var final_transform = new Vector3();

    spherical.CopyToVector3(ref final_transform);
    transform.Translated(final_transform - transform.origin); //Apply position change
    GD.Print(transform);

    //Point at origin (point of orbit)
    transform.LookingAt(target, new Vector3(0, 1, 0));
    GD.Print(transform);
    SetGlobalTransform(transform);

    rotateStart = rotateEnd;
}

public override void _Process(float delta)
{
    if (Input.IsMouseButtonPressed((int)ButtonList.Middle))
    {
        HandleRotation(delta);
    }
    else
    {
        rotateStart = GetViewport().GetMousePosition();
    }
}
\$\endgroup\$
  • \$\begingroup\$ Have you tried the static method, setLookAt()? See source. \$\endgroup\$ – Engineer Feb 13 '18 at 6:17
  • \$\begingroup\$ @ArcaneEngineer That probably is for 2.0. The 3.0 docs don't have it. \$\endgroup\$ – TARDIS Maker Feb 13 '18 at 15:40
1
\$\begingroup\$

Maybe you're misunderstanding how the method works?

You can't look at the zero vector i.e. (0,0,0), if the method needs a direction, since that means it has to have some length in order to be normalized and used to re-orient the camera.

You could look at the difference between the position of object you want to view, and the camera's position: This would make sense as such a non-zero vector describes a direction.

transform.LookingAt(target, object.position - camera.position);

Or am I mistaken? - on this note, best find someone else's code using the same method, and put it to the test in a minimal example. If it works in theirs, there's problem some simple problem in yours.

\$\endgroup\$
1
\$\begingroup\$

Okay, it turns out the issue was that Transform.LookingAt() is not self modifying (I completely missed the bit that specified there was a return value). so the fix is as simple as transform = transform.LookingAt(...,...).

\$\endgroup\$
  • 1
    \$\begingroup\$ Haha, that still happens to me with certain functions. Glad you got it in the end :) \$\endgroup\$ – Engineer Feb 15 '18 at 8:32
  • \$\begingroup\$ @ArcaneEngineer Thanks for all the suggestions! \$\endgroup\$ – GiantCowFilms Feb 16 '18 at 4:26

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.