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

Author Topic: [Solved] How to assign directions to bullets in vector when they are created  (Read 4501 times)

0 Members and 1 Guest are viewing this topic.

argh

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • my blog
    • Email
I have a vector, where i add bullets if a key is pressed. I would like to assign a direction (left/right) - or + upon creation. I can't figure it out. I thought about creating another vector for the bullets that i want going left, but it seems not the smartest solution.  :-\
       
this is how a new bullet is been created. IS it possible to add something like (-1*)so later when i call move on bullets the negative ones will fly left?
             
       sf::Sprite *nshot = new sf::Sprite;
        nshot->setPosition(player.getPosition());
        nshot->setTexture(shoot);
        bullets.push_back(*nshot);
 

this is how i move them atm. (direction_left is true when leftkey is pressed), this is no good, because when i press right they change their direction.
 for (auto i = bullets.begin(); i != bullets.end(); i++)
    {
      //according to direction fly left or right
      if (direction_left)
       i->move(-1000.f * bulletSpeed.asSeconds(), 0);
      else
       i->move(1000.f * bulletSpeed.asSeconds(), 0);
    }
« Last Edit: October 08, 2015, 09:28:51 pm by argh »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
How to assign directions to bullets in vector when they are created
« Reply #1 on: October 08, 2015, 11:58:52 am »
Create a bullet class and have a property orientation or similar.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

argh

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • my blog
    • Email
Re: How to assign directions to bullets in vector when they are created
« Reply #2 on: October 08, 2015, 03:19:31 pm »
Create a bullet class and have a property orientation or similar.

I thought about it, but I wanted to find a simpler solution.
Another possibility is maybe to create 2 vectors(one with left direction another with right)?

New class it is. Ty :)

Satus

  • Guest
Re: How to assign directions to bullets in vector when they are created
« Reply #3 on: October 08, 2015, 04:56:40 pm »
Creating a bullet class that encapsulates all bullet logic behind constructor and/or few simple public method is way simpler than creating multiply vectors, that code would be unreadable for other programmers or yourself if you decide to take a break from your project for a week.  :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to assign directions to bullets in vector when they are created
« Reply #4 on: October 08, 2015, 07:55:12 pm »
I thought about it, but I wanted to find a simpler solution.
Another possibility is maybe to create 2 vectors(one with left direction another with right)?
How is that simpler? Instead of maintaining one container, you have to keep two in sync and do everything twice. When a function needs access to multiple attributes of a bullet, you have to pass them as separate arguments instead of one combined object.

Object-oriented programming is good. Use a class for your object, and please don't inherit it from an SFML class...

By the way:
      sf::Sprite *nshot = new sf::Sprite;
        nshot->setPosition(player.getPosition());
        nshot->setTexture(shoot);
        bullets.push_back(*nshot);
This code really hurts my eyes. You do not only have a memory leak, but seem to use new for no reason. Avoid manual memory management in your C++ code, it's not necessary -- see also my article about RAII.
« Last Edit: October 08, 2015, 07:58:19 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

argh

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • my blog
    • Email
Re: How to assign directions to bullets in vector when they are created
« Reply #5 on: October 08, 2015, 08:49:06 pm »
How is that simpler? Instead of maintaining one container, you have to keep two in sync and do everything twice. When a function needs access to multiple attributes of a bullet, you have to pass them as separate arguments instead of one combined object.
No it's not. I was a bit unclear - i just thought theoretically what other option do I have. Making 2 vectors would add a lot of code to my project - which does not make sense.
Object-oriented programming is good. Use a class for your object, and please don't inherit it from an SFML class...
so you don't recommend doing this ->
class Bullet : public sf::Drawable {}
How else can I make it drawable?
By the way:
(click to show/hide)
This code really hurts my eyes. You do not only have a memory leak, but seem to use new for no reason. Avoid manual memory management in your C++ code, it's not necessary -- see also my article about RAII.
Ty for the tip.I will look into your article.

edit: my create/move bullet function now looks like this:
(click to show/hide)
« Last Edit: October 08, 2015, 08:56:27 pm by argh »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: How to assign directions to bullets in vector when they are created
« Reply #6 on: October 08, 2015, 09:15:27 pm »
Just a suggestion. I'd probably write that bit more like this:
void createBullet()
        {
                const auto reload = time_last_shot.getElapsedTime();
                const auto bulletSpeed = bulletSpeedClock.getElapsedTime();
                const auto reload_delay = sf::seconds(0.8f);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && reload > reload_delay) {
                                bullets.push_back({shoot, directionLeft, 1000, 500, player.getPosition()});
                                time_last_shot.restart();
                }
                for (auto& bullet : bullets) {
                        bullet.Update(bulletSpeed); // if needed as seconds, convert inside Update()
                }
                bullets.erase(std::remove_if(bullets.begin(), bullets.end(), [](const Bullet& b) { return !b.IsAlive(); }), bullets.end());
                bulletSpeedClock.restart();
        }
Also, instead of removing bullets that are not alive you could just use std::partition to move all the live ones to the front and then in your update loop do "if (!bullet.IsAlive) break;" and when you need a new bullet reuse dead ones (if any) or push a new one if no old ones are available.
« Last Edit: October 08, 2015, 09:53:15 pm by Jesper Juhl »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to assign directions to bullets in vector when they are created
« Reply #7 on: October 08, 2015, 09:18:11 pm »
so you don't recommend doing this ->
class Bullet : public sf::Drawable {}
How else can I make it drawable?
That's perfectly okay, classes like sf::Transformable, sf::Drawable and sf::NonCopyable are meant to be used as base classes. But I see a lot of people inheriting sf::Sprite etc. to add their own functionality, while that was never a design intended by the authors.

my create/move bullet function now looks like this
Looks good, cool that you're using the erase-remove-idiom!

I don't think std::shared_ptr is necessary here -- why not simply std::vector<Bullet>? Some copies won't hurt, and it makes a lot easier. If you actually need pointers, you should resort to std::unique_ptr, as you're not sharing ownership.

Furthermore, you can simplify
        for (auto i = bullets.begin(); i != bullets.end(); i++)
        {
            (*i)->Update(bulletSpeed.asSeconds());
        }
as follows:
for (Bullet& bullet : bullets) // assumes std::vector<Bullet>
   bullet.update(bulletSpeed); // misleading name, it's not a speed
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

argh

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • my blog
    • Email
Re: How to assign directions to bullets in vector when they are created
« Reply #8 on: October 08, 2015, 09:28:38 pm »
ty.  a lot of great answers - more then i was hoping for.  8)

 

anything