Leonids
Leonids copied to clipboard
Change particles gravity dynamically
Hi,
I want my particles to change their gravity depending on the device's position. I already have a sensor listener but I can't find a way of changing the gravity (acceleration) dynamically. Is there an easy way to do this?
Thanks
In principle that should be doable by making a custom Modifier.
The main problem I see is that acceleration is meant to be constant because the position of the particles is calculated from time 0 and not incrementally, to avoid rounding problems. This could be a issue, since what you want requires incremental modification.
I suggest you fork the project, then change the update method of the Particle and the ParticleSystem to use incremental timestamps, and then write a custom modifier that updates the acceleration of the particles, that should work.
I may add a parameter to do incremental modification in the future, your idea is interesting and it can only be done with that.
Hope this helps.
Just changing the acceleration (for example in the opposite direction) is not enough. I tried changing the particle system's acceleration and in the Particle's update method I updated the initial position to the current one, and used the delta millis instead of the realMiliseconds variable. In this way the initial point is in fact the last point. The problem is that the direction changes are too slow. I think it's because of the small time value that I always use.
public boolean update(long miliseconds) { long realMiliseconds = miliseconds - mStartingMilisecond; if (realMiliseconds > mTimeToLive) { return false; }
long currentStepMiliseconds = miliseconds - mLastStepMilisecond;
mCurrentX = mInitialX + mSpeedX * currentStepMiliseconds + mAccelerationX
* currentStepMiliseconds * currentStepMiliseconds;
mCurrentY = mInitialY + mSpeedY * currentStepMiliseconds + mAccelerationY
* currentStepMiliseconds * currentStepMiliseconds;
mRotation = mInitialRotation + mRotationSpeed * currentStepMiliseconds / 1000;
for (int i = 0; i < mModifiers.size(); i++) {
mModifiers.get(i).apply(this, currentStepMiliseconds);
}
mInitialX = mCurrentX;
mInitialY = mCurrentY;
mLastStepMilisecond = miliseconds;
return true;
}
If the problem is that the direction changes are too slow, you just need to apply a multiplier to the value. Also, that was the reason why I was using full timers, to avoid rounding.
Other idea is to just move on an use double for mCurrentX and mCurrentY, should not be needed, since the precision of float is enough (in theory), but it is worth a try.
Other than that, it looks good to me.