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

Author Topic: [SOLVED] Rotating bullet around ship  (Read 1116 times)

0 Members and 1 Guest are viewing this topic.

Eulavvalue

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Rotating bullet around ship
« on: May 23, 2013, 04:19:03 am »
Solved: I was scaling my ship and bullet to 0.3 / 0.3 which was messing with calculations. Resized all of them to 30% in an image editor and use no scales and worked fine.

Hello, I'm creating a game similar to asteroids and I'm having trouble trying to set my bullets position to the front of the ship. I've read multiple threads and tried different solutions, but whenever I tried to rotate the bullet it wouldn't rotate around the ship.

From what I understand I want to set my bullets origin to the ships current position, and then somehow calculate the angle and then use setRotation(calculatedAngle) to rotate it to the front of the ship. I've tried, but failed and now I'm here seeing if anyone has some tips. Any help is appreciated!

Below is the relevant code:
// Update space ship
void SpaceShip::update(float elapsedTime)
{
        firingElapsedTime += firingClock.getElapsedTime().asSeconds();
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        {
                if (firingElapsedTime >= firingSpeed)
                {
                        firingElapsedTime = 0.0f;
                        fire();
                }
        }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
    {
        getSprite().rotate(-250 *elapsedTime);
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
    {
        getSprite().rotate(250 * elapsedTime);
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    {
                sf::Vector2f direction(-sin(getSprite().getRotation() * 3.14159265f / 180.0f), -cos(getSprite().getRotation() * 3.14159265f / -180.0f));
                velocity.x -= acceleration * direction.x * elapsedTime;
                velocity.y += acceleration * direction.y * elapsedTime;
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    {
                sf::Vector2f direction(-sin(getSprite().getRotation() * 3.14159265f / 180.0f), -cos(getSprite().getRotation() * 3.14159265f / -180.0f));
                velocity.x += acceleration * direction.x * elapsedTime;
                velocity.y -= acceleration * direction.y * elapsedTime;
    }

    pos = this->getPosition();

        if(pos.x < getSprite().getLocalBounds().width/2 || pos.x > (Game::SCREEN_WIDTH - getSprite().getLocalBounds().width/2) || pos.y < getSprite     ().getLocalBounds().width/2 || pos.y > (Game::SCREEN_HEIGHT - getSprite().getLocalBounds().width/2))
        {
                velocity = -velocity;
        }
   
    getSprite().move(velocity);
        firingClock.restart();
}

// Fire bullets
void SpaceShip::fire()
{
        for (unsigned int i = 0; i < 20; i++)
        {
                if (bullet[i]->active == false)
                {
                        sf::Vector2f direction(-sin(getSprite().getRotation() * 3.14159265f / 180.0f), -cos(getSprite().getRotation() * 3.14159265f / -180.0f));
                        pos = this->getPosition();
                        bullet[i]->setPosition(pos.x, pos.y - 30);
                        bullet[i]->direction.x = direction.x;
                        bullet[i]->direction.y = direction.y;
                        bullet[i]->velocity.x = -bulletAcceleration * direction.x;
                        bullet[i]->velocity.y = bulletAcceleration * direction.y;
                        bullet[i]->active = true;
                        bullet[i]->elapsedTime = 0.0f;
                        std::stringstream objectName;
                        objectName << "Bullet" << i;
                        bullet[i]->objectName = objectName.str();
                        Game::getGameObjectManager().add(objectName.str(), bullet[i]);
                        break;
                }
        }
}
 
« Last Edit: May 23, 2013, 06:46:55 am by Eulavvalue »