Skip to content

Commit

Permalink
Merge pull request #10 from gue-ni/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gue-ni authored Apr 26, 2023
2 parents e0019d5 + a887122 commit 02a565a
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 275 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveMacros: true
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
Expand Down
2 changes: 1 addition & 1 deletion OpenGL_Flightsim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_executable(flightsim
# Source files
src/ai.h
src/clipmap.h
src/collisions.h
src/collider.h
src/data.h
src/flightmodel.h
src/gfx.cpp
Expand Down
2 changes: 1 addition & 1 deletion OpenGL_Flightsim/OpenGL_Flightsim.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ xcopy /y /i /s $(ProjectDir)..\lib\glew-2.2.0\bin\Release\x64\*.dll $(OutDir)</C
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\ai.h" />
<ClInclude Include="src\collisions.h" />
<ClInclude Include="src\collider.h" />
<ClInclude Include="src\data.h" />
<ClInclude Include="src\clipmap.h" />
<ClInclude Include="src\flightmodel.h" />
Expand Down
2 changes: 1 addition & 1 deletion OpenGL_Flightsim/OpenGL_Flightsim.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<ClInclude Include="src\data.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\collisions.h">
<ClInclude Include="src\collider.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="lib\imgui\imgui.h">
Expand Down
20 changes: 10 additions & 10 deletions OpenGL_Flightsim/src/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@
glm::vec3 get_intercept_point(const glm::vec3& position, const glm::vec3& velocity, const glm::vec3& target_position,
const glm::vec3& target_velocity)
{
auto velocity_delta = target_velocity - velocity;
auto position_delta = target_position - position;
auto velocity_delta = target_velocity - velocity;
auto position_delta = target_position - position;
auto time_to_intercept = glm::length(position_delta) / glm::length(velocity_delta);
return target_position + target_velocity * time_to_intercept;
}

void fly_towards(Airplane& airplane, const glm::vec3& target)
{
auto& rb = airplane;
auto& rb = airplane;
auto& joystick = airplane.joystick;
auto position = rb.position;
auto position = rb.position;
auto direction = glm::normalize(rb.inverse_transform_direction(target - rb.position));
auto angle = glm::angle(phi::FORWARD, direction);
auto angle = glm::angle(phi::FORWARD, direction);

float rudder = direction.z;
float rudder = direction.z;
float elevator = direction.y * 5.0f;

float m = phi::PI / 4.0f;
float agressive_roll = direction.z;
float wings_level_roll = rb.right().y;
float m = phi::PI / 4.0f;
float agressive_roll = direction.z;
float wings_level_roll = rb.right().y;
float wings_level_influence = phi::inverse_lerp(0.0f, m, glm::clamp(angle, -m, m));
float aileron = phi::lerp(wings_level_roll, agressive_roll, wings_level_influence);
float aileron = phi::lerp(wings_level_roll, agressive_roll, wings_level_influence);

joystick = glm::clamp(glm::vec4(aileron, rudder, elevator, 0.0f), glm::vec4(-1.0f), glm::vec4(1.0f));
}
Expand Down
28 changes: 14 additions & 14 deletions OpenGL_Flightsim/src/clipmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "gfx.h"

constexpr unsigned int primitive_restart = 0xFFFFU;
const std::string path = "assets/textures/terrain/1/";
const std::string path = "assets/textures/terrain/1/";

struct Seam {
gfx::gl::VertexBuffer vbo;
Expand All @@ -12,7 +12,7 @@ struct Seam {

Seam(int columns, float size)
{
int rows = 1;
int rows = 1;
index_count = columns;

std::vector<glm::vec3> vertices;
Expand Down Expand Up @@ -132,8 +132,8 @@ class Clipmap : public gfx::Object3D
{
#if 1
if (!context.is_shadow_pass) {
auto camera_pos = context.camera->get_world_position();
float height = camera_pos.y;
auto camera_pos = context.camera->get_world_position();
float height = camera_pos.y;
auto camera_pos_xy = glm::vec2(camera_pos.x, camera_pos.z);

heightmap.bind(2);
Expand All @@ -159,12 +159,12 @@ class Clipmap : public gfx::Object3D
for (int l = min_level; l <= levels; l++) {
const int rows = 5, cols = 5;
// float border = 0.0f;
float scale = std::pow(2.0f, l);
float next_scale = std::pow(2.0f, l + 2);
float scale = std::pow(2.0f, l);
float next_scale = std::pow(2.0f, l + 2);
float scaled_segment_size = segment_size * scale;
float tile_size = segments * scaled_segment_size;
glm::vec2 snapped = glm::floor(camera_pos_xy / next_scale) * next_scale;
auto base = calc_base(l, camera_pos_xy);
float tile_size = segments * scaled_segment_size;
glm::vec2 snapped = glm::floor(camera_pos_xy / next_scale) * next_scale;
auto base = calc_base(l, camera_pos_xy);

shader.uniform("u_Scale", scale);
shader.uniform("u_SegmentSize", scaled_segment_size);
Expand All @@ -183,7 +183,7 @@ class Clipmap : public gfx::Object3D
center.draw();
} else {
auto prev_base = calc_base(l - 1, camera_pos_xy);
auto diff = glm::abs(base - prev_base);
auto diff = glm::abs(base - prev_base);

auto l_offset = glm::vec2(tile_size, tile_size);
if (diff.x == tile_size) {
Expand Down Expand Up @@ -283,11 +283,11 @@ class Clipmap : public gfx::Object3D

glm::vec2 calc_base(int level, glm::vec2 camera_pos)
{
float scale = std::pow(2.0f, level);
float next_scale = std::pow(2.0f, level + 2);
float tile_size = segments * segment_size * scale;
float scale = std::pow(2.0f, level);
float next_scale = std::pow(2.0f, level + 2);
float tile_size = segments * segment_size * scale;
glm::vec2 snapped = glm::floor(camera_pos / next_scale) * next_scale;
glm::vec2 base = snapped - tile_size * 2.0f;
glm::vec2 base = snapped - tile_size * 2.0f;
return base;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,13 @@
*/
#pragma once

#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtx/vector_angle.hpp>
#include <glm/mat4x4.hpp>
#include <glm/vec3.hpp>
#include <tuple>

#include "phi.h"

namespace col
{

constexpr float EPSILON = 1e-8f;

template <typename T>
constexpr inline T sq(T x)
{
return x * x;
}

typedef void CollisionCallback(const glm::vec3& point, const glm::vec3& normal);

struct Collider {
Expand All @@ -45,13 +32,15 @@ struct AABB : public Collider {
struct Sphere : public Collider {
glm::vec3 center;
float radius;
Sphere() : Sphere(glm::vec3(0.0f), 1.0f) {}
Sphere(const glm::vec3& center, float radius) : center(center), radius(radius) {}
};

// ray = origin + direction * t
struct Ray : public Collider {
glm::vec3 origin, direction;
Ray() : Ray(glm::vec3(0.0f), glm::vec3(1.0f, 0.0f, 0.0f)) {}
Ray(const glm::vec3& origin, const glm::vec3& direction) : origin(origin), direction(direction) {}
inline glm::vec3 point_at(float t) const { return origin + direction * t; }
};

struct Heightmap {
Expand Down Expand Up @@ -85,9 +74,9 @@ struct Heightmap {
float get_height(const glm::vec2& coord) const
{
glm::vec2 tmp = glm::clamp(coord / magnification, glm::vec2(-1.0f), glm::vec2(1.0f));
auto uv = phi::scale(tmp, glm::vec2(-1.0f), glm::vec2(1.0f), glm::vec2(0.0f), glm::vec2(1.0f));
float value = sample(uv).r;
float height = scale * value + shift;
auto uv = phi::scale(tmp, glm::vec2(-1.0f), glm::vec2(1.0f), glm::vec2(0.0f), glm::vec2(1.0f));
float value = sample(uv).r;
float height = scale * value + shift;
return height;
}
};
Expand All @@ -96,15 +85,13 @@ struct Heightmap {
bool test_collision(const Ray& r, const Sphere& s, float* t)
{
// Christer_Ericson-Real-Time_Collision_Detection.pdf#page=178
assert(std::abs(glm::length(r.direction) - 1.0f) < EPSILON);

auto m = r.origin - s.center;
auto b = glm::dot(m, r.direction);
auto c = glm::dot(m, m) - sq(s.radius);
auto c = glm::dot(m, m) - phi::sq(s.radius);

if (c > 0.0f && b > 0.0f) return false;

auto discr = sq(b) - c;
auto discr = phi::sq(b) - c;

if (discr < 0.0f) return false;

Expand All @@ -113,11 +100,19 @@ bool test_collision(const Ray& r, const Sphere& s, float* t)
}

// test collision between two spheres
bool test_collision(const Sphere& s0, const Sphere& s1)
bool test_collision(const Sphere& a, const Sphere& b, phi::CollisionInfo* info)
{
float distance = glm::length(s0.center - s1.center);
float radius_sum = s0.radius + s1.radius;
return distance < radius_sum;
float distance = glm::length(a.center - b.center);
float radius_sum = a.radius + b.radius;

if (distance < radius_sum) {
info->normal = glm::normalize(b.center - a.center);
info->penetration = radius_sum - distance;
info->point = a.center + a.radius * info->normal;
return true;
} else {
return false;
}
}

// test collision between two axis aligned bounding boxes
Expand All @@ -129,9 +124,10 @@ bool test_collision(const AABB& a, const AABB& b)
(a_max.z < b_min.z || a_min.z > b_max.z));
}

bool test_collision(const Heightmap& heightmap, const glm::vec3& point)
bool test_collision(const Heightmap& heightmap, const glm::vec3& point, float* height)
{
return point.y <= heightmap.get_height({point.x, point.z});
*height = heightmap.get_height({point.x, point.z});
return point.y <= *height;
}

// test collision of two moving spheres
Expand Down Expand Up @@ -171,8 +167,8 @@ bool test_moving_collision(const Sphere& s0, const glm::vec3& velocity0, const S
#else
// Christer_Ericson-Real-Time_Collision_Detection.pdf#page=226
float tmp_t = 0.0f;
auto v = velocity0 - velocity1;
auto vlen = glm::length(v);
auto v = velocity0 - velocity1;
auto vlen = glm::length(v);

Ray ray(s0.center, v / vlen);
Sphere sphere(s1.center, s0.radius + s1.radius);
Expand Down
Loading

0 comments on commit 02a565a

Please sign in to comment.