Skip to content

Commit

Permalink
feat(engine): Fix formatting and add new features
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed Jan 23, 2024
1 parent bcb69f3 commit 366c3b8
Show file tree
Hide file tree
Showing 17 changed files with 1,756 additions and 147 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ SRC = $(SRC_DIR)init.c \
$(SRC_DIR)init_textures.c \
$(SRC_DIR)preset_textures.c \
$(SRC_DIR)clean.c \
$(SRC_DIR)parser_cfg.c \
$(SRC_DIR)parser_obj.c \
$(SRC_DIR)parser_mtl.c \
$(SRC_DIR)help.c \
Expand All @@ -29,6 +30,7 @@ SRC = $(SRC_DIR)init.c \
$(SRC_DIR)draw.c \
$(SRC_DIR)wave.c \
$(SRC_DIR)bvh.c \
$(SRC_DIR)WorldPartition.c \

TEST = $(TEST_DIR)test.c

Expand Down Expand Up @@ -56,6 +58,7 @@ CSFML = -l csfml-graphics \
LDFLAGS = $(INCLUDES) $(LIB_NAME) $(CSFML) -lm

CFLAGS = $(FLAGS) $(LDFLAGS) $(OPTI) $(IGNORE)
CFLAGS = $(FLAGS) $(LDFLAGS)

ifeq ($(OS), linux)
CC := gcc
Expand Down Expand Up @@ -111,7 +114,7 @@ re: fclean all
## DEBUG MODE

debug: OPTI = -Og -pipe
debug: CFLAGS = $(FLAGS) $(LDFLAGS) $(OPTI) $(IGNORE) -g3 -ggdb -DDEBUG
debug: CFLAGS = $(FLAGS) $(LDFLAGS) $(OPTI) $(IGNORE) -g3 -ggdb -DDEBUG -DDEBUG_MODE
debug: fclean lib_debug $(NAME)
@-$(ECHO) $(BOLD) $(GREEN)"\n► DEBUG MODE 🔧 !"$(DEFAULT)

Expand Down
3 changes: 3 additions & 0 deletions assets/config.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# load:yes?no file:name.obj collide:yes?no
yes Artisans_Hub.obj 1
no ganons_tower.obj 1
Binary file added docs/02_Collision/Fiche Hub 2.pdf
Binary file not shown.
28 changes: 28 additions & 0 deletions docs/02_Collision/formula_collision.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
prouver que la moyenne de tout les points d'un triangle est le centre de gravité du triangle et surtout que le maximum et le minimum d'une liste de triangles contient n'importe qu'elle centre de gravité du triangle donné.

$$
\mathbf{\vec{T}}_{\min} \leq \overline{\vec{T}_n} \leq \mathbf{\vec{T}}_{\max}
$$

is sphere-triangle collision

$$
\vec{TN} = \frac{(\vec{T}_1 - \vec{T}_0) \times (\vec{T}_2 - \vec{T}_0)}{\sqrt{(\vec{T}_1 - \vec{T}_0) \times (\vec{T}_2 - \vec{T}_0) \cdot (\vec{T}_1 - \vec{T}_0) \times (\vec{T}_2 - \vec{T}_0)}} \\
SD = (\vec{C} - \vec{T}_0) \cdot \vec{TN} \\
\left| SD \right| < R
$$

resolve sphere-triangle collidion

$$
\vec{V} = (\vec{T}_1 - \vec{T}_0) \times (\vec{T}_2 - \vec{T}_0) \\
\vec{TN} = \frac{\vec{V}}{\sqrt{\vec{V} \cdot \vec{V}}} \\
SD = (\vec{C} - \vec{T}_0) \cdot \vec{TN} \\
if \left| SD \right| < R \\
PD = R - \left| SD \right| \\
if SD < 0 \\
\vec{C} = \vec{C} - \vec{TN} \times PD \\
else \\
\vec{C} = \vec{C} + \vec{TN} \times PD \\
$$
133 changes: 133 additions & 0 deletions includes/WorldPartition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
** EPITECH PROJECT, 2024
** Engine-3D
** File description:
** WorldPartition
*/

