Flame(Flutter game engine) Vector2 (method)(random vector2)

·

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

  1. negate() - It reverses the direction of the vector.

  2. length() - It gives the size or magnitude of the vector.

  3. normalized() - It normalizes the vector (magnitude 1)

  4. 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()

  1. 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)
  1. Random magnitude (speed)

  2. Random direction - vector heading

Vector2.random().normalized() * 10