Rembrandt 0 Report post Posted March 17, 2016 Hi guys, I'm no math genius so here is my question. How do i create a particle explosion where each particle moves to a random direction with the same speed? There is probably a simple solution but I can't figure it out. Thanks! So like in pseudo-code: for(100){ particle.xVelocity = Random particle.yVelocity = Random particles.push(particle) // This array now contains particles that move in a random direction with the same speed } Share this post Link to post Share on other sites

AzraelTycka 56 Report post Posted March 17, 2016 Hello, you could do something like this for example. Your start is the center, radius is velocity magnitude (speed) and dx and dy are your velocities, randomize angle from 0 to 2 * Pi. dx = radius * cos(angle) dy = radius * sin(angle) Or mathematically the exactly same solution with pythagorean theorem: velocity ^ 2 = vx ^2 + vy ^ 2 Velocity is your constant which you set (same for all particles if you want them to move with the same rate), randomly set vs or vy and calculate the other one. Leading you to velocity = constant vx = some RANDOM number (velocity ^ 2 - vx ^ 2) ^ 0.5 = |vy| (absolute value) randomly pick plus or minus as a sign for vy and you are good to go. And let me know if it works I don't have enough time to make sure I didn't miss something right now :-). 1 Kyros2000GameDev reacted to this Share this post Link to post Share on other sites

Rembrandt 0 Report post Posted March 17, 2016 Thanks Azrael! Your first solution with PI works like a charm. But now i have another problem, i would like the particles to travel in a certain region (a slice from a circle). For example a region (0 degrees to 210 degrees). I hope you can help me out, thanks ^^ Share this post Link to post Share on other sites

AzraelTycka 56 Report post Posted March 17, 2016 Hello, if you are going with the cos and sin equations (those two equations are actually equations of circle) then just narrow the angle. Instead of from 0 to 2 * Pi you can use a different angle interval to randomize from. You can even use 3 * Pi, there is a period of 2 * Pi so anything above will reproduce the values periodically, but you can use that periodicity to your advantage. The equation graphically is a circle with radius and angle which starts at the righter most part of the circle (angle goes anticlockwise!), so if you want your particles to aim directly in the top direction you can just either set your velocityX = 0 and velocity Y = someNumber or use the circle equations and set the angle to Pi / 2 (that's 90°): x = radius * cos(Pi / 2) = 0, y = radius * sin(Pi / 2) = radius * 1 = radius. So if you want any other direction just use different angles. From your picture it seems that you want these extremes (first one is NEGATIVE!): < - Pi / 2; 3 * Pi / 2 > (< and > means that both values are INCLUDED!). If you are using standard web interpretation where point [0, 0] is at the top left corner of your window (in math it's usually bottom left in high school) then you will need to do a slight adjustments because your circle is reversed (y values increase towards bottom of your screen not towards top as you know math) I will leave these to you it's fun ;-). Don't forget you can even adjust equations with different sign ;-). You can simply google circle equations and you will get a lot of example with pictures. Anyway angle needs to be in radians, so to make that easier for you 0 degrees = 0 radians, 360 degrees = 2 * Pi. That should make your calculations easier, either that or just use google, it will calculate your values for you if you just input them in the search bar and ask for conversion. 1 Kyros2000GameDev reacted to this Share this post Link to post Share on other sites

Rembrandt 0 Report post Posted March 18, 2016 Sweet, it's working now. Thanks for your explanation Azrael! Share this post Link to post Share on other sites

AzraelTycka 56 Report post Posted March 18, 2016 I'm glad you got it right. Do you have some working demo to see what you produced with that math? Even jsfiddle would be enough I just like animations with particles ;-). Share this post Link to post Share on other sites

Rembrandt 0 Report post Posted March 18, 2016 Well it's an OO pixi.js project so jsfiddle is out of the question i guess. But the game is almost finished, few more days. I will send you a link when I am done okay? ^^ Share this post Link to post Share on other sites

AzraelTycka 56 Report post Posted March 18, 2016 Ok there send me a pm I'm looking forward to it, good luck and bye :-). Share this post Link to post Share on other sites