Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Mover un objeto hacia el jugador  (Read 1552 times)

0 Members and 1 Guest are viewing this topic.

H3lltronik

  • Newbie
  • *
  • Posts: 2
    • View Profile
Mover un objeto hacia el jugador
« on: May 24, 2017, 07:18:48 am »
Hola, soy nuevo en este foro  :)

Estoy entrando en el mundo de la programación de video juegos en sfml, soy bastante nuevo :p
Mi duda es, como logro hacer que un objeto persiga al jugador en todo momento? es decir si este se mueve hacia cualquier lado de la pantalla, el objeto enemigo debe ser capaz de dirigirse hacia la posición del usuario aunque se este moviendo, No logro conseguirlo. Este es el codigo que use para hacer que el enemigo se mueva hacia el jugador pero mientras el jugador se mueve el enemigo da vueltas a lo loco en lugar se perseguir al usuario como quiero que lo haga
void Enemigo::Mover(Jugador &jugador){
    static const float PI = 3.14159265;
    sf::Vector2f Direccion;
    sf::Vector2f PlayPos = jugador.getCarro().getPosition();
    sf::Vector2f EnePos = enemigo.getPosition();

    float dx = PlayPos.x - EnePos.x;
    float dy = PlayPos.y - EnePos.y;

    rotacion = ((atan2(dy, dx)) * 180 / PI)+180;

    Direccion.x = cos(rotacion);
    Direccion.y = sin(rotacion);

    enemigo.move(Direccion);
}

Cualquier clase de ayuda me vendría bastante bien  :(

In "English"
I need to move an object to the player, just like chasing him. My problem is when the player moves the enemy doesn't go to the player, instead it just revolves around till the player stop then it goes to the player position and I need that the enemy goes to the player position in anytime even if the player is moving. Sorry for the bad english
« Last Edit: May 24, 2017, 08:15:37 am by H3lltronik »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Mover un objeto hacia el jugador
« Reply #1 on: May 24, 2017, 07:51:09 am »
In english please :D
Laurent Gomila - SFML developer

H3lltronik

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Mover un objeto hacia el jugador
« Reply #2 on: May 24, 2017, 08:00:30 am »
XD I need to move an object to the player, just like chasing him. My problem is when the player moves the enemy doesn't go to the player, instead it just revolves around till the player stop then it goes to the player position and I need that the enemy goes to the player position in anytime even if the player is moving. I used google transalte, Im bad at English

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Mover un objeto hacia el jugador
« Reply #3 on: May 24, 2017, 08:10:00 am »
Can you rather edit your first post please? Thanks :P
Laurent Gomila - SFML developer

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Mover un objeto hacia el jugador
« Reply #4 on: May 24, 2017, 03:44:59 pm »
atan2, sin and cos use radians, you do not have to convert anything.

 

anything