UPDATE: Sorry mods, I wanted to post this in the Gameplay section, not sure how to move or delete this post. Sorry
I'm currently working on creating spaceship controls inspired by Elite Dangerous, although I'm aiming for something a bit less complex. The main idea is to allow players to control their ship from a first-person perspective, just like they're sitting in the cockpit. The ultimate goal is to enable movement and navigation in a zero-gravity 3D space.
At the moment, I'm using Euler angles for rotations. However, considering that spaceships can rotate in various ways, I'm contemplating whether it would be a good idea to implement Quaternions instead. They might offer a better solution for handling the ship's rotations, given their flexibility.
So far, I've managed to get the ship moving forward and backward. However, I'm facing some challenges in devising effective methods for steering the ship around in 3D space. Any advice or tips you can share would be greatly appreciated. Here are the control functions I've developed thus far:
This is my control code:
void updateShipControls(Game::Entity::Ship &ship, Time &time, GLFWwindow *window) {
float dt = (float) time.getDeltaTime();
int upKeyState = glfwGetKey(window, GLFW_KEY_UP);
int downKeyState = glfwGetKey(window, GLFW_KEY_DOWN);
int leftKeyState = glfwGetKey(window, GLFW_KEY_LEFT);
int rightKeyState = glfwGetKey(window, GLFW_KEY_RIGHT);
if (upKeyState == GLFW_PRESS) {
ship.thrust -= 0.1f;
}
if (downKeyState == GLFW_PRESS) {
ship.thrust += 0.1f;
}
ship.acceleration = ship.transform.forward * ship.thrust;
ship.velocity += ship.acceleration * dt;
float speed = glm::length(ship.velocity);
if (speed > ship.maxSpeed) {
ship.velocity = glm::normalize(ship.velocity) * ship.maxSpeed;
speed = ship.maxSpeed;
}
}
This is the struct I'm using for the ship:
struct Ship {
float thrust;
vec3 velocity;
vec3 acceleration;
quat orientation;
float maxSpeed;
float mass;
float health;
float fuel;
Transform transform;
};
And this is the Transform struct, just in case somebody wants to see it:
struct Transform {
vec3 position = vec3(0.0f);
vec3 scale = vec3(1.0f);
vec3 rotation = vec3(0.0f);
vec3 forward = vec3(0.0f, 0.0f, 1.0f);
vec3 up = vec3(0.0f, 1.0f, 0.0f);
vec3 right = vec3(1.0f, 0.0f, 0.0f);
vec3 worldUp = vec3(0.0f, 1.0f, 0.0f);
mat4 model = mat4(1.0f);
};