Godot Nature of Code  1.2.0
Nature of Code implemented in Godot Engine
Public Member Functions | Public Attributes | Protected Member Functions | Properties | List of all members
Agents.ImageFlowField Class Reference

Flow field based on a texture. More...

Inheritance diagram for Agents.ImageFlowField:
Agents.SimpleFlowField

Public Member Functions

 ImageFlowField ()
 Create a new image flow field More...
 
- Public Member Functions inherited from Agents.SimpleFlowField
Vector2 Lookup (Vector2 position)
 Lookup direction from flow. More...
 

Public Attributes

Texture SourceTexture
 Source texture More...
 
bool CenterOnScreen
 Center on screen More...
 
- Public Attributes inherited from Agents.SimpleFlowField
int Resolution = 30
 Grid resolution More...
 

Protected Member Functions

void CreateFieldFromTexture ()
 Create field from source texture. More...
 
Vector2 ColorToDirection (Color color)
 Convert color to direction. More...
 
override Vector2 ComputeDirectionFromPosition (int x, int y)
 Compute flow direction from a given position. Defaults with a right vector. More...
 
- Protected Member Functions inherited from Agents.SimpleFlowField
void RefreshDirections ()
 Refresh flow directions. More...
 

Properties

int TextureScale = 1 [get, set]
 Texture scale More...
 

Additional Inherited Members

- Protected Attributes inherited from Agents.SimpleFlowField
int cols
 Column count More...
 
int rows
 Row count More...
 
FlowDirection[] field
 Flow field More...
 
Vector2 size
 Size More...
 

Detailed Description

Flow field based on a texture.

Definition at line 8 of file ImageFlowField.cs.

Constructor & Destructor Documentation

◆ ImageFlowField()

Agents.ImageFlowField.ImageFlowField ( )
inline

Create a new image flow field

Definition at line 22 of file ImageFlowField.cs.

23  {
24  _sprite = new Sprite();
25  }

Member Function Documentation

◆ ColorToDirection()

Vector2 Agents.ImageFlowField.ColorToDirection ( Color  color)
inlineprotected

Convert color to direction.

Parameters
colorColor
Returns
Direction

Definition at line 63 of file ImageFlowField.cs.

64  {
65  // Use red component as a reference.
66  var angle = MathUtils.Map(color.r, 0, 1, 0, Mathf.Pi / 2);
67  return new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
68  }
Math utility functions
Definition: MathUtils.cs:7
static float Map(float value, float istart, float istop, float ostart, float ostop)
Map a value from one bound to another.
Definition: MathUtils.cs:17

◆ ComputeDirectionFromPosition()

override Vector2 Agents.ImageFlowField.ComputeDirectionFromPosition ( int  x,
int  y 
)
inlineprotectedvirtual

Compute flow direction from a given position. Defaults with a right vector.

Parameters
xX coordinate
yY coordinate
Returns
Flow direction

Reimplemented from Agents.SimpleFlowField.

Definition at line 70 of file ImageFlowField.cs.

71  {
72  var res = Resolution / TextureScale;
73  var image = SourceTexture.GetData();
74  image.Lock();
75  // Get middle pixel
76  var color = image.GetPixel((x * res) + (res / 2), (y * res) + (res / 2));
77  image.Unlock();
78  return ColorToDirection(color);
79  }
Vector2 ColorToDirection(Color color)
Convert color to direction.
Texture SourceTexture
Source texture
int TextureScale
Texture scale
int Resolution
Grid resolution

◆ CreateFieldFromTexture()

void Agents.ImageFlowField.CreateFieldFromTexture ( )
inlineprotected

Create field from source texture.

Definition at line 30 of file ImageFlowField.cs.

31  {
32  var texSize = SourceTexture.GetSize() * TextureScale;
33  var resolutionSize = new Vector2(Resolution, Resolution);
34  size = texSize;
35  cols = (int)(texSize.x / Resolution);
36  rows = (int)(texSize.y / Resolution);
37  field = new FlowDirection[cols * rows];
38 
39  for (int j = 0; j < rows; ++j)
40  {
41  for (int i = 0; i < cols; ++i)
42  {
43  var idx = i + (j * cols);
44  var direction = new FlowDirection()
45  {
46  RectSize = resolutionSize,
47  RectPosition = new Vector2(i * Resolution, j * Resolution),
48  RectPivotOffset = resolutionSize / 2.0f,
49  Direction = ComputeDirectionFromPosition(i, j),
50  };
51  direction.RectRotation = Mathf.Rad2Deg(direction.Direction.Angle());
52  field[idx] = direction;
53  AddChild(direction);
54  }
55  }
56  }
override Vector2 ComputeDirectionFromPosition(int x, int y)
Compute flow direction from a given position. Defaults with a right vector.
FlowDirection[] field
Flow field
int cols
Column count

Member Data Documentation

◆ CenterOnScreen

bool Agents.ImageFlowField.CenterOnScreen

Center on screen

Definition at line 17 of file ImageFlowField.cs.

◆ SourceTexture

Texture Agents.ImageFlowField.SourceTexture

Source texture

Definition at line 11 of file ImageFlowField.cs.

Property Documentation

◆ TextureScale

int Agents.ImageFlowField.TextureScale = 1
getset

Texture scale

Definition at line 14 of file ImageFlowField.cs.

14 { get; set; } = 1;

The documentation for this class was generated from the following file: