SFML community forums

Help => General => Topic started by: Pacman on November 23, 2014, 01:21:26 am

Title: Shooting projectiles Straight **Advice needed***
Post by: Pacman on November 23, 2014, 01:21:26 am
Hey SFML coders !
How do i get a sprite to be fired as projectile and move straight ahead .

Please explain Precise and simple ... your knowledge will be appreciated
Here is the code i have.

 
/////////////////////////////
   ///Create the bullet
////////////////////////////
void Bullet::createBullet(sf::Texture &bulletTexture, sf::Sprite &bulletImage, sf::Sprite &shipImage)
{
  if(!bulletTexture.loadFromFile("Images/missile.png"))///creates the bullet
        cout << "ERROR:bullet could not load" << endl;

    bulletImage.setTexture(bulletTexture);///
    sf::Vector2f shipLocation = shipImage.getPosition(); ///Gets the location of the ship

    float locationX = shipLocation.x - 83;///sets the X location of the ship places the bullet of  it
    float locationY= shipLocation.y +4 ;///sets the Y loction of the ship

    bulletImage.setPosition(locationX, locationY);
}
 

////////////////////////
///Move the bullet
////////////////////////
void Bullet::moveBullet(sf::Sprite &bulletImage, sf::Sprite &shipImage)
{
    float moveX = shipImage.getPosition().x;
    bulletImage.move(-3,0);
}

 

(http://C:\Users\pacman\Pictures\demo.png)
Title: Re: Shooting projectiles Straight **Advice needed***
Post by: BaneTrapper on November 23, 2014, 03:35:30 am
Hey SFML coders !
How do i get a sprite to be fired as projectile and move straight ahead .
Now honestly.
That is very poorly described what you want, how about you put more information of what exactly you want to have, and then i help you.
Stuff like:
In my game i want to shoot a gun, and projectiles to travel in direction of mouse. How do i do this?

To also answer your question:
angle_in_degrees is just angle in degrees...
distance_to_move is value you move the spite in given angle.
Code: [Select]
posX += cos(angle_in_degrees)*distance_to_move;
posY += sin(angle_in_degrees)*distance_to_move;