#ifndef WORLDPARTITION_H_
#define WORLDPARTITION_H_

#include "engine.h"

// bool is_sphere_triangle_collision(sfVector4f sphere_center, float sphere_radius, triangle_t triangle);

// sfVector4f resolve_sphere_triangle_collision(sfVector4f sphere_center, float sphere_radius, triangle_t triangle);

typedef struct rectangle_s {
sfVector3f pos;
sfVector3f size;
} rectangle_t; // AABB

typedef struct octree_s {
rectangle_t box;
rectangle_t children_box[8];
struct octree_s *children[8];
triangle_t *data; // vector of triangles
unsigned data_size;
unsigned depth;
} octree_t;

#define MAX_DEPTH 8

rectangle_t get_size_of_mesh(link_t *mesh);

octree_t *octree_create(rectangle_t box, unsigned depth);

void octree_resize(octree_t *o, rectangle_t box);

void octree_clear(octree_t *o);

unsigned octree_size(octree_t *o);

// Function to check if a point is inside a rectangle (AABB)
bool point_inside_rectangle(sfVector4f point, rectangle_t rect);

// Function to check if a triangle is inside a rectangle (AABB)
bool rectangle_contains_triangle(rectangle_t box, triangle_t tri);

// Function to check if a line segment intersects with an AABB
bool line_segment_intersects_aabb(sfVector4f p1, sfVector4f p2, rectangle_t rect);

// Function to check if a triangle overlaps with an AABB
bool rectangle_overlaps_triangle(rectangle_t box, triangle_t tri);

void octree_insert(octree_t *o, triangle_t tri);

bool rectangle_contains_point(rectangle_t box, sfVector4f point);

void octree_fill(octree_t *tree, link_t *mesh);

void octree_check_collision(octree_t *tree, sfVector4f *sphere_center, float sphere_radius);

void octree_destroy(octree_t *octree);

typedef struct r_tree_s {
sfVector2f pos;
sfVector2f size;
struct r_tree_s *children[4];
link_t *data; // list of octree_t
} r_tree_t;

r_tree_t *r_tree_create(sfVector2f pos, sfVector2f size);

void r_tree_fill(r_tree_t *r_tree, link_t *octrees);

void r_tree_check_collision(r_tree_t *r_tree, sfVector4f *sphere_center, float sphere_radius);

void r_tree_destroy(r_tree_t *r_tree);

#define CHUNK_SIZE 100

typedef struct {
sfVector2f pos;
sfVector2f size;
bool loaded;
r_tree_t *data;
} chunk_t;

typedef struct world_partition_s {
sfVector2f world_size;
sfVector2f world_position;
sfVector2f chunk_size;
chunk_t *chunks;
} world_partition_t;

world_partition_t *init_world(sfVector2f chunk_size);

world_partition_t *world_partition_create(sfVector2f world_size, sfVector2f chunk_size, sfVector2f start_position);

void world_partition_destroy(world_partition_t *partition);

void world_partition_update(world_partition_t *partition, sfVector2f position, float radius);

void world_partition_load_chunk(world_partition_t *partition, sfVector2f position);

void world_partition_unload_chunk(world_partition_t *partition, sfVector2f position);

bool world_partition_is_chunk_loaded(world_partition_t *partition, sfVector2f position);

void world_partition_set_chunk_data(world_partition_t *partition, sfVector2f position, r_tree_t *data);

r_tree_t *world_partition_get_chunk_data(world_partition_t *partition, sfVector2f position);

/** Collision Pipeline - All steps:
* 1. init_world // validé
* 2. octree_create
* 3. octree_fill
* 4. world_partition_create // validé
* 5. world_partition_update // validé
* 6. world_partition_load_chunk
* 7. r_tree_create
* 8. r_tree_fill
* 9. world_partition_get_chunk_data // validé
* 10. r_tree_check_collision
* 11. octree_check_collision
* 12. is_sphere_triangle_collision // validé
* 13. resolve_sphere_triangle_collision // validé
* 14. world_partition_unload_chunk
* 15. world_partition_destroy
* 16. free_world
*/

