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

Author Topic: Implementing the Projectiles in a Sidescroller Game  (Read 3534 times)

0 Members and 1 Guest are viewing this topic.

Sinclaire

  • Newbie
  • *
  • Posts: 2
    • View Profile
Implementing the Projectiles in a Sidescroller Game
« on: January 23, 2016, 11:36:44 pm »
Hey!

I am currently trying to wrap my head around creating a small Sidescrolling Shooter.
What I can't figure out is what code to use for the shots/projectiles. The goal is obviously to despawn a projectile once it hit a target, so I figured I have to get the bounding boxes of every enemy on the screen and every projectile and test the collision through intersects.
The next step is to despawn the projectile.
So overall, I thought of using a list or vector to contain all of the possibly shot projectiles and maybe allocating additional memory if somehow more projectiles than expected are on the screen at the same time. But despawning the projectiles really gives me trouble, since with a vect I would constantly be erasing elements right in the middle of it, resulting in horrible performance drops (I suppose).
I've been trying to find source codes of other sidescroller games to compare the ideas but tough luck.  :P ???

So yeah, if there happens to be a super-efficient way of implementing shots, please tell me  :)

Resethel

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Implementing the Projectiles in a Sidescroller Game
« Reply #1 on: January 24, 2016, 09:06:59 am »
Unless you have thousands of them on screen, and your code really isn't optimized, I don't think so but the best way to find out is to try ;), coding also is about experimenting!

If you intend to erase a lot of elements at random places in your container, regularly, you may want to use a std::list which is bit more suited for this kind of use, but less efficient .
« Last Edit: January 25, 2016, 08:09:11 pm by Resethel »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Implementing the Projectiles in a Sidescroller Game
« Reply #2 on: January 24, 2016, 08:32:55 pm »
I thought of using a list or vector
[...]
despawning the projectiles really gives me trouble, since with a vect I would constantly be erasing elements right in the middle of it
You kind of answered your own question here. You gave yourself two options and then realised that one of them isn't suited.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: Implementing the Projectiles in a Sidescroller Game
« Reply #3 on: January 24, 2016, 10:36:39 pm »
I thought of using a list or vector to contain all of the possibly shot projectiles

Use an std::vector, not an std::list. The std::list class is usually implemented using linked lists, which will have a terrible effect on performance.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Implementing the Projectiles in a Sidescroller Game
« Reply #4 on: January 24, 2016, 10:56:12 pm »
I would actually agree with GraphicsWhale, even though I gave the otherwise impression. Vectors should be working fine here since your projectile object should be small and light enough to be able to move them around quickly anyway.
Note that your projectiles should not contain the actual textures that they use. It doesn't even need to contain the sprites.

Try it, and then, if you actually have performance issues due to vector re-ordering, consider using a list but only after you've confirmed that you couldn't have designed the projectile to be more simple.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Implementing the Projectiles in a Sidescroller Game
« Reply #5 on: January 25, 2016, 12:22:40 am »
if the order of elements of a vector isn't important you can swap the element you want to delete with the last element, then pop_back(), ensuring you're not erasing from the middle of a vector

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Implementing the Projectiles in a Sidescroller Game
« Reply #6 on: January 25, 2016, 10:04:11 am »
GraphicsWhale and fallahn make good points. Use std::vector, it's the best performing sequential container in 95% of the cases. Only use a different container if you have a good reason to.

And the swap-and-pop_back idiom mentioned by fallahn allows super fast removal in O(1). It may also be appropriate to use std::remove_if() based on a condition (projectile exploded), that's also quite efficient and preserves the order.

It's good to have the background knowledge, but don't spend too much on optimizing non-existing performance problems in little projects -- in Java it's not even possible to allocate dynamic objects sequentially in memory, and there are entire games written in it.
« Last Edit: January 25, 2016, 10:07:14 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ramaskrik

  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Implementing the Projectiles in a Sidescroller Game
« Reply #7 on: February 07, 2016, 12:08:04 pm »
Coming to the discussion a bit late, but I still think this is worth it, as the situation described is a perfect candidate for Object Pool pattern. (Ofc,  only after it is a proved bottleneck of the game )