Skip to content

Commit 74ecb2b

Browse files
committed
A trigger area that turns the player red when he enters.
1 parent cd278d9 commit 74ecb2b

File tree

6 files changed

+44
-2
lines changed

6 files changed

+44
-2
lines changed

game/character.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CCharacter::CCharacter()
2828
m_bEnemyAI = false;
2929
m_flRotationTheta = 0;
3030
m_vecRotationAxis = Vector(0, 1, 0);
31+
m_vecScaling = Vector(1, 1, 1);
3132
m_bTakesDamage = false;
3233
m_bDrawTransparent = false;
3334
m_iHealth = 3;
@@ -216,3 +217,8 @@ const Vector CCharacter::GetGlobalView() const
216217

217218
return m_angView.ToVector();
218219
}
220+
221+
const AABB CCharacter::GetGlobalAABB() const
222+
{
223+
return m_aabbSize * m_vecScaling + GetGlobalOrigin();
224+
}

game/character.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class CCharacter
5757
void SetLocalView(const EAngle& angView);
5858
const Vector GetGlobalView() const;
5959

60+
const AABB GetGlobalAABB() const;
61+
6062
private:
6163
void BuildTransform();
6264

game/game.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ void CGame::Update(float dt)
299299

300300
pCharacter->SetTranslation(pCharacter->GetGlobalOrigin() + pCharacter->m_vecVelocity * dt);
301301
}
302+
303+
if (AABBIntersection(m_hTrigger->GetGlobalAABB(), m_hPlayer->GetGlobalAABB()))
304+
m_hPlayer->m_clrRender = Color(1.0f, 0.2f, 0.1f, 1.0f);
305+
else
306+
m_hPlayer->m_clrRender = Color(0.4f, 0.2f, 0.8f, 1.0f);
302307
}
303308

304309
void CGame::Draw()
@@ -500,6 +505,12 @@ void CGame::DrawCharacters(const std::vector<CCharacter*>& apRenderList, bool bT
500505
// http://youtu.be/7pe1xYzFCvA
501506
c.Transform(pCharacter->GetGlobalTransform());
502507

508+
if (pCharacter->m_bDrawTransparent)
509+
{
510+
c.SetAlpha(0.6f);
511+
c.SetBlend(BLEND_ALPHA);
512+
}
513+
503514
// Render the player-box
504515
c.RenderBox(pCharacter->m_aabbSize.vecMin, pCharacter->m_aabbSize.vecMax);
505516
}
@@ -598,7 +609,7 @@ void CGame::GameLoop()
598609
Vector vecMonsterMin = Vector(-1, 0, -1);
599610
Vector vecMonsterMax = Vector(1, 2, 1);
600611

601-
CCharacter* pTarget1 = CreateCharacter();
612+
/*CCharacter* pTarget1 = CreateCharacter();
602613
pTarget1->SetTransform(Vector(2, 2, 2), 0, Vector(0, 1, 0), Vector(6, 0, 6));
603614
pTarget1->m_aabbSize.vecMin = vecMonsterMin;
604615
pTarget1->m_aabbSize.vecMax = vecMonsterMax;
@@ -620,7 +631,7 @@ void CGame::GameLoop()
620631
pTarget3->m_aabbSize.vecMax = vecMonsterMax;
621632
pTarget3->m_iBillboardTexture = m_iMonsterTexture;
622633
pTarget3->m_bEnemyAI = true;
623-
pTarget3->m_bTakesDamage = true;
634+
pTarget3->m_bTakesDamage = true;*/
624635

625636
Vector vecPropMin = Vector(-1, 0, -1);
626637
Vector vecPropMax = Vector(1, 2, 1);
@@ -649,6 +660,13 @@ void CGame::GameLoop()
649660
pProp4->m_aabbSize.vecMax = vecPropMax;
650661
pProp4->m_clrRender = Color(0.4f, 0.8f, 0.2f, 1.0f);
651662

663+
m_hTrigger = CreateCharacter();
664+
m_hTrigger->SetTransform(Vector(10, 2, 4), 0, Vector(0, 1, 0), Vector(0, 0, -14));
665+
m_hTrigger->m_aabbSize.vecMin = vecPropMin;
666+
m_hTrigger->m_aabbSize.vecMax = vecPropMax;
667+
m_hTrigger->m_bDrawTransparent = true;
668+
m_hTrigger->m_clrRender = Color(0.8f, 0.4f, 0.2f, 0.5f);
669+
652670
float flPreviousTime = 0;
653671
float flCurrentTime = Application()->GetTime();
654672

game/game.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class CGame : public CApplication
104104

105105
// This is the player character
106106
CHandle m_hPlayer;
107+
CHandle m_hTrigger;
107108
};
108109

109110
inline CGame* Game()

math/collision.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ bool LineAABBIntersection(const AABB& aabbBox, const Vector& v0, const Vector& v
8484
return true;
8585
}
8686

87+
// Find the intersection of two axis-aligned bounding boxes http://youtu.be/ENuk9HgeTiI
88+
bool AABBIntersection(const AABB& a, const AABB& b)
89+
{
90+
for (int i = 0; i < 3; i++)
91+
{
92+
if (a.vecMin.v[i] > b.vecMax.v[i])
93+
return false;
94+
if (a.vecMax.v[i] < b.vecMin.v[i])
95+
return false;
96+
}
97+
98+
return true;
99+
}
100+
87101
// Line-Plane Intersection: http://youtu.be/fIu_8b2n8ZM
88102
bool LinePlaneIntersection(const Vector& n, const Vector& c, const Vector& x0, const Vector& x1, Vector& vecIntersection, float& flFraction)
89103
{

math/collision.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Vector;
2121
class AABB;
2222

2323
bool LineAABBIntersection(const AABB& aabbBox, const Vector& v0, const Vector& v1, Vector& vecIntersection, float& flFraction);
24+
bool AABBIntersection(const AABB& a, const AABB& b);
2425

2526
// n - plane normal
2627
// c - any point in the plane

0 commit comments

Comments
 (0)