Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2.2.1-wip
## 2.3.0-wip

- Add translateByVector2 method to Matrix4.
- Require `sdk: ^3.7.0`.

## 2.2.0
Expand Down
7 changes: 7 additions & 0 deletions lib/src/vector_math/matrix4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,13 @@ class Matrix4 {
_m4storage[15] = t4;
}

/// Translate this matrix by a [Vector2].
@pragma('wasm:prefer-inline')
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
void translateByVector2(Vector2 v2) =>
translateByDouble(v2.x, v2.y, 0.0, 1.0);

/// Translate this matrix by a [Vector3].
@pragma('wasm:prefer-inline')
@pragma('vm:prefer-inline')
Expand Down
7 changes: 7 additions & 0 deletions lib/src/vector_math_64/matrix4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,13 @@ class Matrix4 {
_m4storage[15] = t4;
}

/// Translate this matrix by a [Vector2].
@pragma('wasm:prefer-inline')
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
void translateByVector2(Vector2 v2) =>
translateByDouble(v2.x, v2.y, 0.0, 1.0);

/// Translate this matrix by a [Vector3].
@pragma('wasm:prefer-inline')
@pragma('vm:prefer-inline')
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: vector_math
version: 2.2.1-wip
version: 2.3.0-wip
description: A Vector Math library for 2D and 3D applications.
repository: https://github.com/google/vector_math.dart

Expand Down
19 changes: 13 additions & 6 deletions test/matrix4_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,19 @@ void testMatrix4SelfMultiplyTranspose() {
void testMatrix4Translation() {
final inputA = <Matrix4>[];
final inputB = <Matrix4>[];
final output1 = <Matrix4>[];
final output2 = <Matrix4>[];
final outputA = <Matrix4>[];
final outputB = <Matrix4>[];

inputA.add(Matrix4.identity());
inputB.add(Matrix4.translationValues(1.0, 3.0, 5.7));
output1.add(inputA[0] * inputB[0] as Matrix4);
output2.add((Matrix4.identity())..translate(1.0, 3.0, 5.7));
outputA.add(inputA[0] * inputB[0] as Matrix4);
outputB.add((Matrix4.identity())..translate(1.0, 3.0, 5.7));

assert(inputA.length == inputB.length);
assert(output1.length == output2.length);
assert(outputA.length == outputB.length);

for (var i = 0; i < inputA.length; i++) {
relativeTest(output1[i], output2[i]);
relativeTest(outputA[i], outputB[i]);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I shifted the variable names a bit here to make it match some sort of logic with the new names, but please lmk if there is a preferred naming convention.

tbh this test feels like two tests in one but I don't want to change the structure too much.


final input = Matrix4.fromList([
Expand All @@ -452,6 +452,13 @@ void testMatrix4Translation() {
3, 7, 11, 15, //
4, 8, 12, 16, //
]);
final output2 = input.clone();
output2[12] = input.dotRow(0, Vector4(4, 8, 0, 1));
output2[13] = input.dotRow(1, Vector4(4, 8, 0, 1));
output2[14] = input.dotRow(2, Vector4(4, 8, 0, 1));
output2[15] = input.dotRow(3, Vector4(4, 8, 0, 1));
relativeTest(input.clone()..translateByVector2(Vector2(4.0, 8.0)), output2);

final output3 = input.clone();
output3[12] = input.dotRow(0, Vector4(4, 8, 12, 1));
output3[13] = input.dotRow(1, Vector4(4, 8, 12, 1));
Expand Down