GoldSpark
GoldSpark

Reputation: 69

How to get current facing direction vector 2D?

So I am developing a game in 2D in Godot and I want to see a line from the center of my sprite to its facing position. I have the sprite rotating and moving along the rotation direction but when I try to create vector out of that its very wrong. For example the vector line is going from the center of the sprite to near (0,0) position on the screen.

 public override void _Draw()
{
    Vector2 rotationDirection = new Vector2(Mathf.Cos(sprite.GlobalRotation) , Mathf.Sin(sprite.GlobalRotation)) - sprite.GlobalPosition;
    DrawLine(sprite.GlobalPosition, rotationDirection, Colors.Red, 2f);

}

EDIT:

Fixed it it works now.

 public override void _Draw()
{         
    DrawLine(sprite.GlobalPosition, sprite.GlobalPosition + new Vector2(Mathf.Cos(sprite.GlobalRotation), Mathf.Sin(sprite.GlobalRotation)) * 50f, Colors.Red, 2f);

}

Upvotes: 0

Views: 1952

Answers (1)

Thomas
Thomas

Reputation: 182000

A simpler solution would have been to add a Line2D child node. You can see the effect right away in the editor, and there's no need for any maths.

This might also be more efficient, because custom _draw functions don't play nice with Godot's geometry batching.

Upvotes: 1

Related Questions