Skip to content

feat: Add translateByVector2 method to Matrix4 #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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
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
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