How change the life time of existing particles in CCParticleSystemQuad? - c++

I have a CCParticleSystemQuad and it generated many particles with setLifeVaule(40).
Now, in the runtime, I want to change the life value of the existing particles in fewer value such as 0.1.
But setLife() function does not affect existing particles.how can i change the life Time of them?

Related

Making something happen when increasing a variable

How do I make it so that every time a variable increases I can do something like play a particle system
I have the variables coins1 through coins7 and every time one of them updates I need a trigger to happen

Cleaning object pool based on execution time

Problem
My current project implements a pool of objects to avoid constant memory allocation and de-allocation for speed purposes, the object pool is a std::vector<object> and I would like to implement a form of garbage collection to reduce memory usage and increase performance. Every loop the program iterates over the entire vector and if the object is active, executes the update function, this means that if my vector is full of inactive objects, I will be wasting a lot of time iterating over them and also memory storing them. I cannot clean the vector every frame as this would crush performance.
Current attempt
My current attempt at implementing this has been to measure the update time, and use a pre-defined function to determine whether or not we are spending too much time on the update, for the amount of objects currently active, if we are, I clean the vector once to allow the speed to return to normal.
#include <chrono>
void updateObjects()
{
auto begin = std::chrono::high_resolution_clock::now();
//update all objects
for(auto o : objectVec)
{
//only update active objects
if(o.m_alive)
{
o.update();
}
}
//end time of update
auto end = std::chrono::high_resolution_clock::now();
//calculate time taken vs estimated time
auto elapsed = (end-begin).count();
//Estimate is based on performance testing
long estimate = 25*m_particleCount+650000;
//If we have no active objects,
//but are wasting memory on storing them, we clean up
//If the update takes longer than it should, we clean up
if(((objectCount <= 0) && (objectVec.size() > 0)) || (elapsed > estimate))
{
cleanVec(); //remove inactive objects
}
}
This solution works well on my pc, however I am having issues with it on other computers as the time taken for the update to complete varies due to different CPU speeds, and my pre-defined function then doesn't work as it is based off of incorrect data. I am wondering if there is another measurement I can use for this calculation? Is there a way I can measure the pure amount of instructions executed, as this would be the same across computers, some would simply execute them faster? Any other suggestions are welcome, thank you!
Edit
The object pool can be as large as 100,000 objects, typical usage will range from 3000 to the maximum.
My function for cleaning is:
objectVec.erase(std::remove_if(objectVec.begin(),
objectVec.end(),
[](const object& o) {return !(o->m_alive);}),
objectVec.end());
Typically, an object pool uses aging to determine when to expel an individual pool object.
One way to do that is to keep an iteration counter within your update loop. This counter would be incremented every loop. Each object could keep a "last active" time (the loop count the last time the object was active). Inactive objects would be moved to the end of the array, and when old enough would be expelled (destroyed). A separate index of the last active object would be kept, so looping for updates could stop when this was encountered.
If it isn't possible to store an active time in an object, you can still move the inactive objects to the end of the active list, and if there are too many inactive objects pare the list down some.
You should specify how expensive your cleanup is, and how large your pool is, they affect how cleaning up should be implemented.
To make it clear, the pool is effectively an allocator of one type of object and performance gains are completely independent of how individual objects are cleaned up.
I cannot clean the vector every frame as this would crush performance.
If the performance of inactive objects' cleanup dominates, there is nothing you can do in your pool algorithm.
Therefore I assume this is due to std::vector semantics where removing inactive objects involves relocation of other objects. It is then logical to ask do you really need std::vector's properties?
Do you need your objects to be contiguous in memory?
Do you need O(1) random access?
Do you loop over the pool constantly?
Otherwise, is the pool small? As long as it is small std::vector is fine.
If the answer is no, then just use stuff like std::deque and std::list, where removal is O(1) and you can cleanup on every frame.
Otherwise garbage collection of your pool can be done as
keeping a counter of frames since last updated for each object, remove if counter is over a threshold
keeping a counter of total inactive objects, clean pool if percentage of active objects is over a threshold

Faster to create map of pointers or if statement

I'm creating a game engine using C++ and SFML. I have a class called character that will be the base for entities within the game. The physics class is also going to handle character movement.
My question is, is it faster to create a vector of pointers to the characters that move in a frame. Then, whenever a function moves a character it places it inside that vector. After the physics class is done handling the vector it gets cleared?
Or is it faster to have a bool variable that gets set to true whenever a function moves a character and then have an if statement inside my physics class that tests every character for movement?
EDIT:
Ok i've gone with a different approach where a function inside the Physics class is responsible for dealing with character movement. Immediately upon movement, it tests for collision detection. If collision happens it stops the movement in that direction.
Thanks for your help guys
Compared to all the other stuff that is going on in your program (physics, graphics), this will not make a difference. Use the method that makes programming easier because you will not notice a runtime difference at all.
If the total number of characters is relatively small, then you won't notice the difference between the approaches.
Else (the number of characters is large), if most of characters move during a frame, then the approach with flag inside a character seems more appropriate, because even with vector of moved characters, you'll traverse all of them and besides that you get additional overhead of maintaining the vector.
Else (the number of characters is large, but only few of them move during a frame), it may be better to use vector because it can save you time by not traversing characters which didn't move.
What is a small or large number, depends on your application. You should test under which conditions you get better performance using either of approaches.
This would be the right time to quote Hoare, but I'll abstain. Generally, however, you should profile before you optimize (if, and only if, the time budget is not enough on the minimum spec hardware -- if your game runs at 60fps on the target hardware you will do nothing whatsoever).
It is much more likely that the actual physics calculations will be the limiting factor, not doing the "is this unit moving?" check. Also, it is much more likely that submitting draw calls will bite you rather than checking a few hundred or so units.
As an "obvious" thing, it appears to be faster to hold a vector of object pointers and only process the units that are actually moving. However, the "obvious" is not always correct. Iterating linearly over a greater number of elements can very well be faster than jumping around (due to cache). Again, if this part of your game is identified as the bottleneck (very unlikely) then you will have to measure which is better.

Calculation new velocities between objects (AABB)

lately I have been trying to create a 2D platformer engine in C++ with Direct2D. The problem I am currently having is getting objects that are resting against each other to interact correctly after accelerations like gravity have been applied to them.
Right now I can detect collisions and respond to them correctly (I think) and when objects collide they remember what other objects they're resting against so objects can be pushed by other objects (note that there is no bounce in any collisions so when objects collide they are guaranteed to become resting until something else happens). Every time the simulation advances, the acceleration for objects is applied to their velocities (for example vx += ax * t, where t is time elapsed since last advancement).
After these accelerations are applied, I want to check if any objects that are resting against each other are moving at different speeds than their counterparts (as different objects can have different accelerations) and depending on that difference either unlink the two objects so they are no longer resting, or even out their velocities so they are moving at the same speed once again. I am having trouble creating an algorithm that can do this across many resting objects.
Here's a diagram to help explain my problem
http://i.imgur.com/cYYsWdE.png

Fill array objects with the same data or use a pointer?

I'm developing a game using c++ and I don't know what's the better approach to my problem.
I have an array with gameObject objects, each one has an array with frames of animation that I loop through to show the animation in the game.
The animations are time based and I have to tell the objects how much time has passed since the last frame was displayed (deltaTime) to calculate what frame I will display in this iteration.
Everything ok until here.
Is it ok to feed the deltaTime to the objects with a setter method? Or would be better if I had a pointer in each object to a global variable? Or is there another even better approach here regarding performance and organization? Keep in mind that there'll be hundreds of objects.
Thank you for your time :)
In general, avoid global variables. Just loop over the objects, passing the deltaTime to each:
for (auto &gameObject : gameObjects)
{
gameObject.Update(deltaTime);
}
Storing the time within each object isn't recommended, since anything you think you might need it for "in the future", you should just do within Update.