small racing game im working on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
## Matrix calculations.
##
## This class allows to perform all the basic operations with matrices.
## (add, sub, multiply, scalar, dot product)
## It is also possible to import and export matrices from an array.
class_name Matrix

var rows: int
var cols: int

var data = []


func _init(_rows: int,_cols: int,value: float = 0.0):
	randomize()
	rows = _rows
	cols = _cols
	for row in range(rows):
		data.insert(row , [])
		for col in range(cols):
			data[row].insert(col, value)


static func from_array(arr: Array[float]) -> Matrix:
	var result = Matrix.new(arr.size(), 1)
	for row in range(result.rows):
		result.data[row][0] = arr[row]

	return result


static func to_array(matrix: Matrix) -> Array[float]:
	var result = []
	for row in range(matrix.rows):
		for col in range(matrix.cols):
			result.append(matrix.data[row][col])

	return result


static func rand(matrix: Matrix) -> Matrix:
	randomize()

	var result = Matrix.new(matrix.rows, matrix.cols)

	for row in range(result.rows):
		for col in range(result.cols):
			result.data[row][col] = randf_range(-1, 1)

	return result


static func add(a: Matrix, b: Matrix) -> Matrix:
	assert(a.rows == b.rows and a.cols == b.cols)

	var result = Matrix.new(a.rows, a.cols)

	for row in range(result.rows):
		for col in range(result.cols):
			result.data[row][col] = a.data[row][col] + b.data[row][col]

	return result


static func subtract(a: Matrix, b: Matrix) -> Matrix:
	assert(a.rows == b.rows and a.cols == b.cols)

	var result = Matrix.new(a.rows, a.cols)

	for row in range(result.rows):
		for col in range(result.cols):
			result.data[row][col] = a.data[row][col] - b.data[row][col]

	return result


static func scalar(matrix: Matrix, value: float) -> Matrix:
	var result = Matrix.new(matrix.rows, matrix.cols)

	for row in range(result.rows):
		for col in range(result.cols):
			result.data[row][col] = matrix.data[row][col] * value

	return result


static func product(a: Matrix, b: Matrix) -> Matrix:
	assert(a.cols == b.rows)

	var result = Matrix.new(a.rows, b.cols)

	for row in range(result.rows):
		for col in range(result.cols):
			result.data[row][col] = 0.0
			for k in range(a.cols):
				result.data[row][col] += a.data[row][k] * b.data[k][col]

	return result


static func multiply(a: Matrix, b: Matrix) -> Matrix:
	assert(a.rows == b.rows and a.cols == b.cols)

	var result = Matrix.new(a.rows, a.cols)

	for row in range(result.rows):
		for col in range(result.cols):
			result.data[row][col] = a.data[row][col] * b.data[row][col]

	return result


static func random(a: Matrix, b: Matrix) -> Matrix:
	randomize()
	var result = Matrix.new(a.rows, a.cols)
	for row in range(result.rows):
		for col in range(result.cols):
			result.data[row][col] = a.data[row][col] if randf_range(0, 1) > 0.5 else b.data[row][col]

	return result


static func transpose(matrix: Matrix) -> Matrix:
	var result = Matrix.new(matrix.cols, matrix.rows)

	for row in range(result.rows):
		for col in range(result.cols):
			result.data[row][col] = matrix.data[col][row]

	return result


static func copy(matrix: Matrix) -> Matrix:
	var result = Matrix.new(matrix.rows, matrix.cols)
	for row in range(result.rows):
		for col in range(result.cols):
			result.data[row][col] = matrix.data[row][col]
	return result


static func map(matrix: Matrix, callback) -> Matrix:
	var result = Matrix.new(matrix.rows, matrix.cols)

	for row in range(result.rows):
		for col in range(result.cols):
			result.data[row][col] = callback.call(matrix.data[row][col], row, col)

	return result