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

ajout hit detection

parent 36ec91c9
No related branches found
No related tags found
No related merge requests found
...@@ -30,6 +30,30 @@ void initPlayer(){ ...@@ -30,6 +30,30 @@ void initPlayer(){
player.viewAngle = 0; player.viewAngle = 0;
} }
void hitBall(){
//printf("map edges: %d %d\n", BLOCK_SIZE * MAP_WIDTH/2, BLOCK_SIZE *MAP_HEIGHT/2);
//printf("ray1: %d %d\n", ray1[0], ray1[1]);
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("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");
if (player.isHitting){
ball.x = player.x;
ball.y = player.y;
}
printf("valid hit\n");
}
else {
printf("unvalid hit\n");
}
}
//}
}
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);
...@@ -45,4 +69,5 @@ void manageMovement(){ ...@@ -45,4 +69,5 @@ void manageMovement(){
void managePlayer(){ void managePlayer(){
manageMovement(); manageMovement();
hitBall();
} }
\ No newline at end of file
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#define ENTITIES_LEFT 2 #define ENTITIES_LEFT 2
#define ENTITIES_RIGHT 3 #define ENTITIES_RIGHT 3
#define HIT_RANGE 2
#define MOVEMENT_SPEED 2 #define MOVEMENT_SPEED 2
typedef struct player{ typedef struct player{
float x; float x;
...@@ -17,6 +19,7 @@ typedef struct player{ ...@@ -17,6 +19,7 @@ typedef struct player{
int w; int w;
int speed; int speed;
int isMoving; int isMoving;
int isHitting;
int direction; int direction;
int HPMax; int HPMax;
int currentHP; int currentHP;
......
...@@ -18,6 +18,8 @@ SDL_Texture * playerTexture; ...@@ -18,6 +18,8 @@ SDL_Texture * playerTexture;
int ** rays; int ** rays;
int raysListLength = 0; int raysListLength = 0;
int * ray1;
int * ray2;
rayInfo_t raysListHead; rayInfo_t raysListHead;
...@@ -69,6 +71,10 @@ void initRays(){ ...@@ -69,6 +71,10 @@ void initRays(){
} }
} }
int isRaysListEmpty(){
return raysListLength == 0;
}
void addRayToList(int x, int y){ void addRayToList(int x, int y){
if (raysListLength < 2 * NB_RAYS){ if (raysListLength < 2 * NB_RAYS){
*rays[raysListLength] = x; *rays[raysListLength] = x;
...@@ -391,6 +397,21 @@ void castRays(int map[][MAP_WIDTH]){ ...@@ -391,6 +397,21 @@ void castRays(int map[][MAP_WIDTH]){
} }
} }
// draw the ray in the minimap // draw the ray in the minimap
if (r == 0){
//printf("%d %d\n", (int)rx, (int)ry);
ray1[0] = (int)rx;
ray1[1] = (int)ry;
//printf("ray1 %d %d\n", ray1[0], ray1[1]);
//printf("ray2 %d %d\n", ray2[0], ray2[1]);
}
if (r == NB_RAYS - 1){
//printf("%d %d\n", (int)rx, (int)ry);
ray2[0] = (int)rx;
ray2[1] = (int)ry;
printf("ray1 %d %d\n", ray1[0]/BLOCK_SIZE, ray1[1]/BLOCK_SIZE);
printf("ray2 %d %d\n", ray2[0]/BLOCK_SIZE, ray2[1]/BLOCK_SIZE);
}
//printf("raylistlength %d\n", raysListLength);
addRayToList(rx, ry); addRayToList(rx, ry);
addRayToList(rx2, ry2); addRayToList(rx2, ry2);
...@@ -410,7 +431,7 @@ void drawEnnemy(){ ...@@ -410,7 +431,7 @@ void drawEnnemy(){
int ennemyHeight = 200; int ennemyHeight = 200;
//printf("%f %f\n", ennemyAngle, player.angle - (FOV_ANGLE)/2 * DR); //printf("%f %f\n", ennemyAngle, player.angle - (FOV_ANGLE)/2 * DR);
//printf("%f\n", player.angle * RD);
if (ennemyAngle >= player.angle - (FOV_ANGLE)/2 * DR && ennemyAngle <= player.angle + (FOV_ANGLE)/2 * DR){ if (ennemyAngle >= player.angle - (FOV_ANGLE)/2 * DR && ennemyAngle <= player.angle + (FOV_ANGLE)/2 * DR){
rect.x = screenDimension.w/2 + (screenDimension.w * tan(ennemyAngle - player.angle)) * sqrt(3) * 0.5; rect.x = screenDimension.w/2 + (screenDimension.w * tan(ennemyAngle - player.angle)) * sqrt(3) * 0.5;
...@@ -422,7 +443,7 @@ void drawEnnemy(){ ...@@ -422,7 +443,7 @@ void drawEnnemy(){
destRect.y = 0; destRect.y = 0;
destRect.w = 64; destRect.w = 64;
destRect.h = 64; destRect.h = 64;
printf("%d %d %d %d\n", rect.x, rect.y, rect.w, rect.h); //printf("%d %d %d %d\n", rect.x, rect.y, rect.w, rect.h);
SDL_RenderCopy(renderer, playerTexture, &destRect, &rect); SDL_RenderCopy(renderer, playerTexture, &destRect, &rect);
} }
} }
...@@ -449,7 +470,7 @@ void drawBall(){ ...@@ -449,7 +470,7 @@ void drawBall(){
destRect.y = 0; destRect.y = 0;
destRect.w = 64; destRect.w = 64;
destRect.h = 64; destRect.h = 64;
printf("%d %d %d %d\n", rect.x, rect.y, rect.w, rect.h); //printf("%d %d %d %d\n", rect.x, rect.y, rect.w, rect.h);
SDL_RenderCopy(renderer, playerTexture, &destRect, &rect); SDL_RenderCopy(renderer, playerTexture, &destRect, &rect);
} }
} }
...@@ -546,6 +567,9 @@ void mainLoop(){ ...@@ -546,6 +567,9 @@ void mainLoop(){
crowdTexture = loadTexture("Res/crowd.png"); crowdTexture = loadTexture("Res/crowd.png");
playerTexture = loadTexture("Res/player_sprite.png"); playerTexture = loadTexture("Res/player_sprite.png");
ray1 = malloc(sizeof(int) * 2);
ray2 = malloc(sizeof(int) * 2);
unsigned int a = SDL_GetTicks(); unsigned int a = SDL_GetTicks();
unsigned int b = SDL_GetTicks(); unsigned int b = SDL_GetTicks();
double delta = 0; double delta = 0;
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#define DOF 100 #define DOF 100
#define BLOCK_SIZE 64 #define BLOCK_SIZE 64
#define DR 0.0174533 #define DR 0.0174533
#define RD 57.2958
#define FOV_ANGLE 120 #define FOV_ANGLE 120
#define pi 3.14159265358979323846 #define pi 3.14159265358979323846
#define NB_RAYS (screenDimension.w) #define NB_RAYS (screenDimension.w)
...@@ -31,6 +32,8 @@ extern SDL_Window *window; ...@@ -31,6 +32,8 @@ extern SDL_Window *window;
extern SDL_Renderer *renderer; extern SDL_Renderer *renderer;
extern SDL_DisplayMode screenDimension; extern SDL_DisplayMode screenDimension;
extern int * ray1;
extern int * ray2;
void mainLoop(); void mainLoop();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment