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

Author Topic: Spawning bullets  (Read 11252 times)

0 Members and 1 Guest are viewing this topic.

emp1r3

  • Newbie
  • *
  • Posts: 1
    • View Profile
Spawning bullets
« on: May 02, 2012, 07:35:35 am »
Been messing around with SFML a little lately. Anyways, I want to know a simple way how to spawn a bullet and have it move across the screen without having to hold a key down. I know this is simple, it's late, and i'm sort of a noob; but here:

I have the sprite class made and the sprite spawning from the right location. When the "space" key is tapped I want the bullet to move across the screen to the right. The only way it will do this now if the space is held.
Inside while app.isopen loop:

 if (App.GetInput().IsKeyDown(sf::Key::Space)) {

        Bullets.Move(100 * .1, 0);

    }


This is flying right over my head, no idea why. Any help?   :-[

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Spawning bullets
« Reply #1 on: May 02, 2012, 10:45:57 am »
I've never done something like this and might be wrong, but for me the most practical way would be to create a bullet object upon firing which has speed and angle (or a vector) attributes (given it is 2D), and store it into some container. You simply go through the whole container each time and do the movement, if the bullet hits something the instance gets destroyed and removed from the container.

That is, of course, only if you need the bullet to be visible, otherwise you simply calculate the place of impact and show the animation/whatever there.

P.S.: I'm a Beginner, so idk.

Hindi

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Spawning bullets
« Reply #2 on: May 02, 2012, 02:23:30 pm »
I personnaly use a list where I store all my projectiles.
I do not have my code here but roughly :

player.fire : create the projectile and store it into the list
manageProjectile : move each projectile and check their positions for collision. I use this method in my game loop.

Do not forget to destroy every projectile that leaves your screen or meets an obstacle.

iHope

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Spawning bullets
« Reply #3 on: May 02, 2012, 05:49:08 pm »
I highly recommend to 1. Create a class for bullet. This class should draw/move etc. the bullet. 2. Create a vector what uses bullet class like
std::vector<Bullet> Bullets
I can give you the full code, but you should try to figure it out by yourself. I want to remember you about the fact that bullet shooting can done very many ways.
Sorry about my crap english ;D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Spawning bullets
« Reply #4 on: May 02, 2012, 08:53:00 pm »
I can give you the full code, but you should try to figure it out by yourself.

But a bit more information can still be helpfull. ;)

First of all I'd suggest you buy a book about C++ and read the section about classes.
It's just nearly impossible to work with stuff when you don't even fully understand the basics.
Second I'd advice you to use SFML 2rc!

Then for simplicity write your own class as suggested by others, that holds the sprite and physical properties like velocity and acceleration. To make it easier you can ignore acceleration and just use velocity.
As for creating/spawing new bullets you could write a function within your application class, which inistanciates a new bullet object and pushes it on to a std::vector.
Now you just have to call that function whenever you press a key and a new bullet gets created.
The moving part should be outside the KeyPressed-check statement and should move the bullet relative to the framerate or relative to a fixed timestep and make use of the bullet velocity property.

Since it's hard for beginners to understand the movement relative to the framerate, here's an example:
sf::Clock clock;
float dT = 1.f/60.f;
float speed = 5.f;

while(window.isOpen())
{
    // Event handling
    // Updating
    sprite.move(speed*dT, speed*dT);
    // Draw

   dT = clock.restart().asSeconds();
}

For fixed timesteps I can refere you to this article.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hindi

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Spawning bullets
« Reply #5 on: May 02, 2012, 09:29:19 pm »
Quote
But a bit more information can still be helpfull.

I don't even think about someone using C++ without classes and creating a bullet class is obvious to me. Now that I read your message and his message again, I think that you're right and we should have given more information about that.

Quote
pushes it on to a std::vector

I read somewhere that std::list are more efficient when you need to randomly delete objects that are in the list. Isn't it a better choice in this case ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Spawning bullets
« Reply #6 on: May 02, 2012, 10:00:11 pm »
I read somewhere that std::list are more efficient when you need to randomly delete objects that are in the list. Isn't it a better choice in this case ?

It always depends how he's gonna use it but a list doesn't sound that bad. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Spawning bullets
« Reply #7 on: May 03, 2012, 07:16:43 pm »
I read somewhere that std::list are more efficient when you need to randomly delete objects that are in the list. Isn't it a better choice in this case ?
Normally, you should take std::vector. In most situations where a linear sequence is needed, it is the fastest container. If you require special cases like not invalidating iterators, many random deletions/insertions or similar, consider other containers. But usually, the differences only pay out for really big sequences (100 elements is tiny).

Don't forget that you can randomly remove elements in the vector with O(1), if the order is not relevant (and it often isn't). Swap the element to remove with back() and call pop_back(). And generally, prefer std::remove / std::remove_if() over an iterator loop calling erase().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: