Skip to content

Commit

Permalink
Basically the same as before except now the props can't be scaled bec…
Browse files Browse the repository at this point in the history
…ause our InvertedTR() method doesn't support it. That's okay though, most real world applications won't use it.
  • Loading branch information
BSVino committed Aug 1, 2013
1 parent 70c1ca8 commit a05bf3d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 60 deletions.
36 changes: 14 additions & 22 deletions game/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ void CCharacter::BuildTransform()
mRotation.SetRotation(m_flRotationTheta, m_vecRotationAxis);
mTranslation.SetTranslation(m_vecTranslation);
m_mTransform = mTranslation * mRotation * mScaling;

// Produce an inverse transformation matrix from three inverse TRS matrices.
// Order still matters! http://youtu.be/onSyW44OnxA
Matrix4x4 mScalingInverse, mRotationInverse, mTranslationInverse;
mScalingInverse.SetScale(1/m_vecScaling);
mRotationInverse = mRotation.Transposed();
mTranslationInverse.SetTranslation(-m_vecTranslation);
m_mTransformInverse = mScalingInverse * mRotationInverse * mTranslationInverse;
}

void CCharacter::ShotEffect(CRenderingContext* c)
Expand All @@ -105,24 +97,24 @@ void CCharacter::ShotEffect(CRenderingContext* c)

