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

Author Topic: Trying to sync rocket with rocket launcher  (Read 1370 times)

0 Members and 1 Guest are viewing this topic.

ohgodmanyo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Trying to sync rocket with rocket launcher
« on: February 03, 2022, 10:20:56 pm »
Hello, I am trying to get the rocket to sync up with the rocket launcher. What my code does, is it sets the rocket to the position of the player, sets it's angle to the rocket launcher's angle, and then tries to move it a bit forward based on it's angle so that the rocket looks like it just got launched out of the launcher. The only thing that doesn't work is that the rocket is moving multiple times faster than the actual rocket launcher for some reason. The only thing that messed this up was when I tried to move the rocket forward from the player using sin and cos. Please help me sync them up so that the rocket looks like it's being fired from the rocket launcher.


    this->rocket->updatePos(this->player->getPos());
    this->rocket->updateRot(this->launcher->getRot());
    this->rocket->updatePos(sf::Vector2f(this->player->getPos().x + (25 * cos(this->rocket->getRot())), this->player->getPos().y + (25 * -sin(this->rocket->getRot()))));

 

Video of what is going on:

kojack

  • Sr. Member
  • ****
  • Posts: 314
  • C++/C# game dev teacher.
    • View Profile
Re: Trying to sync rocket with rocket launcher
« Reply #1 on: February 04, 2022, 12:08:56 am »
That looks like getRot() is returning angles in degrees.
But cos and sin take angles in radians.
Try making it:
this->rocket->updatePos(sf::Vector2f(this->player->getPos().x + (25 * cos(this->rocket->getRot() * 3.141592654f / 180.f)), this->player->getPos().y + (25 * -sin(this->rocket->getRot() * 3.141592654f / 180.f))));

It also looks like the rocket is rotating in the wrong direction, if so, try removing the - from the sin part, that will reverse rotation.

ohgodmanyo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Trying to sync rocket with rocket launcher
« Reply #2 on: February 04, 2022, 12:21:32 am »
That looks like getRot() is returning angles in degrees.
But cos and sin take angles in radians.
Try making it:
this->rocket->updatePos(sf::Vector2f(this->player->getPos().x + (25 * cos(this->rocket->getRot() * 3.141592654f / 180.f)), this->player->getPos().y + (25 * -sin(this->rocket->getRot() * 3.141592654f / 180.f))));

It also looks like the rocket is rotating in the wrong direction, if so, try removing the - from the sin part, that will reverse rotation.

Thank you very much, this works perfectly!