From 8928d83d80f45a0e5d5726899459514861087f19 Mon Sep 17 00:00:00 2001
From: Marie <marie.audouard@polytechnique.edu>
Date: Tue, 16 May 2023 12:26:57 +0200
Subject: [PATCH] Changement de la structure de bat

---
 Idea.txt                                     |  7 --
 projet-code/scenes_inf443/base/src/bat.cpp   | 99 +++++++++++++++++++-
 projet-code/scenes_inf443/base/src/bat.hpp   | 14 +++
 projet-code/scenes_inf443/base/src/scene.cpp | 63 +++++++------
 projet-code/scenes_inf443/base/src/scene.hpp |  9 +-
 5 files changed, 146 insertions(+), 46 deletions(-)
 delete mode 100644 Idea.txt

diff --git a/Idea.txt b/Idea.txt
deleted file mode 100644
index ff0e283..0000000
--- a/Idea.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-TODO:
-Animals with animation through blender .obj
-Multiple textures to have a pretty mountain with grass stone and snow
-Skybox!
-Day and night cycle
-Balls of fire ice and thunder! -> d'abord faire des bouncing balls qu'on peut lancer
-Enemies/HP
\ No newline at end of file
diff --git a/projet-code/scenes_inf443/base/src/bat.cpp b/projet-code/scenes_inf443/base/src/bat.cpp
index 85c29d3..df8518f 100644
--- a/projet-code/scenes_inf443/base/src/bat.cpp
+++ b/projet-code/scenes_inf443/base/src/bat.cpp
@@ -1,5 +1,8 @@
 #include "bat.hpp"
 #include "environment.hpp"
+#include "key_positions_structure.hpp"
+#include "interpolation.hpp"
+#include "terrain.hpp"
 
 void bat::initialize_bat()
 {
@@ -18,9 +21,9 @@ void bat::initialize_bat()
 
 
 	// Scale the model
-	body.model.scaling = 0.2f;
-	wing1.model.scaling = 0.2f;
-	wing2.model.scaling = 0.2f;
+	//body.model.scaling = 0.2f;
+	//wing1.model.scaling = 0.2f;
+	//wing2.model.scaling = 0.2f;
 
 
 
@@ -45,4 +48,92 @@ void bat::initialize_bat()
 	bat.add(wing1, "Bat wing left1", "Bat base", { -0.05f,0,0 });
 	bat.add(wing2, "Bat wing right1", "Bat base", { 0.05f,0,0 });
 
-}
\ No newline at end of file
+
+	N = 0;
+	size.resize(N);
+	isdead.resize(N);
+	pos.resize(N);
+	pos2.resize(N);
+	keyframe.resize(N);
+}
+
+
+
+void bat::initialize_mvt(vec3 p, int m, float terrain_length)
+{
+	// Definition of the initial data
+	//--------------------------------------//
+
+	// Key 3D positions
+	numarray<vec3> key_positions =
+	{ {-2,1,0}, {0,1,0}, {2,1,0}, {1,3,0}, {2,3,0}, {2,3,1}, {2,2.5,1.5}, {1.5,0,1}, {1.5,0,-1}, {0,0,-1}, {0,-0.5,-1}, {-2,1,-1} };
+
+	float h = 20 + evaluate_terrain_height(0, 0, terrain_length);
+	for (int i = 0; i < key_positions.size(); i++) {
+		key_positions[i] *= size[m];
+		key_positions[i][2] += h;
+		key_positions[i] += p;
+	}
+
+	// Key times (time at which the position must pass in the corresponding position)
+	numarray<float> key_times =
+	{ 0.0f, 1.0f, 2.0f, 2.5f, 3.0f, 3.5f, 3.75f, 4.5f, 5.0f, 6.0f, 7.0f, 8.0f };
+
+	// Initialize the helping structure to display/interact with these positions
+	keyframe[m].initialize(key_positions, key_times);
+
+
+	// Set timer bounds
+	// The timer must span a time interval on which the interpolation can be conducted
+	// By default, set the minimal time to be key_times[1], and the maximal time to be key_time[N-2] (enables cubic interpolation)
+	int NT = key_times.size();
+	timer_mvt.t_min = key_times[0];
+	timer_mvt.t_max = key_times[NT - 1];
+	timer_mvt.t = timer_mvt.t_min;
+}
+
+void bat::display_mvt()
+{
+
+	// Update the current time
+	timer_mvt.update();
+	float t = timer_mvt.t;
+
+
+	for (int m = 0; m < N; m++) {
+		// clear trajectory when the timer restart
+		if (t < timer_mvt.t_min + 0.1f)
+			keyframe[m].trajectory.clear();
+
+		if (t + 0.1f > timer_mvt.t_max) pos2[m] = interpolation(0.01f, keyframe[m].key_positions, keyframe[m].key_times, 0.5f);
+		else pos2[m] = interpolation((t + 0.1f), keyframe[m].key_positions, keyframe[m].key_times, 0.5f);
+
+		// Display the key positions and lines b/w positions
+		//keyframe.display_key_positions(environment);
+
+		// Compute the interpolated position
+		//  This is this function that you need to complete
+		pos[m] = interpolation(t, keyframe[m].key_positions, keyframe[m].key_times, 0.5f);
+
+		// Display the interpolated position (and its trajectory)
+		//keyframe.display_current_position(p, environment);
+	}
+}
+
+
+void bat::add_bat(vec3 new_pos, float terrain_length) {
+	N++;
+	pos.resize(N);
+	pos2.resize(N);
+	isdead.resize(N);
+	size.resize(N);
+	keyframe.resize(N);
+
+
+	pos[N - 1] = new_pos;
+	isdead[N - 1] = false;
+	size[N - 1] = rand_interval(0.05f, 1.0f);;
+	initialize_mvt(new_pos, N - 1, terrain_length);
+
+}
+
diff --git a/projet-code/scenes_inf443/base/src/bat.hpp b/projet-code/scenes_inf443/base/src/bat.hpp
index 149fae0..5cb0a25 100644
--- a/projet-code/scenes_inf443/base/src/bat.hpp
+++ b/projet-code/scenes_inf443/base/src/bat.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "cgp/cgp.hpp"
+#include "key_positions_structure.hpp"
 
 using namespace cgp;
 
@@ -9,5 +10,18 @@ using namespace cgp;
 struct bat{
 	cgp::hierarchy_mesh_drawable bat;
 
+	int N;
+	std::vector<double> size;
+	std::vector<bool> isdead;
+	std::vector<vec3> pos;
+	std::vector<vec3> pos2;
+	// Timer used for the interpolation of the position
+	cgp::timer_interval timer_mvt;
+	// A helper structure used to store and display the key positions/time
+	std::vector<keyframe_structure> keyframe;
+
 	void initialize_bat();
+	void add_bat(vec3 new_pos, float terrain_length);
+	void initialize_mvt(vec3 p, int i, float terrain_length);
+	void display_mvt();
 };
\ No newline at end of file
diff --git a/projet-code/scenes_inf443/base/src/scene.cpp b/projet-code/scenes_inf443/base/src/scene.cpp
index 52392d1..8edd5c3 100644
--- a/projet-code/scenes_inf443/base/src/scene.cpp
+++ b/projet-code/scenes_inf443/base/src/scene.cpp
@@ -95,14 +95,17 @@ void scene_structure::initialize()
 		GL_CLAMP_TO_BORDER);
 
 	bird1.initialize_bird();
-	initialize_mvt();
+	//initialize_mvt();
 	chain1.initialize();
 	bouncing.initialize(10); //10 balls
 	bat1.initialize_bat();
+	bat1.add_bat({0,0,0}, terrain_length);
 	projectiles.initialize();
 	sapin1.initialize_sapin();
+	sapin1.sapin["Trunk"].transform_local.rotation = rotation_transform::from_axis_angle({ 1,0,0 }, Pi / 2);
 }
 
+/*
 void scene_structure::initialize_mvt()
 {
 	// Definition of the initial data
@@ -132,6 +135,8 @@ void scene_structure::initialize_mvt()
 	timer_mvt.t = timer_mvt.t_min;
 
 }
+*/
+
 
 void scene_structure::display_frame()
 {
@@ -164,15 +169,14 @@ void scene_structure::display_frame()
 		//tree.model.translation = tree_position[i];
 		//draw(tree, environment);
 		sapin1.sapin["Trunk"].transform_local.translation = tree_position[i];
-		sapin1.sapin["Trunk"].transform_local.rotation = rotation_transform::from_axis_angle({ 1,0,0 }, Pi / 2);
 		sapin1.sapin.update_local_to_global_coordinates();
 		draw(sapin1.sapin, environment);
 	}
-	vec3 p = display_mvt();
+	//vec3 p = display_mvt();
 	//display_bird(p);
-	display_chain(p);
+	//display_chain(p);
 	display_ball();
-	display_bat(p);
+	display_bat();
 	display_projectiles();
 
 
@@ -183,7 +187,7 @@ void scene_structure::display_frame()
 
 }
 
-
+/*
 void scene_structure::display_bird(vec3 p)
 {
 	// Apply transformation to some elements of the hierarchy
@@ -213,7 +217,7 @@ void scene_structure::display_bird(vec3 p)
 	hierarchy["Cube base"].transform_local.rotation = rotation_transform::from_axis_angle({ 1,0,0 }, 0.8 * cos(3 * timer.t));
 
 	hierarchy.update_local_to_global_coordinates();
-	*/
+	
 
 	draw(bird1.bird, environment);
 	if (gui.display_wireframe) {
@@ -223,8 +227,9 @@ void scene_structure::display_bird(vec3 p)
 
 
 }
+*/
 
-void scene_structure::display_bat(vec3 p)
+void scene_structure::display_bat()
 {
 	// Apply transformation to some elements of the hierarchy
 	bat1.bat["Bat wing left1"].transform_local.rotation = rotation_transform::from_axis_angle({ 0,1,0 }, 0.5 * cos(5 * timer.t));
@@ -234,23 +239,24 @@ void scene_structure::display_bat(vec3 p)
 	// This function must be called before the drawing in order to propagate the deformations through the hierarchy
 	bat1.bat.update_local_to_global_coordinates();
 
-
-	
-	//Orientation and position of the bird along the path
-	float t = timer_mvt.t;
-	vec3 p2;
-	if (t + 0.1f > timer_mvt.t_max) p2 = interpolation(0.01f, keyframe.key_positions, keyframe.key_times, gui.k);
-	else p2 = interpolation((t + 0.1f), keyframe.key_positions, keyframe.key_times, gui.k);
-	bat1.bat["Bat base"].transform_local.rotation = rotation_transform::from_vector_transform({ 0,-1,0 }, normalize(p2 - p));
-	bat1.bat["Bat base"].transform_local.translation = p;
-
-
-	draw(bat1.bat, environment);
-	if (gui.display_wireframe) {
-		draw_wireframe(bat1.bat, environment);
+	bat1.display_mvt();
+	for (int m = 0; m < bat1.N; m++) {
+		//Rescale
+		bat1.bat["Bat base"].drawable.model.scaling = bat1.size[m];
+		bat1.bat["Bat wing left1"].drawable.model.scaling = bat1.size[m];
+		bat1.bat["Bat wing right1"].drawable.model.scaling = bat1.size[m];
+		//Orientation and position of the bat along the path
+		bat1.bat["Bat base"].transform_local.rotation = rotation_transform::from_vector_transform({ 0,-1,0 }, normalize(bat1.pos2[m] - bat1.pos[m]));
+		bat1.bat["Bat base"].transform_local.translation = bat1.pos[m];
+
+		if (!bat1.isdead[m])	draw(bat1.bat, environment);
+		if (gui.display_wireframe) {
+			draw_wireframe(bat1.bat, environment);
+		}
 	}
 }
 
+
 void scene_structure::display_semiTransparent()
 {
 	// Enable use of alpha component as color blending for transparent elements
@@ -285,13 +291,9 @@ void scene_structure::display_semiTransparent()
 
 
 
-
+/*
 vec3 scene_structure::display_mvt()
 {
-	// Basic elements of the scene
-	environment.light = camera_control.camera_model.position();
-	if (gui.display_frame)
-		draw(global_frame, environment);
 
 	// Update the current time
 	timer_mvt.update();
@@ -311,6 +313,7 @@ vec3 scene_structure::display_mvt()
 	// Display the interpolated position (and its trajectory)
 	//keyframe.display_current_position(p, environment);
 }
+*/
 
 void scene_structure::draw_segment(vec3 const& a, vec3 const& b)
 {
@@ -345,13 +348,13 @@ void scene_structure::display_gui()
 	ImGui::Checkbox("Wireframe", &gui.display_wireframe);
 	ImGui::Checkbox("Fly", &gui.fly);
 
-	ImGui::SliderFloat("Time", &timer_mvt.t, timer_mvt.t_min, timer_mvt.t_max);
-	ImGui::SliderFloat("Time scale", &timer_mvt.scale, 0.0f, 2.0f);
+	//ImGui::SliderFloat("Time", &timer_mvt.t, timer_mvt.t_min, timer_mvt.t_max);
+	//ImGui::SliderFloat("Time scale", &timer_mvt.scale, 0.0f, 2.0f);
 	ImGui::SliderFloat("K", &gui.k, 0.0f, 10.0f);
 	ImGui::SliderFloat("Speed", &gui.speed, 0.0f, 10.0f);
 
 	// Display the GUI associated to the key position
-	keyframe.display_gui();
+	//keyframe.display_gui();
 }
 
 void scene_structure::display_ball() {
diff --git a/projet-code/scenes_inf443/base/src/scene.hpp b/projet-code/scenes_inf443/base/src/scene.hpp
index 19fa3bb..3360957 100644
--- a/projet-code/scenes_inf443/base/src/scene.hpp
+++ b/projet-code/scenes_inf443/base/src/scene.hpp
@@ -60,11 +60,9 @@ struct scene_structure : cgp::scene_inputs_generic {
 	cgp::mesh_drawable grass;
 	
 
-	// A helper structure used to store and display the key positions/time
-	keyframe_structure keyframe;
 
-	// Timer used for the interpolation of the position
-	cgp::timer_interval timer_mvt;
+
+
 
 	// Timer used for the animation
 	timer_basic timer_chain;
@@ -86,7 +84,7 @@ struct scene_structure : cgp::scene_inputs_generic {
 	void display_semiTransparent();
 	void display_chain(vec3 bird_pos);
 	void display_bird(vec3 p);
-	void display_bat(vec3 p);
+	void display_bat();
 
 	void initialize_mvt();
 	vec3 display_mvt();
@@ -99,6 +97,7 @@ struct scene_structure : cgp::scene_inputs_generic {
 	void draw_segment(vec3 const& a, vec3 const& b);
 
 	void display_ball();
+	bool scene_structure::test_dead(vec3 p);
 	void display_projectiles();
 };
 
-- 
GitLab