Skip to content

✨ Add tuning variables for elasticity #3136 #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: community
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/game/collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ bool CCollision::TestBox(vec2 Pos, vec2 Size, int Flag) const
return false;
}

void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elasticity, bool *pDeath) const
void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elasticity, bool *pDeath, bool* pGrounded) const
{
// do the move
vec2 Pos = *pInoutPos;
Expand All @@ -159,6 +159,9 @@ void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elas
if(Distance > 0.00001f)
{
const float Fraction = 1.0f/(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++)
{
vec2 NewPos = Pos + Vel*Fraction; // TODO: this row is not nice
Expand All @@ -176,26 +179,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 @@ -32,7 +32,7 @@ class CCollision
int GetHeight() const { return m_Height; }
int IntersectLine(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const;
void MovePoint(vec2 *pInoutPos, vec2 *pInoutVel, float Elasticity, int *pBounces) const;
void MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elasticity, bool *pDeath=0) const;
void MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elasticity, bool *pDeath=0, bool* pGrounded=0) const;
bool TestBox(vec2 Pos, vec2 Size, int Flag=COLFLAG_SOLID) const;
};

Expand Down
5 changes: 4 additions & 1 deletion src/game/gamecore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ void CCharacterCore::Move()
m_Vel.x = m_Vel.x*RampValue;

vec2 NewPos = m_Pos;
m_pCollision->MoveBox(&NewPos, &m_Vel, vec2(PHYS_SIZE, PHYS_SIZE), 0, &m_Death);
bool Grounded = false;
m_pCollision->MoveBox(&NewPos, &m_Vel, vec2(PHYS_SIZE, PHYS_SIZE), vec2(m_pWorld->m_Tuning.m_PlayerElasticityX, m_pWorld->m_Tuning.m_PlayerElasticityY), &m_Death, &Grounded);
if(Grounded)
m_Jumped &= ~2;

m_Vel.x = m_Vel.x*(1.0f/RampValue);

Expand Down
2 changes: 1 addition & 1 deletion src/game/server/entities/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void CCharacter::HandleNinja()
// Set velocity
m_Core.m_Vel = m_Ninja.m_ActivationDir * g_pData->m_Weapons.m_Ninja.m_Velocity;
vec2 OldPos = m_Pos;
GameServer()->Collision()->MoveBox(&m_Core.m_Pos, &m_Core.m_Vel, vec2(GetProximityRadius(), GetProximityRadius()), 0.f);
GameServer()->Collision()->MoveBox(&m_Core.m_Pos, &m_Core.m_Vel, vec2(GetProximityRadius(), GetProximityRadius()), vec2(GameServer()->Tuning()->m_PlayerElasticityX, GameServer()->Tuning()->m_PlayerElasticityY));

// reset velocity so the client doesn't predict stuff
m_Core.m_Vel = vec2(0.f, 0.f);
Expand Down
2 changes: 1 addition & 1 deletion src/game/server/entities/flag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void CFlag::TickDefered()
else
{
m_Vel.y += GameWorld()->m_Core.m_Tuning.m_Gravity;
GameServer()->Collision()->MoveBox(&m_Pos, &m_Vel, vec2(ms_PhysSize, ms_PhysSize), 0.5f);
GameServer()->Collision()->MoveBox(&m_Pos, &m_Vel, vec2(ms_PhysSize, ms_PhysSize), vec2(GameWorld()->m_Core.m_Tuning.m_FlagElasticity, GameWorld()->m_Core.m_Tuning.m_FlagElasticity));
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/game/tuning.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ MACRO_TUNING_PARAM(LaserBounceCost, laser_bounce_cost, 0)

MACRO_TUNING_PARAM(PlayerCollision, player_collision, 1)
MACRO_TUNING_PARAM(PlayerHooking, player_hooking, 1)

MACRO_TUNING_PARAM(PlayerElasticityX, player_elasticity_x, 0.0f)
MACRO_TUNING_PARAM(PlayerElasticityY, player_elasticity_y, 0.0f)
MACRO_TUNING_PARAM(FlagElasticity, flag_elasticity, 0.5f)
#endif