Skip to content

Commit

Permalink
Add gamemode THE_VOID
Browse files Browse the repository at this point in the history
  • Loading branch information
ZealanL committed Mar 28, 2023
1 parent 99bb6c3 commit 12cbccf
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 53 deletions.
125 changes: 77 additions & 48 deletions src/Sim/Arena/Arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Car* Arena::GetCarFromID(uint32_t id) {
}

void Arena::SetGoalScoreCallback(GoalScoreEventFn callbackFunc, void* userInfo) {
if (gameMode == GameMode::THE_VOID)
RS_ERR_CLOSE("Cannot set a goal score callback when on THE_VOID gamemode!");

_goalScoreCallback.func = callbackFunc;
_goalScoreCallback.userInfo = userInfo;
}
Expand Down Expand Up @@ -101,8 +104,10 @@ void Arena::ResetToRandomKickoff(int seed) {

ball->SetState(BallState());

for (BoostPad* boostPad : _boostPads)
boostPad->SetState(BoostPadState());
if (gameMode == GameMode::SOCCAR) {
for (BoostPad* boostPad : _boostPads)
boostPad->SetState(BoostPadState());
}
}

bool Arena::_BulletContactAddedCallback(
Expand Down Expand Up @@ -302,19 +307,21 @@ Arena::Arena(GameMode gameMode, float tickRate) {
solverInfo.m_erp2 = 0.8f;
}

_SetupArenaCollisionShapes();
if (gameMode == GameMode::SOCCAR) {
_SetupArenaCollisionShapes();

#ifndef RS_NO_SUSPCOLGRID
_suspColGrid = RocketSim::GetDefaultSuspColGrid();
_suspColGrid.defaultWorldCollisionRB = _worldCollisionRBs.front();
_suspColGrid = RocketSim::GetDefaultSuspColGrid();
_suspColGrid.defaultWorldCollisionRB = _worldCollisionRBs.front();
#endif

// Give arena collision shapes the proper restitution/friction values
for (auto rb : _worldCollisionRBs) {
// TODO: Move to RLConst
rb->setRestitution(0.3f);
rb->setFriction(0.6f);
rb->setRollingFriction(0.f);
// Give arena collision shapes the proper restitution/friction values
for (auto rb : _worldCollisionRBs) {
// TODO: Move to RLConst
rb->setRestitution(0.3f);
rb->setFriction(0.6f);
rb->setRollingFriction(0.f);
}
}

{ // Initialize ball
Expand All @@ -332,7 +339,7 @@ Arena::Arena(GameMode gameMode, float tickRate) {
ball->SetState(BallState());
}

{ // Initialize boost pads
if (gameMode == GameMode::SOCCAR) { // Initialize boost pads
using namespace RLConst::BoostPads;

_boostPads.reserve(LOCS_AMOUNT_BIG + LOCS_AMOUNT_SMALL);
Expand Down Expand Up @@ -374,7 +381,7 @@ void Arena::WriteToFile(std::filesystem::path path) {
}
}

{ // Serialize boost pads
if (gameMode == GameMode::SOCCAR) { // Serialize boost pads
out.Write<uint32_t>(_boostPads.size());
for (auto pad : _boostPads)
pad->GetState().Serialize(out);
Expand Down Expand Up @@ -417,12 +424,13 @@ Arena* Arena::LoadFromFile(std::filesystem::path path) {
}
}

{ // Deserialize boost pads
// Deserialize boost pads
if (gameMode == GameMode::SOCCAR) {
uint32_t boostPadAmount = in.Read<uint32_t>();

#ifndef RS_MAX_SPEED
if (boostPadAmount != newArena->_boostPads.size())
RS_ERR_CLOSE(ERROR_PREFIX << "Failed to load from " << path <<
RS_ERR_CLOSE(ERROR_PREFIX << "Failed to load from " << path <<
", different boost pad amount written in file (" << boostPadAmount << "/" << newArena->_boostPads.size() << ")");
#endif

Expand All @@ -433,7 +441,7 @@ Arena* Arena::LoadFromFile(std::filesystem::path path) {
}
}

{ // Serialize ball
{ // Deserialize ball
BallState ballState = BallState();
ballState.Deserialize(in);
newArena->ball->SetState(ballState);
Expand Down Expand Up @@ -461,6 +469,7 @@ Arena* Arena::Clone(bool copyCallbacks) {
newCar->_velocityImpulseCache = car->_velocityImpulseCache;
}


assert(this->_boostPads.size() == newArena->_boostPads.size());
for (int i = 0; i < this->_boostPads.size(); i++)
newArena->_boostPads[i]->SetState(this->_boostPads[i]->GetState());
Expand Down Expand Up @@ -502,46 +511,62 @@ void Arena::Step(int ticksToSimulate) {

{ // Ball zero-vel sleeping
if (ball->_rigidBody->m_linearVelocity.length2() == 0 && ball->_rigidBody->m_angularVelocity.length2() == 0) {
// hooooooonk mimimimimimimi hooooooonk mimimimimimimi
ball->_rigidBody->setActivationState(ISLAND_SLEEPING);
} else {
ball->_rigidBody->setActivationState(ACTIVE_TAG);
}
}

if (gameMode == GameMode::SOCCAR) {
#ifndef RS_NO_SUSPCOLGRID
{ // Add dynamic bodies to suspension grid
for (Car* car : _cars) {
{ // Add dynamic bodies to suspension grid
for (Car* car : _cars) {
btVector3 min, max;
car->_rigidBody->getAabb(min, max);
_suspColGrid.UpdateDynamicCollisions(min, max, false);
}

btVector3 min, max;
car->_rigidBody->getAabb(min, max);
ball->_rigidBody->getAabb(min, max);
_suspColGrid.UpdateDynamicCollisions(min, max, false);
}

btVector3 min, max;
ball->_rigidBody->getAabb(min, max);
_suspColGrid.UpdateDynamicCollisions(min, max, false);
}
#endif
}

for (Car* car : _cars)
car->_PreTickUpdate(tickTime, &_suspColGrid);
for (Car* car : _cars) {
SuspensionCollisionGrid* suspColGridPtr;
#ifdef NO_SUSPCOLGRID
suspColGridPtr = NULL;
#else
if (gameMode == GameMode::SOCCAR) {
suspColGridPtr = &_suspColGrid;
} else {
suspColGridPtr = NULL;
}
#endif
car->_PreTickUpdate(tickTime, suspColGridPtr);
}

if (gameMode == GameMode::SOCCAR) {
#ifndef RS_NO_SUSPCOLGRID
{ // Remove dynamic bodies from suspension grid
for (Car* car : _cars) {
{ // Remove dynamic bodies from suspension grid
for (Car* car : _cars) {
btVector3 min, max;
car->_rigidBody->getAabb(min, max);
_suspColGrid.UpdateDynamicCollisions(min, max, true);
}

btVector3 min, max;
car->_rigidBody->getAabb(min, max);
ball->_rigidBody->getAabb(min, max);
_suspColGrid.UpdateDynamicCollisions(min, max, true);
}

btVector3 min, max;
ball->_rigidBody->getAabb(min, max);
_suspColGrid.UpdateDynamicCollisions(min, max, true);
}
}
#endif
}

for (BoostPad* pad : _boostPads)
pad->_PreTickUpdate(tickTime);
if (gameMode == GameMode::SOCCAR) {
for (BoostPad* pad : _boostPads)
pad->_PreTickUpdate(tickTime);
}

// Update world
_bulletWorld->stepSimulation(tickTime, 0, tickTime);
Expand All @@ -553,8 +578,10 @@ void Arena::Step(int ticksToSimulate) {
_boostPadGrid.CheckCollision(car);
}

for (BoostPad* pad : _boostPads)
pad->_PostTickUpdate(tickTime);
if (gameMode == GameMode::SOCCAR) {
for (BoostPad* pad : _boostPads)
pad->_PostTickUpdate(tickTime);
}

ball->_FinishPhysicsTick();

Expand Down Expand Up @@ -586,16 +613,18 @@ Arena::~Arena() {
for (Car* car : _cars)
delete car;

// Remove all boost pads
for (BoostPad* boostPad : _boostPads)
delete boostPad;
if (gameMode == GameMode::SOCCAR) {
// Remove all boost pads
for (BoostPad* boostPad : _boostPads)
delete boostPad;

{ // Delete collision RBs and shapes
for (btRigidBody* colRB : _worldCollisionRBs)
delete colRB;
{ // Delete collision RBs and shapes
for (btRigidBody* colRB : _worldCollisionRBs)
delete colRB;

for (btCollisionShape* colObject : _worldCollisionShapes)
delete colObject;
for (btCollisionShape* colObject : _worldCollisionShapes)
delete colObject;
}
}

// Remove ball
Expand Down
3 changes: 3 additions & 0 deletions src/Sim/Arena/Arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct btSequentialImpulseConstraintSolver;

enum class GameMode : byte {
SOCCAR,

// No goals, boosts, or arena - cars/ball will fall infinitely, ball is frozen until touched
THE_VOID,
// More coming soon!
};

Expand Down
11 changes: 6 additions & 5 deletions src/Sim/btVehicleRL/btVehicleRL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ float btVehicleRL::rayCast(btWheelInfoRL& wheel, SuspensionCollisionGrid* grid)

btAssert(m_vehicleRaycaster);

#ifndef RS_NO_SUSPCOLGRID
btCollisionObject* object = grid->CastSuspensionRay(m_vehicleRaycaster, source, target, rayResults);
#else
btCollisionObject* object = (btCollisionObject*)m_vehicleRaycaster->castRay(source, target, rayResults);
#endif
btCollisionObject* object;
if (grid) {
object = grid->CastSuspensionRay(m_vehicleRaycaster, source, target, rayResults);
} else {
object = (btCollisionObject*)m_vehicleRaycaster->castRay(source, target, rayResults);
}

if (object) {
wheel.m_raycastInfo.m_contactPointWS = rayResults.m_hitPointInWorld;
Expand Down

0 comments on commit 12cbccf

Please sign in to comment.