2D particle system--bouncing effect?

This topic is 3367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have a simple 2D particle-based game using vectors where each particle is a pixel on the screen, and is affected by gravity. What do I need to do to make the particles bounce realistically off other particles or the boundaries of the game window? I already have all the vectors/collisions/piling up stuff coded, just need to know how to do bouncing. This is in Java, but a general explanation of how to do it would be fine too.

Share on other sites
Hi Jacob_,

particle systems usually do not let particles collide with each other and this also makes things a bit more complicated. If a particle should bounce off a line (e.g. the border of your window) you first have to calculate whether the particle actually reaches the line within the timestep T. If it does you first have to calculate:
1. T_c the time the collision occures (note: T_c must be smaller than T)
2. P_c the point of collision (2d point)
3. V_c the new velocity which is mirrored at the line (2d vector)
The new position is then P_c + (T - T_c)*V_c.

Does that help?

Cheers,
fng

Share on other sites
"V_c the new velocity which is mirrored at the line (2d vector)"

That's the part I'm having trouble with. What's the best way to find the new vector?

Edit: I could probably do it with a bunch of if/else if statements based on whether the x/y velocities of the current particle are positive or negative, but there has to be a better way.

Share on other sites
For vertical and horizontal lines it's easy. To collide with screen-edges, just do if(particle.x > right || particle.x < left) particle.speedX = -particle.speedX. It's a bit harder for spheres. Google has the answer: sphere 2d elastic collision. For example this and this seems to give the answer.

Share on other sites
Here the steps to calculate the velocity a general line that is not the window border:
Assuming D is a normal vector along the line that you are colliding with. Then you have to calculate the tangential and normal component V_t and V_n of your original velocity V_o. The tangential component goes parrallel to your line and the normal component is perpendicular to that.

We have:

V_t = (V_o . D) * D
V_n = V_o - V_t

(where A . B denotes the scalar product of two vectors A.x * B.x + A.y * B.y )

The new velocity is then: V_c = - V_o + 2 * V_t

Some nice tutorials concernind 2d collision detection and response can be found here: http://www.metanetsoftware.com/technique.html

Cheers,
fng

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• Forum Statistics

• Total Topics
634111
• Total Posts
3015563
×