void CCharacter::TakeDamage(int iDamage)
{
if (!m_bTakesDamage)
return;

m_iHealth -= iDamage;

if (m_iHealth <= 0)
{
if (m_bTakesDamage)
{
// Spawn another baddy to take this guy's place.
CCharacter* pNew = Game()->CreateCharacter();

// Position the new monster in a random spot near the player.
pNew->SetTransform(Vector(1, 1, 1), 0, Vector(0, 1, 0), Vector((float)(rand()%20)-10, 0, (float)(rand()%20)-10));

pNew->m_aabbSize.vecMin = Vector(-1, 0, -1);
pNew->m_aabbSize.vecMax = Vector(1, 2, 1);
pNew->m_iBillboardTexture = Game()->GetMonsterTexture();
pNew->m_bEnemyAI = true;
pNew->m_bTakesDamage = true;
}
// Spawn another baddy to take this guy's place.
CCharacter* pNew = Game()->CreateCharacter();

// Position the new monster in a random spot near the player.
pNew->SetTransform(Vector(1, 1, 1), 0, Vector(0, 1, 0), Vector((float)(rand()%20)-10, 0, (float)(rand()%20)-10));

pNew->m_aabbSize.vecMin = Vector(-1, 0, -1);
pNew->m_aabbSize.vecMax = Vector(1, 2, 1);
pNew->m_iBillboardTexture = Game()->GetMonsterTexture();
pNew->m_bEnemyAI = true;
pNew->m_bTakesDamage = true;

// We're at zero health, time to die.
Game()->RemoveCharacter(this);
Expand Down
1 change: 0 additions & 1 deletion game/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class CCharacter
Vector m_vecRotationAxis;
float m_flRotationTheta;
Matrix4x4 m_mTransform;
Matrix4x4 m_mTransformInverse;
Vector m_vecMovement;
Vector m_vecMovementGoal;
Vector m_vecVelocity;
Expand Down
15 changes: 8 additions & 7 deletions game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@ bool CGame::TraceLine(const Vector& v0, const Vector& v1, Vector& vecIntersectio
if (!pCharacter->m_bHitByTraces)
continue;

Matrix4x4 mInverse = pCharacter->m_mTransform.InvertedTR();

// The v0 and v1 are in the global coordinate system and we need to transform it to the target's
// local coordinate system to use axis-aligned intersection. We do so using the inverse transform matrix.
// http://youtu.be/-Fn4atv2NsQ
if (LineAABBIntersection(pCharacter->m_aabbSize, pCharacter->m_mTransformInverse*v0, pCharacter->m_mTransformInverse*v1, vecTestIntersection, flTestFraction) && flTestFraction < flLowestFraction)
if (LineAABBIntersection(pCharacter->m_aabbSize, mInverse*v0, mInverse*v1, vecTestIntersection, flTestFraction) && flTestFraction < flLowestFraction)
{
// Once we have the result we can use the regular transform matrix to get it back in
// global coordinates. http://youtu.be/-Fn4atv2NsQ
Expand Down Expand Up @@ -276,7 +278,6 @@ void CGame::Update(float dt)
// Produce a transformation matrix from our three TRS matrices.
// Order matters! http://youtu.be/7pe1xYzFCvA
m_hPlayer->m_mTransform = mPlayerTranslation * mPlayerRotation * mPlayerScaling;
m_hPlayer->m_mTransformInverse = m_hPlayer->m_mTransform.InvertedRT();

float flMonsterSpeed = 0.5f;
for (size_t i = 0; i < MAX_CHARACTERS; i++)
Expand Down Expand Up @@ -594,13 +595,13 @@ void CGame::GameLoop()
m_hPlayer->m_bTakesDamage = true;

m_hPropEuler = CreateCharacter();
m_hPropEuler->SetTransform(Vector(2, 0.5f, 0.5f), 0, Vector(0, 1, 0), Vector(0, 3, 4));
m_hPropEuler->SetTransform(Vector(1, 1, 1), 0, Vector(0, 1, 0), Vector(0, 3, 4));
m_hPropEuler->m_aabbSize.vecMin = Vector(-1, -1, -1);
m_hPropEuler->m_aabbSize.vecMax = Vector(1, 1, 1);
m_hPropEuler->m_clrRender = Color(0.4f, 0.8f, 0.2f, 1.0f);

m_hPropQuaternion = CreateCharacter();
m_hPropQuaternion->SetTransform(Vector(2, 0.5f, 0.5f), 0, Vector(0, 1, 0), Vector(0, 3, -4));
m_hPropQuaternion->SetTransform(Vector(1, 1, 1), 0, Vector(0, 1, 0), Vector(0, 3, -4));
m_hPropQuaternion->m_aabbSize.vecMin = Vector(-1, -1, -1);
m_hPropQuaternion->m_aabbSize.vecMax = Vector(1, 1, 1);
m_hPropQuaternion->m_clrRender = Color(0.4f, 0.8f, 0.2f, 1.0f);
Expand All @@ -609,13 +610,13 @@ void CGame::GameLoop()
Vector vecPropMax = Vector(1, 2, 1);

CCharacter* pProp1 = CreateCharacter();
pProp1->SetTransform(Vector(2, 1, 4), 20, Vector(0, 1, 0), Vector(18, 0, 10));
pProp1->SetTransform(Vector(1, 1, 1), 20, Vector(0, 1, 0), Vector(18, 0, 10));
pProp1->m_aabbSize.vecMin = vecPropMin;
pProp1->m_aabbSize.vecMax = vecPropMax;
pProp1->m_clrRender = Color(0.4f, 0.8f, 0.2f, 1.0f);

CCharacter* pProp2 = CreateCharacter();
pProp2->SetTransform(Vector(1, 2, 3), 30, Vector(0, 1, 0), Vector(10, 0, 15));
pProp2->SetTransform(Vector(1, 1, 1), 30, Vector(0, 1, 0), Vector(10, 0, 15));
pProp2->m_aabbSize.vecMin = vecPropMin;
pProp2->m_aabbSize.vecMax = vecPropMax;
pProp2->m_clrRender = Color(0.4f, 0.8f, 0.2f, 1.0f);
Expand All @@ -627,7 +628,7 @@ void CGame::GameLoop()
pProp3->m_clrRender = Color(0.4f, 0.8f, 0.2f, 1.0f);

CCharacter* pProp4 = CreateCharacter();
pProp4->SetTransform(Vector(2, 2, 2), 40, Vector(0, 1, 0), Vector(-2, 0, 14));
pProp4->SetTransform(Vector(1, 1, 1), 40, Vector(0, 1, 0), Vector(-2, 0, 14));
pProp4->m_aabbSize.vecMin = vecPropMin;
pProp4->m_aabbSize.vecMax = vecPropMax;
pProp4->m_clrRender = Color(0.4f, 0.8f, 0.2f, 1.0f);
Expand Down
45 changes: 17 additions & 28 deletions math/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,41 +537,30 @@ void Matrix4x4::SetRightVector(const Vector& v)
m[2][2] = v.z;
}

// Not a true inversion, only works if the matrix is a translation/rotation matrix.
void Matrix4x4::InvertRT()
// Use the information embedded in a matrix to create its inverse.
// http://youtu.be/7CxKAtWqHC8
Matrix4x4 Matrix4x4::InvertedTR() const
{
TAssert(fabs(GetForwardVector().LengthSqr() - 1) < 0.00001f);
// This method can only be used if the matrix is a translation/rotation matrix.
// The below asserts will trigger if this is not the case.
TAssert(fabs(GetForwardVector().LengthSqr() - 1) < 0.00001f); // Each basis vector should be length 1.
TAssert(fabs(GetUpVector().LengthSqr() - 1) < 0.00001f);
TAssert(fabs(GetRightVector().LengthSqr() - 1) < 0.00001f);
TAssert(fabs(GetForwardVector().Dot(GetUpVector())) < 0.0001f); // All vectors should be orthogonal.
TAssert(fabs(GetForwardVector().Dot(GetRightVector())) < 0.0001f);
TAssert(fabs(GetRightVector().Dot(GetUpVector())) < 0.0001f);

Matrix4x4 t;
Matrix4x4 M;

for (int h = 0; h < 3; h++)
for (int v = 0; v < 3; v++)
t.m[h][v] = m[v][h];
// Create the transposed upper 3x3 matrix
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
M.m[i][j] = m[j][i];

Vector vecTranslation = GetTranslation();
// The new matrix translation = -Rt
M.SetTranslation(-(M*GetTranslation()));

Init(t);

SetTranslation(t*(-vecTranslation));
}

Matrix4x4 Matrix4x4::InvertedRT() const
{
TAssert(fabs(GetForwardVector().LengthSqr() - 1) < 0.00001f);
TAssert(fabs(GetUpVector().LengthSqr() - 1) < 0.00001f);
TAssert(fabs(GetRightVector().LengthSqr() - 1) < 0.00001f);

Matrix4x4 r;

for (int h = 0; h < 3; h++)
for (int v = 0; v < 3; v++)
r.m[h][v] = m[v][h];

r.SetTranslation(r*(-GetTranslation()));

return r;
return M;
}

float Matrix4x4::Trace() const
Expand Down
8 changes: 6 additions & 2 deletions math/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ class Matrix4x4
Vector GetUpVector() const { return Vector(m[1][0], m[1][1], m[1][2]); }
Vector GetRightVector() const { return Vector(m[2][0], m[2][1], m[2][2]); }

void InvertRT();
Matrix4x4 InvertedRT() const;
Matrix4x4 InvertedTR() const;

float Trace() const;

Expand All @@ -136,6 +135,11 @@ class Matrix4x4
struct MVector4D
{
float x, y, z, w;

float Length() const
{
return sqrt(x*x + y*y + z*z + w*w);
}
};

union {
Expand Down

0 comments on commit a05bf3d

Please sign in to comment.