#endif /* !WORLDPARTITION_H_ */
40 changes: 30 additions & 10 deletions includes/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "constants.h"
#include "lib.h"
#include "link_list.h"
Expand All @@ -31,6 +33,7 @@
#endif

typedef struct engine_s engine_t;
typedef struct world_partition_s world_partition_t;

typedef enum {
IDLE,
Expand All @@ -42,7 +45,7 @@ typedef enum {
} texture;

typedef enum {
MAP, WAVE, MESH, PLAYER
MAP, WAVE, MESH, PLAYER, CUBE
} mesh_type;


Expand Down Expand Up @@ -98,16 +101,21 @@ typedef struct mesh_s {
} mesh_t;

typedef struct Tree_s {
struct Tree_s *node[4]; // node
struct Tree_s *node[8]; // node
sfVector3f s_g[2]; // smaller/greater vectors
triangle_t *triangle; // triangle at front node
triangle_t **triangle; // triangles at front node
} Tree_t;

union vector
{
sfVector3f vec3;
sfVector4f vec4;
};
typedef struct config_s {
char name[64];
struct config_s *next;
uint8_t collision; // 0 = no collision, 1 = map collision, 2 = sphere box collision, 3 rect box collision
bool load;
// char *texture;
// sfVector3f pos;
// sfVector3f rot;
// sfVector3f scale;
} config_t;

struct engine_s {
/*graphic*/
Expand All @@ -116,6 +124,8 @@ struct engine_s {
sfRenderStates **textures;
sfImage **images;
/*camera*/
float velocity_y;
float radius;
sfVector4f Pos;
sfVector4f Dir;
float fawZ;
Expand All @@ -131,22 +141,29 @@ struct engine_s {
float ViewtoProjection[4][4];
/*link*/
link_t *list_objs;
link_t *list_octree;
link_t *FinalMesh;
/*collide*/
world_partition_t *world;
Tree_t *root;
sfVector3f s_g[2];
/*water*/
link_t *wave_list;
float t; // Temps
/*config*/
config_t *config;
};

extern engine_t engine;

/* INIT */
void parse_configs(void);
bool open_folder(char *path);
bool init_engine();
sfRenderStates **init_textures();
sfImage **init_images();
void add_cube_box_as_mesh_in_list(sfVector3f s_g[2]);
void clean_cube_box_from_list(void);

/* CLEAN */
void clean_triangles(link_t *mesh);
Expand Down Expand Up @@ -180,6 +197,7 @@ bool cmp_av_two_triangles(void *triangle_1, void *triangle_2);

/* CLIPPING */
void clipping(triangle_t triangle);
int Triangle_ClipAgainstPlane(sfVector4f front, sfVector4f back, triangle_t *intri, triangle_t (*outtri)[2]);

/* UTILS */
sfVector4f Vector_Add(sfVector4f v, sfVector4f w);
Expand All @@ -196,16 +214,18 @@ float calcul_dist(sfVector4f p, sfVector4f pp, sfVector4f n);
void Matrix_Multiply(float (*m)[4], float (*m1)[4], float (*m2)[4]);
matrix Matrix_QuickInverse(float (*m)[4]);
sfVector3f average_triangle(triangle_t *t);
void print_matrix(float (*m)[4]);
void print_vector(sfVector4f v);
void print_triangle(triangle_t *t);
void print_cube(sfVector3f s_g[2]);

/* DRAW */
void display_triangles(link_t *mesh);
void draw_triangle(triangle_t *node);

/* BVH */
void set_bvh(Tree_t *tree, link_t *mesh);
sfVector3f get_bvh(Tree_t *tree, sfVector4f point);
void set_bvh(Tree_t *tree, link_t *mesh, unsigned len);
void get_bvh(Tree_t *tree, sfVector4f *point, float radius);
void print_bvh(Tree_t *tree);

/* WATER */
Expand Down
Loading

0 comments on commit 366c3b8

Please sign in to comment.