Home > Back-end >  Enemy does not follow Player OpenGL 2D
Enemy does not follow Player OpenGL 2D

Time:11-10

I have a project to do where one the tasks is to make an enemy that follows the player. This is how I drew the player

//Body
    modelMatrix = visMatrix;
    modelMatrix *= transform2D::Translate(translateX, translateY);
    modelMatrix *= transform2D::Scale(1, 1);
    modelMatrix *= transform2D::Rotate(playerAngle);
    modelMatrix *= transform2D::Translate(-50, -50);
    RenderMesh2D(meshes["square3"], shaders["VertexColor"], modelMatrix);

    //Eye1
    modelMatrix = visMatrix;
    modelMatrix *= transform2D::Translate(translateX, translateY);
    modelMatrix *= transform2D::Scale(0.25f, 0.25f);
    modelMatrix *= transform2D::Rotate(playerAngle);
    modelMatrix *= transform2D::Translate(150, 200);
    modelMatrix *= transform2D::Rotate(0);
    modelMatrix *= transform2D::Translate(-50, -50);
    RenderMesh2D(meshes["square1"], shaders["VertexColor"], modelMatrix);

    //Eye2
    modelMatrix = visMatrix;
    modelMatrix *= transform2D::Translate(translateX, translateY);
    modelMatrix *= transform2D::Scale(0.25f, 0.25f);
    modelMatrix *= transform2D::Rotate(playerAngle);
    modelMatrix *= transform2D::Translate(-150, 200);
    modelMatrix *= transform2D::Rotate(0);
    modelMatrix *= transform2D::Translate(-50, -50);
    RenderMesh2D(meshes["square1"], shaders["VertexColor"], modelMatrix);

This is how I drew the enemy

//Body
modelMatrix = visMatrix;
modelMatrix *= transform2D::Translate(translateEnemyX, translateEnemyY);
modelMatrix *= transform2D::Scale(1, 1);
modelMatrix *= transform2D::Rotate(enemyAngle);
modelMatrix *= transform2D::Translate(-50, -50);
RenderMesh2D(meshes["border"], shaders["VertexColor"], modelMatrix);

//Eye1
modelMatrix = visMatrix;
modelMatrix *= transform2D::Translate(translateEnemyX, translateEnemyY);
modelMatrix *= transform2D::Scale(0.25f, 0.25f);
modelMatrix *= transform2D::Rotate(enemyAngle);
modelMatrix *= transform2D::Translate(150, 200);
modelMatrix *= transform2D::Rotate(0);
modelMatrix *= transform2D::Translate(-50, -50);
RenderMesh2D(meshes["square2"], shaders["VertexColor"], modelMatrix);

//Eye2
modelMatrix = visMatrix;
modelMatrix *= transform2D::Translate(translateEnemyX, translateEnemyY);
modelMatrix *= transform2D::Scale(0.25f, 0.25f);
modelMatrix *= transform2D::Rotate(enemyAngle);
modelMatrix *= transform2D::Translate(-150, 200);
modelMatrix *= transform2D::Rotate(0);
modelMatrix *= transform2D::Translate(-50, -50);
RenderMesh2D(meshes["square2"], shaders["VertexColor"], modelMatrix);

I made the enemy to face the player by calculating the angle like this, and this works

enemyAngle = atan2(translateX - translateEnemyX, translateY - translateEnemyY);

And I have tried to move the enemy towards the player like this

enemySpeed = 10;
translateEnemyX  = enemySpeed * cos(enemyAngle);
translateEnemyY  = enemySpeed * sin(enemyAngle);

But it does not work, the enemy just moves away from the player.

The question is, how to make the enemy move towards player?

CodePudding user response:

If it moves away from the player, perhaps you switched up sin and cos.
My guess is, it should be this instead: translateEnemyX = enemySpeed * sin(enemyAngle); translateEnemyY = enemySpeed * cos(enemyAngle);

Other than that I think the right side should also contain your delta time between frames, otherwise it's going to move faster at higher frame rate.

Edit: Nevermind, that shouldn't work, your equation should contain the player's position, since that's what you want the enemy to move towards.
Try taking the difference between the enemy position and the player position, something like this: translateEnemyX = (translatePlayerX - translateEnemyX) * enemySpeed * deltaTime; translateEnemyY = (translatePlayerY - translateEnemyY) * enemySpeed * deltaTime;

  • Related