question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make Vector2.angle and Vector2.angleRad consistent

See original GitHub issue

Current situation

  • Vector2.angle() returns values between [0, 360]
  • Vector2.angle(Vector2) returns values between [-180, 180]
  • Vector2.angleRad() returns values between [-Math.PI, Math.PI]
  • Vector2.angleRad(Vector2) returns values between [-Math.PI, Math.PI]

There’s two inconsistencies here.

  1. Result range: Why only one of these function returns always positive values, while the 3 others don’t?
  2. Naming: Why some function are named with the angle unit, and not the others?

Proposition:

  1. Add angleDeg() and angleDeg(Vetor2) functions returning both values between [-180, +180]
  2. Deprecate angle() and angle(Vector2) functions

This would make usage of angles in radians and degrees more consistent. Conversions would also be easier, since multiplying by MathUtils.degreeToRadians or MathUtils.radiansToDegree would always be enough.

Reproduction steps/code

This:

System.out.println(new Vector.2(0f, -1f).angleDeg()); // -90
System.out.println(new Vector.2(0f, -1f).angleRad()); // -Math.PI / 2

Would be preferable to:

System.out.println(new Vector.2(0f, -1f).angle()); // 270
System.out.println(new Vector.2(0f, -1f).angleRad()); // -Math.PI / 2

Version of LibGDX and/or relevant dependencies

1.9.8

Please select the affected platforms

Platform is irrelevant here. This is math.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
obigucommented, Oct 2, 2018

In the 2D world and the non engineering specialised world everybody is used to work in the [0º,360º] range and this is how it works across most libGDX classes ranging from Math utils classes, to Camera, etc… The word convention means that is what people usually use, nothing else, you won’t find a statement online, just ask anybody how they usually work in 2D geometry.

I don’t know about 3D engines Unity and Unreal but those 2 links you provide don’t proof all the functions on the engine work on that range (here’s for example a link to a class that works on the [0,360] https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html). Even if they did though, it doesn’t matter, we can do better/simpler because Vector2 is 2D only.

1reaction
jcornazcommented, Oct 3, 2018

I aggree it is not a “big deal”. I’m only saying it can be better.

However, we probably not have the same definition of “beginner friendly”. I find it very difficult for a beginner to understand why none of angle(), angle(Vector2), angle() and angleRad(Vector2) behave consistently. It would be so much simpler to get started if theses functions would be consistent.

The current behavior is:

  • angle() returns between [0, 360] toward +y
  • angle(Vector2) returns between [-180, 180] toward -y
  • angleRad() returns between [-Math.PI, Math.PI] toward +y
  • angleRad(Vector2) returns between [-Math.PI, Math.PI] toward -y

Do you really find that “beginner friendly”?

My conception of “beginner friendly” would be more:

  • angle() returns between [-180, 180] toward +y
  • angle(Vector2) returns between [-180, 180] toward +y
  • angleRad() returns between [-Math.PI, Math.PI] toward +y
  • angleRad(Vector2) returns between [-Math.PI, Math.PI] toward +y

user code may rely on it.

That’s why I don’t propose to change it, but to deprecate it and provide an alternative.

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# Getting a Vector 2 from an angle - Unity Forum
1. My character's rotation is given as an angle (e.g 90 degrees, 45 degrees). 2. The sin function should return the ratio between...
Read more >
Getting the angle between Vector2 - Stack Overflow
Both star and road are moving downward at a constant speed during each update. The car does not move but appears to be...
Read more >
Scripting API: Vector2.Angle - Unity - Manual
The angle returned is the unsigned angle between the two vectors. Note: The angle returned will always be between 0 and 180 degrees,...
Read more >
How to convert a radial into a Vector2 - Godot Engine - Q&A
You can convert an angle in radians (not radial) into a Vector2 using simple math: var direction = Vector2(cos(angle), sin(angle)).
Read more >
Phaser 3 API Documentation - Class: Vector2
The angle between this Vector, and the positive x-axis, given in radians. Type: number. clone(). Make a clone of this Vector2.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found