Skip to content

Commit

Permalink
add bounce positions in order to calculate hit map indices properly
Browse files Browse the repository at this point in the history
  • Loading branch information
mwinkens committed Nov 9, 2024
1 parent d0fc4af commit 6898b03
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
19 changes: 18 additions & 1 deletion src/game/client/prediction/entities/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,24 @@ void CCharacter::DDRacePostCoreTick()
HandleSkippableTiles(CurrentIndex);

// handle Anti-Skip tiles
std::vector<int> vIndices = Collision()->GetMapIndices(m_PrevPos, m_Pos);
std::vector<int> vIndices;
if(m_Core.m_BouncePostions.empty())
{
vIndices = Collision()->GetMapIndices(m_PrevPos, m_Pos);
}
else
{
// handle bouncing being multiple straight lines
vIndices = Collision()->GetMapIndices(m_PrevPos, m_Core.m_BouncePostions.front());
for(size_t i = 0; i < m_Core.m_BouncePostions.size() - 1; ++i)
{
std::vector<int> vStepIndices = Collision()->GetMapIndices(m_Core.m_BouncePostions[i], m_Core.m_BouncePostions[i + 1]);
vIndices.insert(vIndices.end(), vStepIndices.begin(), vStepIndices.end());
}
std::vector<int> vLastStepIndices = Collision()->GetMapIndices(m_Core.m_BouncePostions.back(), m_Pos);
vIndices.insert(vIndices.end(), vLastStepIndices.begin(), vLastStepIndices.end());
}

if(!vIndices.empty())
for(int Index : vIndices)
HandleTiles(Index);
Expand Down
8 changes: 7 additions & 1 deletion src/game/collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ bool CCollision::TestBox(vec2 Pos, vec2 Size) const
return false;
}

void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elasticity, bool *pGrounded) const
void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elasticity, bool *pGrounded, std::vector<vec2> *pBouncePositions) const
{
// do the move
vec2 Pos = *pInoutPos;
Expand Down Expand Up @@ -564,13 +564,17 @@ void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elast
NewPos.y = Pos.y;
Vel.y *= -ElasticityY;
Hits++;
if(pBouncePositions)
pBouncePositions->push_back(NewPos);
}

if(TestBox(vec2(NewPos.x, Pos.y), Size))
{
NewPos.x = Pos.x;
Vel.x *= -ElasticityX;
Hits++;
if(pBouncePositions)
pBouncePositions->push_back(NewPos);
}

// neither of the tests got a collision.
Expand All @@ -583,6 +587,8 @@ void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elast
Vel.y *= -ElasticityY;
NewPos.x = Pos.x;
Vel.x *= -ElasticityX;
if(pBouncePositions)
pBouncePositions->push_back(NewPos);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/game/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CCollision
int IntersectLineTeleWeapon(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision, int *pTeleNr = nullptr) const;
int IntersectLineTeleHook(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision, int *pTeleNr = nullptr) const;
void MovePoint(vec2 *pInoutPos, vec2 *pInoutVel, float Elasticity, int *pBounces) const;
void MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elasticity, bool *pGrounded = nullptr) const;
void MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elasticity, bool *pGrounded = nullptr, std::vector<vec2> *pBouncePositions = nullptr) const;
bool TestBox(vec2 Pos, vec2 Size) const;

// DDRace
Expand Down
5 changes: 4 additions & 1 deletion src/game/gamecore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,13 @@ void CCharacterCore::Move()

vec2 OldVel = m_Vel;
bool Grounded = false;
m_BouncePostions.clear();

m_pCollision->MoveBox(&NewPos, &m_Vel, PhysicalSizeVec2(),
vec2(m_Tuning.m_GroundElasticityX,
m_Tuning.m_GroundElasticityY),
&Grounded);
&Grounded,
&m_BouncePostions);

if(Grounded)
{
Expand Down
1 change: 1 addition & 0 deletions src/game/gamecore.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class CCharacterCore

int m_Colliding;
bool m_LeftWall;
std::vector<vec2> m_BouncePostions;

// DDNet Character
void SetTeamsCore(CTeamsCore *pTeams);
Expand Down
19 changes: 18 additions & 1 deletion src/game/server/entities/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,24 @@ void CCharacter::DDRacePostCoreTick()
return;

// handle Anti-Skip tiles
std::vector<int> vIndices = Collision()->GetMapIndices(m_PrevPos, m_Pos);
std::vector<int> vIndices;
if(m_Core.m_BouncePostions.empty())
{
vIndices = Collision()->GetMapIndices(m_PrevPos, m_Pos);
}
else
{
// handle bouncing being multiple straight lines
vIndices = Collision()->GetMapIndices(m_PrevPos, m_Core.m_BouncePostions.front());
for(size_t i = 0; i < m_Core.m_BouncePostions.size() - 1; ++i)
{
std::vector<int> vStepIndices = Collision()->GetMapIndices(m_Core.m_BouncePostions[i], m_Core.m_BouncePostions[i + 1]);
vIndices.insert(vIndices.end(), vStepIndices.begin(), vStepIndices.end());
}
std::vector<int> vLastStepIndices = Collision()->GetMapIndices(m_Core.m_BouncePostions.back(), m_Pos);
vIndices.insert(vIndices.end(), vLastStepIndices.begin(), vLastStepIndices.end());
}

if(!vIndices.empty())
{
for(int &Index : vIndices)
Expand Down

0 comments on commit 6898b03

Please sign in to comment.