vktutorial/hk_game_object.hpp

89 lines
2.3 KiB
C++
Raw Permalink Normal View History

2024-04-03 21:28:08 +08:00
#pragma once
#include "hk_model.hpp"
// libs
#include <glm/gtc/matrix_transform.hpp>
2024-04-03 21:28:08 +08:00
// std
#include <memory>
namespace hk
{
struct TransformComponent
2024-04-03 21:28:08 +08:00
{
glm::vec3 translation{};
glm::vec3 scale{1.0f, 1.0f, 1.0f};
glm::vec3 rotation{};
2024-04-03 21:28:08 +08:00
// Matrix corrsponds to Translate * Ry * Rx * Rz * Scale
// Rotations correspond to Tait-bryan angles of Y(1), X(2), Z(3)
// https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
glm::mat4 mat4()
{
const float c3 = glm::cos(rotation.z);
const float s3 = glm::sin(rotation.z);
const float c2 = glm::cos(rotation.x);
const float s2 = glm::sin(rotation.x);
const float c1 = glm::cos(rotation.y);
const float s1 = glm::sin(rotation.y);
return glm::mat4{
{
scale.x * (c1 * c3 + s1 * s2 * s3),
scale.x * (c2 * s3),
scale.x * (c1 * s2 * s3 - c3 * s1),
0.0f,
},
{
scale.y * (c3 * s1 * s2 - c1 * s3),
scale.y * (c2 * c3),
scale.y * (c1 * c3 * s2 + s1 * s3),
0.0f,
},
{
scale.z * (c2 * s1),
scale.z * (-s2),
scale.z * (c1 * c2),
0.0f,
},
{translation.x, translation.y, translation.z, 1.0f}};
2024-04-03 21:28:08 +08:00
}
};
2024-04-12 23:24:53 +08:00
struct RigidBody2dComponent
{
glm::vec2 velocity;
float mass{1.0f};
};
2024-04-03 21:28:08 +08:00
class GameObject
{
public:
using id_t = unsigned int;
static GameObject createGameObject()
{
static id_t currentId = 0;
return GameObject{currentId++};
}
GameObject(const GameObject &) = delete;
GameObject &operator=(const GameObject &) = delete;
GameObject(GameObject &&) = default;
GameObject &operator=(GameObject &&) = default;
id_t getId() const { return m_id; }
std::shared_ptr<Model> m_model{};
glm::vec3 m_color{};
TransformComponent m_transform{};
2024-04-03 21:28:08 +08:00
2024-04-12 23:24:53 +08:00
RigidBody2dComponent m_rigidBody2d{};
2024-04-03 21:28:08 +08:00
private:
GameObject(id_t objId) : m_id(objId) {}
id_t m_id;
};
} // namespace hk