2023-10-15 00:47:23 +00:00
|
|
|
// =========================================
|
|
|
|
// | Spacewar-Physics.c |
|
|
|
|
// | Copyright (C) 2023, Barra Ó Catháin |
|
|
|
|
// | See end of file for copyright notice. |
|
|
|
|
// =========================================
|
2023-10-15 22:54:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
2023-10-15 00:47:23 +00:00
|
|
|
#include "Spacewar-Physics.h"
|
|
|
|
|
|
|
|
void doPhysicsTick(SpacewarState * state)
|
|
|
|
{
|
2023-10-16 22:43:21 +00:00
|
|
|
double gravityMagnitude, gravityAcceleration, velocityMagnitude;
|
2023-10-15 00:47:23 +00:00
|
|
|
for (int shipIndex = 0; shipIndex < 32; shipIndex++)
|
|
|
|
{
|
2023-10-15 22:54:02 +00:00
|
|
|
SpacewarShipState * currentShip = &state->playerStates[shipIndex];
|
2023-10-15 00:47:23 +00:00
|
|
|
if (currentShip->inPlay)
|
|
|
|
{
|
|
|
|
// Calculate Gravity:
|
|
|
|
xyVectorBetweenPoints(currentShip->position.xComponent, currentShip->position.yComponent,
|
2023-10-15 22:54:02 +00:00
|
|
|
0.0, 0.0, ¤tShip->gravity);
|
|
|
|
gravityMagnitude = normalizeXYVector(¤tShip->gravity);
|
2023-10-15 00:47:23 +00:00
|
|
|
gravityAcceleration = 0;
|
2023-10-16 22:43:21 +00:00
|
|
|
|
2023-10-15 00:47:23 +00:00
|
|
|
// Some maths that felt okay:
|
2023-10-15 22:54:02 +00:00
|
|
|
if (gravityMagnitude >= 116)
|
2023-10-15 00:47:23 +00:00
|
|
|
{
|
2023-10-16 22:43:21 +00:00
|
|
|
gravityAcceleration = (45000 / pow(gravityMagnitude, 2)) * 6.67;
|
2023-10-15 00:47:23 +00:00
|
|
|
}
|
2023-10-16 22:43:21 +00:00
|
|
|
// We're pactually in the black hole; teleport:
|
2023-10-15 00:47:23 +00:00
|
|
|
else
|
|
|
|
{
|
2023-10-15 22:54:02 +00:00
|
|
|
currentShip->position.xComponent = (double)(random() % 7500);
|
|
|
|
currentShip->position.yComponent = (double)(random() % 7500);
|
2023-10-16 22:43:21 +00:00
|
|
|
currentShip->velocity.xComponent = 0;
|
|
|
|
currentShip->velocity.yComponent = 0;
|
2023-10-15 22:54:02 +00:00
|
|
|
}
|
2023-10-16 22:43:21 +00:00
|
|
|
|
2023-10-15 22:54:02 +00:00
|
|
|
multiplyXYVector(¤tShip->gravity, gravityAcceleration);
|
2023-10-15 00:47:23 +00:00
|
|
|
|
|
|
|
// Apply Inputs:
|
2023-10-16 22:43:21 +00:00
|
|
|
|
|
|
|
// Rotate the engine vector if needed:
|
|
|
|
if (state->playerInputs[shipIndex].turningClockwise == 1)
|
|
|
|
{
|
|
|
|
rotateXYVector(¤tShip->engine, 2.5);
|
|
|
|
}
|
|
|
|
if (state->playerInputs[shipIndex].turningAnticlockwise == 1)
|
|
|
|
{
|
|
|
|
rotateXYVector(¤tShip->engine, -2.5);
|
|
|
|
}
|
2023-10-15 00:47:23 +00:00
|
|
|
|
|
|
|
// Apply Gravity and Velocity to Position:
|
2023-10-16 22:43:21 +00:00
|
|
|
if (state->playerInputs[shipIndex].accelerating == 1)
|
|
|
|
{
|
|
|
|
addXYVector(¤tShip->velocity, ¤tShip->engine);
|
|
|
|
}
|
2023-10-15 22:54:02 +00:00
|
|
|
addXYVector(¤tShip->velocity, ¤tShip->gravity);
|
|
|
|
addXYVector(¤tShip->position, ¤tShip->velocity);
|
2023-10-15 00:47:23 +00:00
|
|
|
|
|
|
|
// Wrap position to game field:
|
2023-10-15 22:54:02 +00:00
|
|
|
if (currentShip->position.xComponent > 8000.0)
|
2023-10-15 00:47:23 +00:00
|
|
|
{
|
2023-10-15 22:54:02 +00:00
|
|
|
state->playerStates[shipIndex].position.xComponent = -7999.0;
|
2023-10-15 00:47:23 +00:00
|
|
|
state->playerStates[shipIndex].velocity.xComponent *= 0.9;
|
|
|
|
}
|
2023-10-15 22:54:02 +00:00
|
|
|
if (currentShip->position.xComponent < -8000.0)
|
2023-10-15 00:47:23 +00:00
|
|
|
{
|
2023-10-15 22:54:02 +00:00
|
|
|
state->playerStates[shipIndex].position.xComponent = 7999.0;
|
2023-10-15 00:47:23 +00:00
|
|
|
state->playerStates[shipIndex].velocity.xComponent *= 0.9;
|
|
|
|
}
|
2023-10-15 22:54:02 +00:00
|
|
|
if (currentShip->position.yComponent > 8000.0)
|
2023-10-15 00:47:23 +00:00
|
|
|
{
|
2023-10-15 22:54:02 +00:00
|
|
|
state->playerStates[shipIndex].position.yComponent = -7999.0;
|
2023-10-15 00:47:23 +00:00
|
|
|
state->playerStates[shipIndex].velocity.yComponent *= 0.9;
|
|
|
|
}
|
2023-10-15 22:54:02 +00:00
|
|
|
if (currentShip->position.yComponent < -8000.0)
|
2023-10-15 00:47:23 +00:00
|
|
|
{
|
2023-10-15 22:54:02 +00:00
|
|
|
state->playerStates[shipIndex].position.yComponent = 7999.0;
|
2023-10-15 00:47:23 +00:00
|
|
|
state->playerStates[shipIndex].velocity.yComponent *= 0.9;
|
2023-10-15 22:54:02 +00:00
|
|
|
}
|
2023-10-15 00:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ========================================================
|
|
|
|
// | End of Spacewar-Physics.c, copyright notice follows. |
|
|
|
|
// ========================================================
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|