Flame(Flutter game engine) Vector2 (method)(random vector2)
taesikmun
1 min read
Vector2 Class
It is basically a class that represents a 2D Vector which is an entity of(x,y), which also has a length value.
It is used for representing the position, a velocity which is a vector with direction and magnitude, or an (x,y) rectangle size.
Vector2 Method
negate() - It reverses the direction of the vector.
length() - It gives the size or magnitude of the vector.
normalized() - It normalizes the vector (magnitude 1)
random() - It gives a random vector in the range of (0,0) to (1,1)
How to create a vector that is moving at 90' north (up) and with speed of 20 units?
Vectir2(0,-1).normalized() * 20
Random generation
Case with random()
- Random location
//First, get a random vector and set its velocity
velocity = Vector2.random().normalized() * 22
//Then set the position of the component to the location
position = Vector2(80,100)
Random magnitude (speed)
Random direction - vector heading
Vector2.random().normalized() * 10
0
Subscribe to my newsletter
Read articles from taesikmun directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
taesikmun
taesikmun
Software developer in South Korea