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

Author Topic: Bullet not firing  (Read 2362 times)

0 Members and 1 Guest are viewing this topic.

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Bullet not firing
« on: April 24, 2014, 07:30:28 am »
So I have a player class that just shoots a bullet and for some reason it is not shooting the bullet the way I want it to. The problem is trying to get the bullet to shoot from the player's location in the direction upwards. So, player would have this:


void Player::shoot(Bullet &bullet)
{
    bullet.isFired(true);
}

 

All the shoot function does is simply tell the bullet object that it has been fired and should now update, thus:

void Bullet::update(sf::Vector2f position, sf::Vector2f speed)
{
    if (wasFired == true)
    {
        setPosition(position.x + speed.x, position.y + speed.y);
    }
    else if (wasFired == false)
    {
        wasFired = false;
    }
}
 

The code here is only raw and written on the fly just to get a base up and running. And in the initialisation thereof:

void Game::update()
{
    bullet.update(tank.getPosition(), sf::Vector2f(0, -10));
}
 

When I run the game, the bullet moves by 10 units upwards, but it does not update the position thereafter; however it does update the position according to the player's x-axis.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10838
    • View Profile
    • development blog
    • Email
AW: Bullet not firing
« Reply #1 on: April 24, 2014, 08:35:54 am »
There's no need for if(wasFired==true), wasFired is already a boolean, so you can just write if(wasFired).
Check for else if(WASFIRED==false), os even more useless, a boolean is either true or false, so if(wasFired) is false, then it's enough to just use else.
Setting wasFired to false won't help anything either, given the else clause already guarantees that wasFired is false. ;)

As for your problem, you need to provide a complete and minimal example, with the given code it's not possible to tell what gets called how.
You also might simply want to step through youd code and see what goes wrong where.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Bullet not firing
« Reply #2 on: April 24, 2014, 08:48:20 am »
Your bullet position will always be tank.getPosition() + (0, -10), which I guess is a fixed position. If you want your bullet to move, tank.getPosition() should only be the initial position, and then you should have an update function similar to this:

bullet.move(speed * elapsedTime);
Laurent Gomila - SFML developer

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Bullet not firing
« Reply #3 on: April 24, 2014, 09:40:43 pm »
Thanks for your responses. This is what I've amended now:

]
void Bullet::update(sf::Vector2f position, sf::Vector2f speed, sf::Time elapsedTime)
{
    if (wasFired)
    {
        move(speed * elapsedTime.asSeconds());
    }
    else
    {
        wasFired = false;
    }
}
 

I've inserted a new parameter to take into account the elapsed time, which is initialised here:

void Game::update()
{
    alien.activateAI(true);
    bullet.update(tank.getPosition(), sf::Vector2f(0, -10), pClock.getElapsedTime());
}
 

Suffice it to say the bullet fires; albeit it does not fire from the initial position of the tank. The problem is that it only fires from the bullet's own initial position. I have been able to update the bullet's position relative to the tank's position but then I can't shoot a bullet as intended. It seems I can't do one or the other for now. Is there any way I can resolve this issue? My first solution was to create an updatePosition(@param) function which simply updates the position of the bullet according to where the tank is. Are there better solutions?

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Re: Bullet not firing
« Reply #4 on: April 24, 2014, 10:05:36 pm »
]
void Bullet::update(sf::Vector2f position, sf::Vector2f speed, sf::Time elapsedTime)
{
    if (wasFired)
    {
        move(speed * elapsedTime.asSeconds());
    }
    else
    {
        wasFired = false;
    }
}
 
wasFired is false already. You just killed variable!

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Bullet not firing
« Reply #5 on: April 24, 2014, 10:52:08 pm »
Sorry lol. I've deleted the else statement:- repose en paix!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Bullet not firing
« Reply #6 on: April 24, 2014, 11:41:09 pm »
Quote
void Player::shoot(Bullet &bullet)
{
    bullet.isFired(true);
}
This is where you should set the initial bullet position to the position of the tank.
Laurent Gomila - SFML developer

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Bullet not firing
« Reply #7 on: April 25, 2014, 12:32:59 am »
It makes sense now! Because the update function was in the while(windowisOpen) loop it was constantly initialising the bullet's position to that of the player's; but we only had to initialise the position if the bullet was shot. The only thing in this case that needed to be updated was the position. Thanks for your help!

 

anything