diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/ball.c b/travail_de_groupe/jeu_appren_par_renfo/src/ball.c new file mode 100644 index 0000000000000000000000000000000000000000..80878b28f59c8802ed5f1cefc003e449e3b2154e --- /dev/null +++ b/travail_de_groupe/jeu_appren_par_renfo/src/ball.c @@ -0,0 +1,87 @@ +#include "ball.h" + +//ball_t ball; +int trajectoireAntoine[NUMBERPOINT_TRAJEC][2]; + +void initBall() +{ +} + +float defineAngle(int canonX, int canonY, int xDropPoint, int yDropPoint) +{ + float distance; + float angleSin; + distance = sqrtf(powf((float)(xDropPoint - canonX), 2) + powf((float)(yDropPoint - canonY), 2)); + angleSin = asinf(distance / (xDropPoint - canonX)); + return angleSin; +} + +/* + * Fonction qui prend une valeur de x et 3 points. Elle + * renvoie la coordonnée y liée à la valeur de x sur la + * courbe passant par les 3 points. + * Valeur de x : float xp + * Point n°1 (départ) : int xd, int yd + * Point n°2 (filet ) : int xf, int yf + * Point n°3 (target) : int xt, int yt + */ +int lagrangeInterpolation(float xp, int xd, int yd, int xf, int yf, int xt, int yt) +{ + float x[4], y[4], yp = 0, p; + int i, j, n; + // nombre de points pour trouver la courbe + n = 3; + // coordonnées x des 3 points + x[1] = xd; + x[2] = xf; + x[3] = xt; + // coordonnées y des 3 points + y[1] = yd; + y[2] = yf; + y[3] = yt; + // implementation de l'interpolation de Lagrange + for (i = 1; i <= n; i++) + { + p = 1; + for (j = 1; j <= n; j++) + { + if (i != j) + { + p = p * (xp - x[j]) / (x[i] - x[j]); + } + } + yp = yp + p * y[i]; + } + // affichage pouvant servir + // printf("Interpolated value at %.3f is %.3f.\n", xp, yp); + // valeur de y associée à xp sur la courbe + return yp; +} + +/* + * Fonction qui prend en paramètres 3 points et retourne met dans le + * tableau à 2 dimensions trajectoireAntoine la liste des points de + * la courbe qui passe par ces 3 points. Le pas de temps est defini + * par NUMBERPOINT_TRAJEC. + */ +void calculTrajectoireAntoine2(int xd, int yd, int xf, int yf, int xt, int yt) +{ + // pas de temps + int pas_de_temps = (xt - xd) / (NUMBERPOINT_TRAJEC - 1); + int i; + // point de départ + trajectoireAntoine[0][0] = xd; + trajectoireAntoine[0][1] = yd; + // on cherche les pas de temps pour tous les pas de temps + // sauf départ et arrivée + for (i = 1; i < NUMBERPOINT_TRAJEC - 1; i++) + { + // calcul du x + trajectoireAntoine[i][0] = (int)(xd + i * pas_de_temps); + // calcul du y + trajectoireAntoine[i][1] = lagrangeInterpolation(trajectoireAntoine[i][0], xd, yd, xf, yf, xt, yt); + } + // point d'arrivée + trajectoireAntoine[NUMBERPOINT_TRAJEC - 1][0] = xt; + trajectoireAntoine[NUMBERPOINT_TRAJEC - 1][1] = yt; +} diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/ball.h b/travail_de_groupe/jeu_appren_par_renfo/src/ball.h new file mode 100644 index 0000000000000000000000000000000000000000..d55aca2904112afc683c6b30723f6f1f4bb2c5e8 --- /dev/null +++ b/travail_de_groupe/jeu_appren_par_renfo/src/ball.h @@ -0,0 +1,35 @@ +#ifndef BALL_H +#define BALL_H + +#include <stdlib.h> +#include <time.h> +#include <stdio.h> +#include <math.h> + +#define NUMBERPOINT_TRAJEC 50 + +typedef enum +{ + false, + true +} booleen_t; + +typedef struct ball +{ + int x; + int y; + int z; + float angle; + float speed; +} ball_t; + +//extern ball_t ball; +extern int trajectoireAntoine[NUMBERPOINT_TRAJEC][2]; + +void initBall(); +float defineAngle(int, int, int, int); + +int lagrangeInterpolation(float, int, int, int, int, int, int); +void calculTrajectoireAntoine2(int, int, int, int, int, int); + +#endif \ No newline at end of file diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.c b/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.c index d8b9658623878b099f1d3e4a25c7c9fe92965991..9ca34328dd27273d46113503d3b7b4538f3f9d04 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.c +++ b/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.c @@ -119,6 +119,18 @@ void gestGame(){ break; } + case SDL_MOUSEBUTTONDOWN: + if (event.button.button == SDL_BUTTON_LEFT){ + player.isHitting = 1; + } + break; + + case SDL_MOUSEBUTTONUP: + if (event.button.button == SDL_BUTTON_LEFT){ + player.isHitting = 0; + } + break; + default: break; diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/player.c b/travail_de_groupe/jeu_appren_par_renfo/src/player.c index 4a1974e3dca673a71ceffd19f9da2de4c65ed050..5fbbe3784a063338193695e377c638dd3657777a 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/player.c +++ b/travail_de_groupe/jeu_appren_par_renfo/src/player.c @@ -6,6 +6,9 @@ player_t ennemy; player_t ball; +int * landingPoint; +int * lastHitPoint; + void initPlayer(){ player.x= 6*BLOCK_SIZE; player.y= 6*BLOCK_SIZE; @@ -28,6 +31,33 @@ void initPlayer(){ player.deltax = 0; player.deltay = 1; player.viewAngle = 0; + ball.isHit = 0; + ball.speed = 0; + ball.angle = -pi; +} + +int * generateLandingPoint(){ + int * landingPoint = malloc(sizeof(int) * 2); + int randomLength = rand() % (int)(MAP_WIDTH*BLOCK_SIZE - player.x); + int randomPointX = randomLength * cos(player.angle) + player.x; + int randomPointY = randomLength * sin(player.angle) + player.y; + landingPoint[0] = randomPointX/BLOCK_SIZE; + landingPoint[1] = randomPointY/BLOCK_SIZE; + printf("landing point: %d %d\n", landingPoint[0], landingPoint[1]); + return landingPoint; +} + +int * allocLastHitPoint(){ + int * lastHitPoint = malloc(sizeof(int) * 2); + lastHitPoint[0] = 0; + lastHitPoint[1] = 0; + return lastHitPoint; +} + +void freeIntList(int * list){ + if (list != NULL){ + free(list); + } } void hitBall(){ @@ -36,14 +66,24 @@ void hitBall(){ int angleMin = RD * atan2((MAP_WIDTH/2)*BLOCK_SIZE - player.x, player.y); int angleMax = 90 + RD * atan2((MAP_WIDTH/2)*BLOCK_SIZE - player.x, MAP_HEIGHT * BLOCK_SIZE - player.y); int currAngle = (int) ((player.angle) * RD +90) %360; - //printf("player angle: %d\n",(int) ((player.angle) * RD +90) %360 ); + //printf("player angle: %d\n",(int) ((player.angle) * RD +90) %360); printf("distance to ball: %f\n", sqrt(pow(ball.x - player.x, 2) + pow(ball.y - player.y, 2))/BLOCK_SIZE); if (sqrt(pow(player.x - ball.x, 2) + pow(player.y - ball.y, 2))/BLOCK_SIZE < HIT_RANGE){ if (currAngle < angleMax && currAngle > angleMin){ printf("hit\n"); + freeIntList(landingPoint); + freeIntList(lastHitPoint); + lastHitPoint = allocLastHitPoint(); + landingPoint = generateLandingPoint(); if (player.isHitting){ + lastHitPoint[0] = player.x; + lastHitPoint[1] = player.h; ball.x = player.x; ball.y = player.y; + ball.z = player.h; + ball.angle = player.angle; + ball.speed = HIT_FORCE; + ball.isHit = 1; } printf("valid hit\n"); } @@ -54,6 +94,16 @@ void hitBall(){ //} } +void updateBall(){ + ball.x = ball.x + ball.speed * cos(ball.angle); + ball.y = ball.y + ball.speed * sin(ball.angle); + if (ball.isHit){ + ball.z = lagrangeInterpolation(ball.x, lastHitPoint[0], lastHitPoint[1], 15 , 2*player.h, landingPoint[0], 0); + } + printf("ball position: %f %f %f\n", ball.x/BLOCK_SIZE, ball.y/BLOCK_SIZE, ball.z/BLOCK_SIZE); +} + + void manageMovement(){ float x_increment = (Keys[0] - Keys[2]) * player.deltax + (Keys[3] - Keys[1]) * sin(player.angle); float y_increment = (Keys[0] - Keys[2]) * player.deltay + (Keys[1] - Keys[3]) * cos(player.angle); @@ -70,4 +120,5 @@ void manageMovement(){ void managePlayer(){ manageMovement(); hitBall(); + updateBall(); } \ No newline at end of file diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/player.h b/travail_de_groupe/jeu_appren_par_renfo/src/player.h index b95048ddaa059bf3406a29621773764fde293457..11c624bd391ac2f8d281f06f7320a2b2ef3f0bdf 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/player.h +++ b/travail_de_groupe/jeu_appren_par_renfo/src/player.h @@ -3,6 +3,7 @@ #include "map.h" #include "render.h" +#include "ball.h" #define ENTITIES_UP 0 #define ENTITIES_DOWN 1 @@ -10,16 +11,19 @@ #define ENTITIES_RIGHT 3 #define HIT_RANGE 2 +#define HIT_FORCE 1 #define MOVEMENT_SPEED 2 typedef struct player{ float x; float y; + float z; int h; int w; int speed; int isMoving; int isHitting; + int isHit; int direction; int HPMax; int currentHP;