Skip to content
Snippets Groups Projects
Commit bdd6c11e authored by belkhiritaha's avatar belkhiritaha
Browse files

gestion de la trajectoire de la balle quand on la tape

parent 4bfdc32a
No related branches found
No related tags found
No related merge requests found
#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;
}
#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
...@@ -119,6 +119,18 @@ void gestGame(){ ...@@ -119,6 +119,18 @@ void gestGame(){
break; 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: default:
break; break;
......
...@@ -6,6 +6,9 @@ player_t ennemy; ...@@ -6,6 +6,9 @@ player_t ennemy;
player_t ball; player_t ball;
int * landingPoint;
int * lastHitPoint;
void initPlayer(){ void initPlayer(){
player.x= 6*BLOCK_SIZE; player.x= 6*BLOCK_SIZE;
player.y= 6*BLOCK_SIZE; player.y= 6*BLOCK_SIZE;
...@@ -28,6 +31,33 @@ void initPlayer(){ ...@@ -28,6 +31,33 @@ void initPlayer(){
player.deltax = 0; player.deltax = 0;
player.deltay = 1; player.deltay = 1;
player.viewAngle = 0; 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(){ void hitBall(){
...@@ -36,14 +66,24 @@ void hitBall(){ ...@@ -36,14 +66,24 @@ void hitBall(){
int angleMin = RD * atan2((MAP_WIDTH/2)*BLOCK_SIZE - player.x, player.y); 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 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; 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); 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 (sqrt(pow(player.x - ball.x, 2) + pow(player.y - ball.y, 2))/BLOCK_SIZE < HIT_RANGE){
if (currAngle < angleMax && currAngle > angleMin){ if (currAngle < angleMax && currAngle > angleMin){
printf("hit\n"); printf("hit\n");
freeIntList(landingPoint);
freeIntList(lastHitPoint);
lastHitPoint = allocLastHitPoint();
landingPoint = generateLandingPoint();
if (player.isHitting){ if (player.isHitting){
lastHitPoint[0] = player.x;
lastHitPoint[1] = player.h;
ball.x = player.x; ball.x = player.x;
ball.y = player.y; ball.y = player.y;
ball.z = player.h;
ball.angle = player.angle;
ball.speed = HIT_FORCE;
ball.isHit = 1;
} }
printf("valid hit\n"); printf("valid hit\n");
} }
...@@ -54,6 +94,16 @@ void hitBall(){ ...@@ -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(){ void manageMovement(){
float x_increment = (Keys[0] - Keys[2]) * player.deltax + (Keys[3] - Keys[1]) * sin(player.angle); 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); float y_increment = (Keys[0] - Keys[2]) * player.deltay + (Keys[1] - Keys[3]) * cos(player.angle);
...@@ -70,4 +120,5 @@ void manageMovement(){ ...@@ -70,4 +120,5 @@ void manageMovement(){
void managePlayer(){ void managePlayer(){
manageMovement(); manageMovement();
hitBall(); hitBall();
updateBall();
} }
\ No newline at end of file
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "map.h" #include "map.h"
#include "render.h" #include "render.h"
#include "ball.h"
#define ENTITIES_UP 0 #define ENTITIES_UP 0
#define ENTITIES_DOWN 1 #define ENTITIES_DOWN 1
...@@ -10,16 +11,19 @@ ...@@ -10,16 +11,19 @@
#define ENTITIES_RIGHT 3 #define ENTITIES_RIGHT 3
#define HIT_RANGE 2 #define HIT_RANGE 2
#define HIT_FORCE 1
#define MOVEMENT_SPEED 2 #define MOVEMENT_SPEED 2
typedef struct player{ typedef struct player{
float x; float x;
float y; float y;
float z;
int h; int h;
int w; int w;
int speed; int speed;
int isMoving; int isMoving;
int isHitting; int isHitting;
int isHit;
int direction; int direction;
int HPMax; int HPMax;
int currentHP; int currentHP;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment