Topic: OO stuff and frame drop

Hello! i'm having multiple problems with on my shooter project,
have a look at this Video, it's a gameplay vid of an arcade game that illustrate some of the problems i'm having.

And i apologise for my english / clarity of what i'm trying to explain. i have very hard times doing this.

I'm using the same script than Here (+  the isActive(object) for optimisation that is not present here) for all objects :

- 1 Player projectile
- 1 Ennemies
- 1 Ennemy projectile

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Problem 1 : Brutal frame drop when player is shooting fast
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PunBB bbcode test

see the player shots ? i believe all single projectiles are OO items spawned every frames, right ?

If i'm trying to do this in maratis, the frame rate drop dramatically

my method :

one hitbox (just a invisible cube), and one graphic item (the projectile)

I'm joining the collision cube with the projectile graphic with :

setParent
OR
follow behavior

Both method works and the result is the same for both methods (same frame drop, no differences)

Note that :

spawning 1 bullet every frame works ok
spawning more than 1 bullet every frame result in hyper frame drop

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Problem 2 : Frame drop when two ennemies of the same type are shooting together
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

PunBB bbcode test

See the "tank" who's shooting green projectiles ?

if i'm spawning 1 ennemy like that, it will run ok, if i spawn 2 at the same time,
when it starts shooting even if its a very low amount of projectiles (about 10 for each)
frame drop occurs. I believe it's a similiar problem than in problem 1.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Problem 3 : Progressive frame drop when using "for i=1, #projectileList do" in the ennemy script to check whenever there is a collision between the player projectile and the ennemy
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

This one starts to freeze after some time. i would say after 20~ ennemies are spawned.

I had make sure everything in the ennemy script and bullet script
(objects, values, and extra stuff) got deactivated when a collision occurs.

When removing "for i=1, #projectileList do" from the ennemy script, frame drop is gone, but then i cant make collision between player bullet and ennemy hmm


So right spawning multiple projectiles very fast makes the game freeze and testing collision between OO projectiles/ennemies make the game freeze hmm

I don't know what to do, any ideas ?

Re: OO stuff and frame drop

If you want the spawning to behave more like a particle system, you probably want to use the getClone function for bullets, and also give each clone a lifetime that it can be active.

Take you can take cues from the settings of the particle system in Blender. I just made a post called "Code snippets." The first post is a snippet that allows you to clone an object on another object's position. My objects are 3d with no velocity, but if your objects are just flat planes then it should move even faster.

Last edited by Tutorial Doctor (2014-01-30 01:10:01)

Re: OO stuff and frame drop

All my objects are already clones (here's a video of the current project)

It's just that spawning them faster, like in the video in my first post will make the frame rate goes crazy.
also,  testing collision between clones is making the game freeze after a while.

Re: OO stuff and frame drop

I want to help You, but unfortunately I can not understand.. Google translate is not perfect. smile

Re: OO stuff and frame drop

Hi Vegas,
your video looks quite interesting, waiting to see more !

About the frame drop, there is some concept that need to be understood :

1 : dynamic object creation is not fast, for multiple reasons :
  - memory allocation : each cloning allocate new memory, fast games usually pre-allocate memory.
  - when using physics, the new object need to be also allocated to the physics world
  - when using sound, the source need to be also allocated to the sound context

2 : collision need to be optimized for your need

There is two things I suggest :

1 : pre-allocate the projectiles :
  - clone a list of each object at the beginning of the scene, think about how much of each projectile
    would be used at max at the same time (let's say 32 of each)
  - when you shot a projectile, use one of the object from the list and turn with this 32 objects

2 : if it's still slow, try to not use physics where it's not necessary :
 
for example, use a simpler collision test for the projectiles :
(if the distance between object and projectile is less than a certain distance, there is a collision)

distance = length(projectilePos - objectPos)

if distance < (projectileRadius + objectRadius) then
  --do something
end

Re: OO stuff and frame drop

Thanks you & thank you, everything's more clearer now smile

And hum, I feel a bit dumb to ask again, as you and ant0n practically wrote 90% of my project's scripts but, there's alot of situation where i wanted to use this distance method instead of physics collisions, how can i get the two objects radius like in you small snippet ?

(projectileRadius + objectRadius)

could you make a little example when you have time ?

Re: OO stuff and frame drop

The radius is just a value you have to choose.
Sphere to Sphere collision works like that :
https://community.aldebaran-robotics.com/doc/1-14/_images/motion_collision_sphere_2.png
when D is inferior than r1+r2 it means the two sphere are colliding.

Let's say the object Enemy1 has a radius of 10 units and that the object Bullet1 a radius of 2 :

Enemy1 = getObject("Enemy1")
Bullet1 = getObject("Bullet1")

Enemy1_radius = 10
Bullet1_radius = 2

function onSceneUpdate()

    Enemy1_pos = getTransformedPosition(Enemy1)
    Bullet1_pos = getTransformedPosition(Bullet1)

    distance = length(Enemy1_pos - Bullet1_pos)

    if distance < (Enemy1_radius + Bullet1_radius) then -- collision !
        -- do something
    end 

end

(the radius don't need to be the exact same size of the mesh, in shoot game it's usually smaller to reduce difficulty, choose the best value for the gameplay)

Re: OO stuff and frame drop

In fact, i would never have guessed how it works, Thank you sir wink
now let's rock cool

Re: OO stuff and frame drop

To add to the point of pre-allocating memory; are there ways for users to get more control over the heap? Or to be able to create our own?

Last edited by sunnystormy (2014-01-30 16:41:24)

Re: OO stuff and frame drop

To add to the point of pre-allocating memory; are there ways for users to get more control over the heap? Or to be able to create our own?

It's a natural bottleneck, specially for fast game like this shooting game where hundred of objects are created in a single frame. It's not just the memory allocation, but the fact that a new object need to be added to the scene description, some object data need to be initialized (matrices, references, parenting).

Also, it's possible that the slow down in this example is more related to the physics than a pure memory allocation bottleneck, in an indirect way, like above, because the collision object need to be added to the physics scene, the scene octree probably need to be updated because of that, etc.

Re: OO stuff and frame drop

Cool. Thanks for explaining. Good luck with your game, Vegas! smile

Re: OO stuff and frame drop

sunnystormy wrote:

Good luck with your game, Vegas! smile

hehe thanks wink

I had some trouble setting-up everything but now it's all good, i can spawn projectiles
just like in the gameplay video on the first post without any frame drop cool

Also, anael i'm gonna add your explanations about clones on the wiki if you don't mind,
that's some important infos imo

Cheers !

Re: OO stuff and frame drop

Hi Vegas,
yes, thank you for adding the technique to the wiki.

For your game, was the pre-allocation of clone enough
or did you also use the simpler collision detection describe above ?

Re: OO stuff and frame drop

Hi, i went the safe way and directly used both techniques wink