Skip to content

Commit

Permalink
implement tuning values for elasticity
Browse files Browse the repository at this point in the history
implement tuning values for elasticity

add grounded check to movebox and give jump back when bouncing

fix styling issues

fix jumps being given back at ceilings

(cherry picked from commit DDNet commit e1ba77d3dce3ca16b63a7dcb66dfa6e5386ae6a8)
  • Loading branch information
AssassinTee authored and Kaffeine committed Feb 16, 2024
1 parent 5fefb34 commit e20075d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/game/collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ bool CCollision::TestBox(vec2 Pos, vec2 Size) const
return false;
}

void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elasticity) const
void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elasticity, bool *pGrounded) const
{
// do the move
vec2 Pos = *pInoutPos;
Expand All @@ -348,7 +348,10 @@ void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elas

if(Distance > 0.00001f)
{
float Fraction = 1.0f/(float)(Max+1);
float Fraction = 1.0f / (float)(Max + 1);
float ElasticityX = clamp(Elasticity.x, -1.0f, 1.0f);
float ElasticityY = clamp(Elasticity.y, -1.0f, 1.0f);

for(int i = 0; i <= Max; i++)
{
// Early break as optimization to stop checking for collisions for
Expand All @@ -374,26 +377,30 @@ void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elas

if(TestBox(vec2(Pos.x, NewPos.y), Size))
{
if(pGrounded && ElasticityY > 0 && Vel.y > 0)
*pGrounded = true;
NewPos.y = Pos.y;
Vel.y *= -Elasticity;
Vel.y *= -ElasticityY;
Hits++;
}

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

// neither of the tests got a collision.
// this is a real _corner case_!
if(Hits == 0)
{
if(pGrounded && ElasticityY > 0 && Vel.y > 0)
*pGrounded = true;
NewPos.y = Pos.y;
Vel.y *= -Elasticity;
Vel.y *= -ElasticityY;
NewPos.x = Pos.x;
Vel.x *= -Elasticity;
Vel.x *= -ElasticityX;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/game/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CCollision
int GetHeight() const { return m_Height; };
int IntersectLine(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision = nullptr, vec2 *pOutBeforeCollision = nullptr) const;
void MovePoint(vec2 *pInoutPos, vec2 *pInoutVel, float Elasticity, int *pBounces) const;
void MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elasticity) const;
void MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elasticity, bool *pGrounded = nullptr) const;
bool TestBox(vec2 Pos, vec2 Size) const;

void Dest();
Expand Down
13 changes: 12 additions & 1 deletion src/game/gamecore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,18 @@ void CCharacterCore::Move(CParams* pParams)
NewPos.y -= PosDiff;
}

m_pCollision->MoveBox(&NewPos, &m_Vel, Size, 0);
bool Grounded = false;
m_pCollision->MoveBox(&NewPos, &m_Vel, Size,
vec2(pTuningParams->m_GroundElasticityX,
pTuningParams->m_GroundElasticityY),
&Grounded);

if(Grounded)
{
m_Jumped &= ~2;
m_JumpedTotal = 0;
}

NewPos.y += PosDiff;

m_Vel.x = m_Vel.x*(1.0f/RampValue);
Expand Down
3 changes: 2 additions & 1 deletion src/game/server/entities/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ void CCharacter::HandleWaterJump()
if(m_DartLifeSpan > 0)
{
m_Core.m_Vel = m_DartDir * 15.0f;
GameServer()->Collision()->MoveBox(&m_Core.m_Pos, &m_Core.m_Vel, vec2(m_ProximityRadius, m_ProximityRadius), 0.f);
const vec2 GroundElasticity{};
GameServer()->Collision()->MoveBox(&m_Core.m_Pos, &m_Core.m_Vel, vec2(m_ProximityRadius, m_ProximityRadius), GroundElasticity);
m_Core.m_Vel = vec2(0.f, 0.f);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/game/server/infclass/entities/infccharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ void CInfClassCharacter::HandleNinjaMove(float NinjaVelocity)
{
SetVelocity(m_DartDir * NinjaVelocity);

GameServer()->Collision()->MoveBox(&m_Core.m_Pos, &m_Core.m_Vel, CCharacterCore::PhysicalSizeVec2(), 0.f);
const vec2 GroundElasticity{};
Collision()->MoveBox(&m_Core.m_Pos, &m_Core.m_Vel, vec2(GetProximityRadius(), GetProximityRadius()), GroundElasticity);

// reset velocity so the client doesn't predict stuff
ResetVelocity();
Expand Down
3 changes: 3 additions & 0 deletions src/game/tuning.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ MACRO_TUNING_PARAM(GrenadeFireDelay, grenade_fire_delay, 500, "Delay of firing g
MACRO_TUNING_PARAM(LaserFireDelay, laser_fire_delay, 800, "Delay of firing laser laser")
MACRO_TUNING_PARAM(NinjaFireDelay, ninja_fire_delay, 800, "Delay of firing ninja")
MACRO_TUNING_PARAM(HammerHitFireDelay, hammer_hit_fire_delay, 320, "Delay of hammering (when hitting another tee)")

MACRO_TUNING_PARAM(GroundElasticityX, ground_elasticity_x, 0, "Wall elasticity")
MACRO_TUNING_PARAM(GroundElasticityY, ground_elasticity_y, 0, "Ground/ceiling elasticity")

0 comments on commit e20075d

Please sign in to comment.