Hi there !
I want to make a AI for an evil i decide to make the evil follow the player.My problem is that the evil doesn’t move correctly he doesn’t follow the player. i implement this code:
> float checkAngle(SDL_Rect a, SDL_Rect b ){
double dx =b.x - a.x;
double dy =b.y - a.y;
printf("distance x : %f\n",dx);
printf("distance y : %f\n",dy);
float angle2=atan(dy/dx)* 180 / PI;
printf("angle 2 : %f\n",angle2);
return angle2;
return 0;
}
void ennemi::move( Tile *tiles[],int *ptr_diff,perso perso,bool *quit,float angle)
{
minotaureBox.x += minoVelX*(cos(angle*PI/180));
minotaureBox.y += minoVelY*(sin(angle*PI/180));
// minotaureBox.x += minoVelX;
if(checkCollision(minotaureBox,perso.getBox())){
minotaureBox.x -= minoVelX;
minotaureBox.y -= minoVelY;
// game_over(quit);
}
//If the perso went too far to the left or right or touched a wall
if( ( minotaureBox.x < 0 ) || ( minotaureBox.x + MINOTAURE_WIDTH > LEVEL_WIDTH ) || touchesWall( minotaureBox, tiles ) )
{
//move back
minotaureBox.x -= minoVelX;
minoVelX= -minoVelX;
}
minotaureBox.y += minoVelY;
//If the perso went too far up or down or touched a wall
if( ( minotaureBox.y < 0 ) || ( minotaureBox.y + MINOTAURE_HEIGHT > LEVEL_HEIGHT ) || touchesWall( minotaureBox, tiles ) )
{
//move back
minotaureBox.y -= minoVelY;
// minoVelY= -minoVelY;
}
}
In play function i have this:
int play(int *ptr_diff){
//The level tiles Tile* tiles[ TOTAL_TILES ]; //TABLEAU DE TUILES if( !load_e( tiles ) ) { printf( "Failed to load carte!\n" ); } if( !chargement_perso() ) { printf( "Failed to load perso!\n" );
}
//Event handler
SDL_Event e;
//Main loop flag
bool quit = false;
//The perso that will be moving around on the screen
perso perso;
ennemi minotaure;float angle_2=0; //Current time start time Uint32 startTime = 0; startTime = SDL_GetTicks(); //Level camera SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
init2(); SDL_RenderPresent( gRenderer2 );
while( !quit ) { if( Mix_PlayingMusic() == 0 ) { //Play the music Mix_PlayMusic( gMusic, -1 ); } //Handle events on queue while( SDL_PollEvent( &e ) != 0 ) {
//User requests quit sert quand on a une seule fenetre if( e.type == SDL_QUIT ) { quit = true; } if( e.type == SDL_WINDOWEVENT && e.window.windowID == mWindowID )//1 ere fenetre { switch( e.window.event ) { case SDL_WINDOWEVENT_CLOSE: quit = true; //on ferme aussi la deuxieme fenetre
// SDL_HideWindow( gWindow );
break;
}
}
if( e.type == SDL_WINDOWEVENT && e.window.windowID == mWindowID2 ) //2 eme fenetre { switch( e.window.event ) { case SDL_WINDOWEVENT_CLOSE: SDL_HideWindow( gWindow2 ); break; } } if( e.key.keysym.sym == SDLK_s ) { Mix_PauseMusic(); printf("pause\n"); } if( e.key.keysym.sym == SDLK_p ) { Mix_ResumeMusic(); printf("reprendre musique \n"); } //Handle input for the perso perso.handleEvent( e ,minotaure); } //Move the perso
// angle_2 = checkAngle(minotaure.getBox(),perso.getBox());
perso.move( tiles,minotaure,chat,ptr_diff,&quit); minotaure.move(tiles,ptr_diff,perso,&quit,angle_2); perso.setCamera( camera ); chronometre(startTime);
//Clear screen SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( gRenderer ); // Render level for( int i = 0; i < TOTAL_TILES; ++i ) { tiles[ i ]->render( camera ); } //Render perso perso.render( camera ); minotaure.render(camera,1); if (*ptr_diff==2){ chat.render(camera,2); } //Update screen SDL_RenderPresent( gRenderer ); } close( tiles );
return 0;
}
Thanks