forked from flame-engine/flame
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Matrix4 extension (flame-engine#838)
* Added Matrix4 extension * Added unit tests for Matrix4 extension class * Update doc/util.md Co-authored-by: Lukas Klingsbo <[email protected]> * Renamed transformVector2 to transform2
- Loading branch information
1 parent
39b6ade
commit 50ca95a
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import 'package:vector_math/vector_math_64.dart'; | ||
|
||
export 'package:vector_math/vector_math_64.dart' hide Colors; | ||
|
||
extension Matrix4Extension on Matrix4 { | ||
/// A first row and first column value. | ||
double get m11 => storage[0]; | ||
|
||
/// A first row and second column value. | ||
double get m12 => storage[1]; | ||
|
||
/// A first row and third column value. | ||
double get m13 => storage[2]; | ||
|
||
/// A first row and fourth column value. | ||
double get m14 => storage[3]; | ||
|
||
/// A second row and first column value. | ||
double get m21 => storage[4]; | ||
|
||
/// A second row and second column value. | ||
double get m22 => storage[5]; | ||
|
||
/// A second row and third column value. | ||
double get m23 => storage[6]; | ||
|
||
/// A second row and fourth column value. | ||
double get m24 => storage[7]; | ||
|
||
/// A third row and first column value. | ||
double get m31 => storage[8]; | ||
|
||
/// A third row and second column value. | ||
double get m32 => storage[9]; | ||
|
||
/// A third row and third column value. | ||
double get m33 => storage[10]; | ||
|
||
/// A third row and fourth column value. | ||
double get m34 => storage[11]; | ||
|
||
/// A fourth row and first column value. | ||
double get m41 => storage[12]; | ||
|
||
/// A fourth row and second column value. | ||
double get m42 => storage[13]; | ||
|
||
/// A fourth row and third column value. | ||
double get m43 => storage[14]; | ||
|
||
/// A fourth row and fourth column value. | ||
double get m44 => storage[15]; | ||
|
||
/// Translate this matrix by a [Vector2]. | ||
void translate2(Vector2 vector) { | ||
return translate(vector.x, vector.y); | ||
} | ||
|
||
/// Transform [position] of type [Vector2] using the transformation defined by | ||
/// this. | ||
Vector2 transform2(Vector2 position) { | ||
return Vector2( | ||
(position.x * m11) + (position.y * m21) + m41, | ||
(position.x * m12) + (position.y * m22) + m42, | ||
); | ||
} | ||
|
||
/// Transform a copy of [position] of type [Vector2] using the transformation | ||
/// defined by this. If a [out] parameter is supplied, the copy is stored in | ||
/// [out]. | ||
Vector2 transformed2(Vector2 position, [Vector2? out]) { | ||
if (out == null) { | ||
// ignore: parameter_assignments | ||
out = Vector2.copy(position); | ||
} else { | ||
out.setFrom(position); | ||
} | ||
return transform2(out); | ||
} | ||
|
||
/// Create a scaled matrix. | ||
/// | ||
/// Scale by a [Vector3], [Vector4], or x,y,z | ||
static Matrix4 scale(dynamic x, [double? y, double? z]) { | ||
return Matrix4.identity()..scale(x, y, z); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'package:flame/extensions.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../util/expect_vector2.dart'; | ||
|
||
void main() { | ||
group('Matrix4 test', () { | ||
final matrix4 = Matrix4.fromList([ | ||
1, 2, 3, 4, // first row | ||
5, 6, 7, 8, // second row | ||
9, 10, 11, 12, // third row | ||
13, 14, 15, 16, // fourth row | ||
]); | ||
|
||
test('test m11', () => expect(matrix4.m11, matrix4.storage[0])); | ||
test('test m12', () => expect(matrix4.m12, matrix4.storage[1])); | ||
test('test m13', () => expect(matrix4.m13, matrix4.storage[2])); | ||
test('test m14', () => expect(matrix4.m14, matrix4.storage[3])); | ||
test('test m21', () => expect(matrix4.m21, matrix4.storage[4])); | ||
test('test m22', () => expect(matrix4.m22, matrix4.storage[5])); | ||
test('test m23', () => expect(matrix4.m23, matrix4.storage[6])); | ||
test('test m24', () => expect(matrix4.m24, matrix4.storage[7])); | ||
test('test m31', () => expect(matrix4.m31, matrix4.storage[8])); | ||
test('test m32', () => expect(matrix4.m32, matrix4.storage[9])); | ||
test('test m33', () => expect(matrix4.m33, matrix4.storage[10])); | ||
test('test m34', () => expect(matrix4.m34, matrix4.storage[11])); | ||
test('test m41', () => expect(matrix4.m41, matrix4.storage[12])); | ||
test('test m42', () => expect(matrix4.m42, matrix4.storage[13])); | ||
test('test m43', () => expect(matrix4.m43, matrix4.storage[14])); | ||
test('test m44', () => expect(matrix4.m44, matrix4.storage[15])); | ||
|
||
test('test transform2', () { | ||
final matrix4 = Matrix4.translation(Vector3(0, 10, 0)); | ||
final input = Vector2.all(10); | ||
|
||
expectVector2(matrix4.transform2(input), Vector2(10, 20)); | ||
}); | ||
|
||
test('test transformed2', () { | ||
final matrix4 = Matrix4.translation(Vector3(0, 10, 0)); | ||
final input = Vector2.all(10); | ||
final out = Vector2.zero(); | ||
final result = matrix4.transformed2(input, out); | ||
|
||
expectVector2(out, input); | ||
expectVector2(result, Vector2(10, 20)); | ||
}); | ||
}); | ||
} |