Godot: New Vector convenience method - rand

Created on 12 Dec 2019  Â·  9Comments  Â·  Source: godotengine/godot

I find myself needing random vectors for enemy movements and use something like this:

func new_direction() -> Vector2:
   var new_dir: = Vector2()
   new_dir.x = rand_range(-1, 1)
   new_dir.y = rand_range(-1, 1)
   return new_dir.normalized()

I wonder if this might be generalised for 2D and 3D and new convenience methods be created. For certain type of games, the resulting vector might be constrained to orthogonal movement so arguments might be considered for that and/or other constraints.

In the first instance, floats in the range (-1, -1) to (1, 1) and orthogonal would prove useful.
I would not expect randomize to be part of the method, as is the case elsewhere.

enhancement core

Most helpful comment

Maybe adding these methods to RandomNumberGenerator make more sense?

All 9 comments

func new_direction() -> Vector2:
    return Vector2(-1 + randf() * 2, -1 + randf() * 2).normalized()

Thanks @KoBeWi. I'm aware of that but, if you look at the methods in vector2.cpp, more than 25 of them are single lines of code. It's not the difficulty of writing but the convenience that I'm asking about.

As a Godot noob, I don't know what the criteria are for enhancing existing classes but this seems like a candidate, hence my enquiring...

I'm not sure about this either, as none of the other engine types (Color, Transform, …) have built-in random methods to my knowledge. If we add random methods for Vector2/Vector3, people will likely start asking them to be added to every engine type out there, which will bloat the class reference.

People will also have different requirements for random Vector methods; some people may prefer to use normal distribution (randfn()), for instance. This means we'd have to add even more methods.

@Calinou Unity has random methods like that, which are quite specific and tailored to what happens in games https://docs.unity3d.com/ScriptReference/Random.html

Maybe adding these methods to RandomNumberGenerator make more sense?

@Beechside
For 2D random unit vectors I'm using:

var angle = randf() * 2 * PI
var rand_direction_vector = Vector2( sin(angle), cos(angle) )

For 3D it is more complicated, as you need to calculate a point on the sphere, and using:

func new_direction() -> Vector3:
   var new_dir: = Vector3()
   new_dir.x = rand_range(-1, 1)
   new_dir.y = rand_range(-1, 1)
   new_dir.z = rand_range(-1, 1)
   return new_dir.normalized()

will mess up random distribution as, you will get more vectors pointing to the corners of a 2x2x2 cube.

It would be nice to have something for random unit vector built-in e.g.:

var new_2d = Vector2.RAND
var new_3d = Vector3.RAND

or

var new_2d = Vector2.rand
var new_3d = Vector3.rand

since Vector.ZERO is declaration and is using constant, and RAND would not be constant.

Surely everyone will have different use case, but random unit vector would be quite useful, especially if the user is less math oriented.

Relevant math Stackexchange post to help with implementing the function: https://math.stackexchange.com/questions/44689/how-to-find-a-random-axis-or-unit-vector-in-3d

Edit: Linked Wikipedia page from second answer even has example code

I've come up with a general-purpose solution for this in #43103.

See also godotengine/godot-proposals#1731 which may as well supersede this feature request.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

testman42 picture testman42  Â·  3Comments

RebelliousX picture RebelliousX  Â·  3Comments

timoschwarzer picture timoschwarzer  Â·  3Comments

SleepProgger picture SleepProgger  Â·  3Comments

n-pigeon picture n-pigeon  Â·  3Comments