2026-03-21 13:05:28 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
|
|
|
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "planet.h"
|
2026-07-09 11:50:45 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
#include <cassert>
|
2026-03-21 13:05:28 +01:00
|
|
|
#include <allegro5/allegro_primitives.h>
|
|
|
|
|
|
2026-07-09 11:50:45 +02:00
|
|
|
#define GRAVITY_CONSTANT 1.0f // Gravitational constant scaled for simulation
|
2026-03-21 13:05:28 +01:00
|
|
|
#define SHIP_MASS 1.0f // Assumed constant ship mass
|
|
|
|
|
#define MASS_TO_RADIUS 1.5f // Radius scale factor: radius = MASS_TO_RADIUS * mass^(1/3)
|
|
|
|
|
|
2026-07-09 11:50:45 +02:00
|
|
|
Planet::Planet(const float x, const float y, const float mass) :
|
|
|
|
|
pos(x, y), mass(mass), radius(MASS_TO_RADIUS * cbrtf(mass))
|
|
|
|
|
{}
|
2026-03-21 13:05:28 +01:00
|
|
|
|
2026-07-09 11:50:45 +02:00
|
|
|
Vec<float> Planet::getGravity(const Ship &ship) const {
|
|
|
|
|
const Vec<float> distance = this->pos - ship.getPos();
|
2026-03-21 13:05:28 +01:00
|
|
|
|
2026-07-09 11:50:45 +02:00
|
|
|
if(distance.length() < this->radius)
|
|
|
|
|
return Vec<float>(0, 0);
|
2026-03-21 13:05:28 +01:00
|
|
|
|
|
|
|
|
// F = G * m1 * m2 / r^2
|
2026-07-09 11:50:45 +02:00
|
|
|
const float force = GRAVITY_CONSTANT * SHIP_MASS * this->mass / (distance.length() * distance.length());
|
2026-03-21 13:05:28 +01:00
|
|
|
|
2026-07-09 11:50:45 +02:00
|
|
|
// a = F / m_ship = G * m_this / r^2
|
|
|
|
|
const float acceleration = force / SHIP_MASS;
|
2026-03-21 13:05:28 +01:00
|
|
|
|
|
|
|
|
// Normalize direction and apply acceleration
|
2026-07-09 11:50:45 +02:00
|
|
|
return distance.normalized() * acceleration;
|
2026-03-21 13:05:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-07-09 11:50:45 +02:00
|
|
|
void Planet::draw() {
|
2026-03-21 13:05:28 +01:00
|
|
|
// Color intensity based on mass (brighter = more massive)
|
2026-07-09 11:50:45 +02:00
|
|
|
float color_scale = fminf(1.0f, this->mass / 1000.0f);
|
2026-03-21 13:05:28 +01:00
|
|
|
int r = (int)(100 + 155 * color_scale);
|
|
|
|
|
int g = (int)(100 + 50 * color_scale);
|
|
|
|
|
int b = (int)(150 - 100 * color_scale);
|
|
|
|
|
|
2026-07-09 11:50:45 +02:00
|
|
|
al_draw_filled_circle(this->pos.x, this->pos.y, this->radius,
|
|
|
|
|
al_map_rgb(r, g, b));
|
2026-03-21 13:05:28 +01:00
|
|
|
}
|