From fbb8fee361045994cf8f6f6154d7ce287480fd1a Mon Sep 17 00:00:00 2001 From: "remi.greub" <remi.greub@hes-so.ch> Date: Wed, 2 Apr 2025 09:45:42 +0200 Subject: [PATCH] derniere version --- G3_TP1_labyrinth_etu/src/labyrinth.c | 106 ++++++++++++++++----------- 1 file changed, 64 insertions(+), 42 deletions(-) diff --git a/G3_TP1_labyrinth_etu/src/labyrinth.c b/G3_TP1_labyrinth_etu/src/labyrinth.c index ab7340d..7c9f50f 100644 --- a/G3_TP1_labyrinth_etu/src/labyrinth.c +++ b/G3_TP1_labyrinth_etu/src/labyrinth.c @@ -117,13 +117,10 @@ int main(void) accel_read(&ball_accel); // example: reading of the accelerometer // creating tasks... - /*for(int i = 0; i<NUM_THREADS; i++){ - - }*/ xTaskCreate(Algo_main_ball, (signed portCHAR *)"Masterball", configMINIMAL_STACK_SIZE, &ball_param_th[0], tskIDLE_PRIORITY+1, NULL); - /*xTaskCreate(Algo_bots, (signed portCHAR *)"libreball1", + xTaskCreate(Algo_bots, (signed portCHAR *)"libreball1", configMINIMAL_STACK_SIZE, &ball_param_th[2], tskIDLE_PRIORITY+1, NULL); xTaskCreate(Algo_bots, (signed portCHAR *)"libreball2", @@ -131,7 +128,7 @@ int main(void) NULL); xTaskCreate(Algo_bots, (signed portCHAR *)"libreball3", configMINIMAL_STACK_SIZE, &ball_param_th[4], tskIDLE_PRIORITY+1, - NULL);*/ + NULL); vTaskStartScheduler(); // launch scheduler LPC_TIM0->TCR = 1; @@ -144,9 +141,8 @@ void Algo_main_ball(void *params){ ball_param_th_t *ball = (ball_param_th_t *)params; accel_t ball_acc; int un_ou_moins_un_x = 1, un_ou_moins_un_y = 1; - //bool collision_prev=false; while(1){ - lcd_filled_circle(ball->ball.pos.x, ball->ball.pos.y, ball->ball.radius, LCD_BLACK); + lcd_filled_circle(round(ball->ball.pos.x), round(ball->ball.pos.y), ball->ball.radius, LCD_BLACK); int CollisionState = test_collision(&ball->ball); if(CollisionState != NO_COLLISION){ //ralentissement de 10% de la vitesse de la balle @@ -159,6 +155,7 @@ void Algo_main_ball(void *params){ //ball->ball.speed.x *= -REBOUND_FACTOR; //ball->ball.pos.x += ball->ball.radius*0.4; //round ball->ball.pos.x += 2; + //ball->ball.pos.x = ceil(ball->ball.pos.x); } if(CollisionState & TOUCH_UP){ un_ou_moins_un_y = -1; @@ -166,6 +163,7 @@ void Algo_main_ball(void *params){ //ball->ball.speed.y *= -REBOUND_FACTOR; //ball->ball.pos.y += ball->ball.radius*0.4; //round() ball->ball.pos.y += 2; + //ball->ball.pos.y = ceil(ball->ball.pos.y); } if(CollisionState & TOUCH_RIGHT){ un_ou_moins_un_x = -1; @@ -173,6 +171,7 @@ void Algo_main_ball(void *params){ //ball->ball.speed.x *= -REBOUND_FACTOR; //ball->ball.pos.x -= ball->ball.radius*0.4; //round() ball->ball.pos.x -= 2; + //ball->ball.pos.x = floor(ball->ball.pos.x); } if(CollisionState & TOUCH_DOWN){ un_ou_moins_un_y = -1; @@ -180,14 +179,14 @@ void Algo_main_ball(void *params){ //ball->ball.speed.y *= -REBOUND_FACTOR; //ball->ball.pos.y -= ball->ball.radius*0.4; //round() ball->ball.pos.y -= 2; + //ball->ball.pos.y = floor(ball->ball.pos.y); } ball->ball.pos.x = round(ball->ball.pos.x); ball->ball.pos.y = round(ball->ball.pos.y); - //collision_prev = true; }else{ accel_read(&ball_acc); - ball->ball.speed.x =((ball_acc.accel_x*0.4)+ball->ball.speed.x)*(un_ou_moins_un_x); - ball->ball.speed.y = ((ball_acc.accel_y*0.4)+ball->ball.speed.y)*(un_ou_moins_un_y); + ball->ball.speed.x =((ball_acc.accel_x*0.3)+ball->ball.speed.x)*(un_ou_moins_un_x); + ball->ball.speed.y = ((ball_acc.accel_y*0.3)+ball->ball.speed.y)*(un_ou_moins_un_y); ball->ball.speed.x = (ball->ball.speed.x >= SPEED_MAX) ? SPEED_MAX: ball->ball.speed.x; ball->ball.speed.y = (ball->ball.speed.y >= SPEED_MAX) ? SPEED_MAX: ball->ball.speed.y; @@ -198,27 +197,24 @@ void Algo_main_ball(void *params){ ball->ball.pos.y = ball->ball.pos.y + ball->ball.speed.y*(un_ou_moins_un_y); un_ou_moins_un_y = 1; un_ou_moins_un_x = 1; - //if(collision_prev){ - ball->ball.pos.x = round(ball->ball.pos.x); - ball->ball.pos.y = round(ball->ball.pos.y); - //collision_prev = false; - //} + //ball->ball.pos.x = round(ball->ball.pos.x); + //ball->ball.pos.y = round(ball->ball.pos.y); } //détection des collisions sur les bords de l'ecran LCD - if(ball->ball.pos.x <= ball->ball.radius){ + if(round(ball->ball.pos.x) <= ball->ball.radius){ ball->ball.pos.x = ball->ball.radius+1; } - if(ball->ball.pos.y <= ball->ball.radius){ + if(round(ball->ball.pos.y) <= ball->ball.radius){ ball->ball.pos.y = ball->ball.radius+1; } - if(ball->ball.pos.x >= (LCD_MAX_WIDTH - (ball->ball.radius))){ + if(round(ball->ball.pos.x) >= (LCD_MAX_WIDTH - (ball->ball.radius))){ ball->ball.pos.x = (LCD_MAX_WIDTH - (ball->ball.radius+1)); } - if(ball->ball.pos.y >= (LCD_MAX_HEIGHT - (ball->ball.radius))){ + if(round(ball->ball.pos.y) >= (LCD_MAX_HEIGHT - (ball->ball.radius))){ ball->ball.pos.y = (LCD_MAX_HEIGHT - (ball->ball.radius+1)); } - lcd_filled_circle(ball->ball.pos.x, ball->ball.pos.y, ball->ball.radius, ball->ball.color); + lcd_filled_circle(round(ball->ball.pos.x), round(ball->ball.pos.y), ball->ball.radius, ball->ball.color); vTaskDelay((portTickType)20/portTICK_RATE_MS); } } @@ -228,53 +224,79 @@ void Algo_bots(void *params){ accel_t ball_acc; int un_ou_moins_un_x = 1, un_ou_moins_un_y = 1; while(1){ - lcd_filled_circle(ball->ball.pos.x, ball->ball.pos.y, ball->ball.radius, LCD_BLACK); + lcd_filled_circle(round(ball->ball.pos.x), round(ball->ball.pos.y), ball->ball.radius, LCD_BLACK); int CollisionState = test_collision(&ball->ball); if(CollisionState != NO_COLLISION){ //ralentissement de 10% de la vitesse de la balle - ball->ball.speed.x = ball->ball.speed.x - (ball->ball.speed.x/10); - ball->ball.speed.y = ball->ball.speed.y - (ball->ball.speed.y/10); + //ball->ball.speed.x = ball->ball.speed.x - (ball->ball.speed.x/10); + //ball->ball.speed.y = ball->ball.speed.y - (ball->ball.speed.y/10); if(CollisionState & TOUCH_LEFT){ un_ou_moins_un_x = -1; - ball->ball.pos.x += ball->ball.radius/2; + ball->ball.speed.x = ball->ball.speed.x - (ball->ball.speed.x/10); + //ball->ball.speed.x *= -REBOUND_FACTOR; + //ball->ball.pos.x += ball->ball.radius*0.4; //round + ball->ball.pos.x += 3; + //ball->ball.pos.x = ceil(ball->ball.pos.x); } if(CollisionState & TOUCH_UP){ un_ou_moins_un_y = -1; - ball->ball.pos.y += ball->ball.radius/2; + ball->ball.speed.y = ball->ball.speed.y - (ball->ball.speed.y/10); + //ball->ball.speed.y *= -REBOUND_FACTOR; + //ball->ball.pos.y += ball->ball.radius*0.4; //round() + ball->ball.pos.y += 3; + //ball->ball.pos.y = ceil(ball->ball.pos.y); } if(CollisionState & TOUCH_RIGHT){ un_ou_moins_un_x = -1; - ball->ball.pos.x -= ball->ball.radius/2; + ball->ball.speed.x = ball->ball.speed.x - (ball->ball.speed.x/10); + //ball->ball.speed.x *= -REBOUND_FACTOR; + //ball->ball.pos.x -= ball->ball.radius*0.4; //round() + ball->ball.pos.x -= 3; + //ball->ball.pos.x = floor(ball->ball.pos.x); } if(CollisionState & TOUCH_DOWN){ un_ou_moins_un_y = -1; - ball->ball.pos.y -= ball->ball.radius/2; + ball->ball.speed.y = ball->ball.speed.y - (ball->ball.speed.y/10); + //ball->ball.speed.y *= -REBOUND_FACTOR; + //ball->ball.pos.y -= ball->ball.radius*0.4; //round() + ball->ball.pos.y -= 3; + //ball->ball.pos.y = floor(ball->ball.pos.y); } + ball->ball.pos.x = round(ball->ball.pos.x); + ball->ball.pos.y = round(ball->ball.pos.y); }else{ RandAccel(&ball_acc); - ball->ball.speed.x = ((ball_acc.accel_x*0.1)+ball->ball.speed.x)*(un_ou_moins_un_x); - ball->ball.speed.y = ((ball_acc.accel_y*0.1)+ball->ball.speed.y)*(un_ou_moins_un_y); + ball->ball.speed.x =((ball_acc.accel_x*0.4)+ball->ball.speed.x)*(un_ou_moins_un_x); + ball->ball.speed.y = ((ball_acc.accel_y*0.4)+ball->ball.speed.y)*(un_ou_moins_un_y); + + ball->ball.speed.x = (ball->ball.speed.x >= SPEED_MAX) ? SPEED_MAX: ball->ball.speed.x; + ball->ball.speed.y = (ball->ball.speed.y >= SPEED_MAX) ? SPEED_MAX: ball->ball.speed.y; + ball->ball.speed.x = (ball->ball.speed.x <= -SPEED_MAX) ? -SPEED_MAX: ball->ball.speed.x; + ball->ball.speed.y = (ball->ball.speed.y <= -SPEED_MAX) ? -SPEED_MAX: ball->ball.speed.y; ball->ball.pos.x = ball->ball.pos.x + ball->ball.speed.x*(un_ou_moins_un_x); ball->ball.pos.y = ball->ball.pos.y + ball->ball.speed.y*(un_ou_moins_un_y); un_ou_moins_un_y = 1; un_ou_moins_un_x = 1; + //ball->ball.pos.x = round(ball->ball.pos.x); + //ball->ball.pos.y = round(ball->ball.pos.y); + } - if(ball->ball.pos.x <= ball->ball.radius){ - ball->ball.pos.x = ball->ball.radius+1; - } - if(ball->ball.pos.y <= ball->ball.radius){ - ball->ball.pos.y = ball->ball.radius+1; - } - if(ball->ball.pos.x >= LCD_MAX_WIDTH-ball->ball.radius){ - ball->ball.pos.x = LCD_MAX_WIDTH-ball->ball.radius; - } - if(ball->ball.pos.y >= LCD_MAX_HEIGHT-ball->ball.radius){ - ball->ball.pos.y = LCD_MAX_HEIGHT-ball->ball.radius; - } + //détection des collisions sur les bords de l'ecran LCD + if(round(ball->ball.pos.x) <= ball->ball.radius){ + ball->ball.pos.x = ball->ball.radius+1; + } + if(round(ball->ball.pos.y) <= ball->ball.radius){ + ball->ball.pos.y = ball->ball.radius+1; + } + if(round(ball->ball.pos.x) >= (LCD_MAX_WIDTH - (ball->ball.radius))){ + ball->ball.pos.x = (LCD_MAX_WIDTH - (ball->ball.radius+1)); + } + if(round(ball->ball.pos.y) >= (LCD_MAX_HEIGHT - (ball->ball.radius))){ + ball->ball.pos.y = (LCD_MAX_HEIGHT - (ball->ball.radius+1)); } - lcd_filled_circle(ball->ball.pos.x, ball->ball.pos.y, ball->ball.radius, ball->ball.color); + lcd_filled_circle(round(ball->ball.pos.x), round(ball->ball.pos.y), ball->ball.radius, ball->ball.color); vTaskDelay((portTickType)20/portTICK_RATE_MS); } } -- GitLab