• Create Account

# Finalspace

Member Since 29 Mar 2012
Offline Last Active Today, 05:55 AM

### In Topic: Is my matrix math busted - it probably is?

Today, 05:32 AM

This will do:

```internal void TestMath() {
constant F32 allowedError = 0.00001f;
constant F32 cs45 = 0.707106769f; // NOTE(finaL): 45 degrees unit axis scalar

F32 sResult;

Assert(Equals(sResult, PI32 * 0.25f, allowedError));
Assert(Equals(sResult, 45.0f, allowedError));

sResult = ToAngle(V2(0, 1));
Assert(Equals(sResult, PI32 * 0.5f, allowedError));
sResult = ToAngle(V2(0, -1));
Assert(Equals(sResult, PI32 * -0.5f, allowedError));
sResult = ToAngle(V2(1, 0));
Assert(Equals(sResult, 0, allowedError));
sResult = ToAngle(V2(-1, 0));
Assert(Equals(sResult, PI32, allowedError));
sResult = ToAngle(V2(cs45, cs45));
Assert(Equals(sResult, PI32 * 0.25f, allowedError));

Mat2f rotIdentity = MakeIdentity2();
Assert(
Equals(rotIdentity.col1.x, 1, allowedError)
&& Equals(rotIdentity.col1.y, 0, allowedError)
&& Equals(rotIdentity.col2.x, 0, allowedError)
&& Equals(rotIdentity.col2.y, 1, allowedError));

Mat2f rot90 = MakeRotation2(PI32 * 0.5f);
Assert(
Equals(rot90.col1.x, 0, allowedError)
&& Equals(rot90.col1.y, 1, allowedError)
&& Equals(rot90.col2.x, -1, allowedError)
&& Equals(rot90.col2.y, 0, allowedError));

Mat2f rot45 = MakeRotation2(PI32 * 0.25f);
Assert(
Equals(rot45.col1.x, 0.707106769f, allowedError)
&& Equals(rot45.col1.y, 0.707106769f, allowedError)
&& Equals(rot45.col2.x, -0.707106769f, allowedError)
&& Equals(rot45.col2.y, 0.707106769f, allowedError));

Mat2f rot90Inverse = Transpose2(rot90);
Assert(
Equals(rot90Inverse.col1.x, 0, allowedError)
&& Equals(rot90Inverse.col1.y, -1, allowedError)
&& Equals(rot90Inverse.col2.x, 1, allowedError)
&& Equals(rot90Inverse.col2.y, 0, allowedError));

Vec2f v, vResult;
Mat2f mResult;

v = V2(100, 25);

// NOTE(final): Matrix * vector is equal to vector * matrix
vResult = rotIdentity * v;
Assert(Equals(vResult.x, 100.0f, allowedError) && Equals(vResult.y, 25.0f, allowedError));
vResult = v * rotIdentity;
Assert(Equals(vResult.x, 100.0f, allowedError) && Equals(vResult.y, 25.0f, allowedError));

vResult = rot90 * v;
Assert(Equals(vResult.x, -25.0f, allowedError) && Equals(vResult.y, 100.0f, allowedError));
vResult = v * rot90;
Assert(Equals(vResult.x, -25.0f, allowedError) && Equals(vResult.y, 100.0f, allowedError));

vResult = rot90Inverse * v;
Assert(Equals(vResult.x, 25.0f, allowedError) && Equals(vResult.y, -100.0f, allowedError));
vResult = v * rot90Inverse;
Assert(Equals(vResult.x, 25.0f, allowedError) && Equals(vResult.y, -100.0f, allowedError));

mResult = rot90Inverse * rot90;
Assert(
Equals(mResult.col1.x, 1, allowedError)
&& Equals(mResult.col1.y, 0, allowedError)
&& Equals(mResult.col2.x, 0, allowedError)
&& Equals(mResult.col2.y, 1, allowedError));

mResult = rot90 * rot90Inverse;
Assert(
Equals(mResult.col1.x, 1, allowedError)
&& Equals(mResult.col1.y, 0, allowedError)
&& Equals(mResult.col2.x, 0, allowedError)
&& Equals(mResult.col2.y, 1, allowedError));

mResult = rot45 * rot90;
Assert(
Equals(mResult.col1.x, -cs45, allowedError)
&& Equals(mResult.col1.y, cs45, allowedError)
&& Equals(mResult.col2.x, -cs45, allowedError)
&& Equals(mResult.col2.y, -cs45, allowedError));
}

```

### In Topic: Is my matrix math busted - it probably is?

Today, 05:08 AM

Found the culprit, the 2x2 mult was busted:

This is totally wrong - what was i thinking:

```inline Mat2f operator *(const Mat2f &a, const Mat2f &b)
{
Mat2f result = {};
result.col1 = a * b.col1;
result.col2 = a * b.col2;
return(result);
}

```
This is correct:
```inline Mat2f operator *(const Mat2f &a, const Mat2f &b)
{
Mat2f result = {};
for (U32 i = 0; i < 2; ++i) {
for (U32 j = 0; j < 2; ++j) {
F32 sum = 0;
for (U32 k = 0; k < 2; ++k) {
sum += a.m[i * 2 + k] * b.m[k * 2 + j];
}
result.m[i * 2 + j] = sum;
}
}
return(result);
}
```

You might want to check your Matrix*vector code, your original code was correct. By rewriting it you just bypassed a bug in your Matrix*vector code. Your M*V code should dot the rows of the matrix with the vector, not the columns. My guess is that you just copied the V*M code (which is the same as transpose(M)*V), without swapping rows/columns.

Oh thanks, you are right. The matrix * vector was wrong and the multiplication was correct. Fixed it. Need to add a unit test for this - so this will never happen again.

### In Topic: Is my matrix math busted - it probably is?

Yesterday, 02:59 PM

Found the culprit, the 2x2 mult was busted:

This is totally wrong - what was i thinking:
```inline Mat2f operator *(const Mat2f &a, const Mat2f &b)
{
Mat2f result = {};
result.col1 = a * b.col1;
result.col2 = a * b.col2;
return(result);
}

```
This is correct:
```inline Mat2f operator *(const Mat2f &a, const Mat2f &b)
{
Mat2f result = {};
for (U32 i = 0; i < 2; ++i) {
for (U32 j = 0; j < 2; ++j) {
F32 sum = 0;
for (U32 k = 0; k < 2; ++k) {
sum += a.m[i * 2 + k] * b.m[k * 2 + j];
}
result.m[i * 2 + j] = sum;
}
}
return(result);
}
```

### In Topic: Fixing '*': operator has no effect ?

Yesterday, 12:40 PM

There are just some defines, no real macros at all - so the compiler should just include that expanded in the translation unit, isn´t it?.

#define CONTACT_FEATURE_FACEA_PRIME 3;
#define CONTACT_FEATURE_FACEB_PRIME 5;
#define CONTACT_FEATURE_CLIP_PRIME 11;
#define CONTACT_FEATURE_SWAP_PRIME 17;

*damn: There was semicolons at the end... problem solved.

### In Topic: Platformer style tutorial. How to import files from tiled etc?

09 September 2016 - 08:06 AM

Google for "speculative contacts platformer" and you will find a very good resource.

PARTNERS