# Physics generating particles aimed at certain angle...

This topic is 4683 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 generic, basic particle system that constantly shoots straight up and the particles get a random x and y velocity so they arc in a fountain like fashion. I was wondering how to rotate this so the programmer/user could "aim" the system at something... Let me show you. I would like the particles to aim within the two arrows given the angle in the middle (the smaller line with no arrow head ~45 degrees, Pi/4 radians). P.S. / Edit: I do know vector math and whatnot if it is needed... How would I go about doing this? Thank you much, John Sedlak

##### Share on other sites
alpha is the angle you are aiming at
beta is half the angle between alpha and the border (so 2*beta is the angle between the arrows in your image)

r = random number between -1 and +1
vx = cos(aplha + r*beta)
vy = sin(alpha + r*beta)

vx and vy are the components of your velocity vector
the length of this vector is 1, so you may want to multiply it with a random factor

##### Share on other sites
Thanks for the help.

There are some problems with it but I think I will be able to get it down to what I want.

Here is a video of the particle system. The fps is very low because I am recording live video as well as drawing 500 quads with no sort of buffer right now.

[Zip File: 6.59mb -> .wmv format]

Do you like it so far?

image...

##### Share on other sites
I dont know why this is difficult.

Can you do a rand range??

float angle = rand_in_some_range( low_angle, hi_angle );

particle.velocity = Vector2(cos(angle), sin(angle)) * speed_or_something.

[Edited by - Raymond_Porter420 on April 27, 2005 4:07:08 PM]

##### Share on other sites
I am now doing it like so:

// Convert current angle to degreesint indeg = (int)(m_angle * 180 / Math.PI);// Get random angle within boundsfloat ang = (float)(m_random.Next(indeg - 15, indeg + 15) * Math.PI / 180);// Get a random speed valuefloat random_speed = (float)(m_random.NextDouble() * 10);// Setup velocities.m_parts.Xv = Math.Cos( (double)ang ) * random_speed;m_parts.Yv = -Math.Sin( (double)ang ) * random_speed;

##### Share on other sites
Quote:
 Original post by KriscI am now doing it like so:// Convert current angle to degreesint indeg = (int)(m_angle * 180 / Math.PI);// Get random angle within boundsfloat ang = (float)(m_random.Next(indeg - 15, indeg + 15) * Math.PI / 180);// Get a random speed valuefloat random_speed = (float)(m_random.NextDouble() * 10);// Setup velocities.m_parts.Xv = Math.Cos( (double)ang ) * random_speed;m_parts.Yv = -Math.Sin( (double)ang ) * random_speed;

Radians tend to be decimal numbers, so storing the angle in an 'int' will cause you to lose information. How about determining its angle first in degrees, and THEN converting it into radians? Like so:

// Get random angle within bounds (then convert to radians)float ang = (float)(m_random.Next(m_angle - 15, m_angle + 15) * Math.PI / 180);

Particles look nifty so far, BTW. Keep it up! :)

##### Share on other sites
Quote:
Original post by HopeDagger
Quote:
 Original post by KriscI am now doing it like so:// Convert current angle to degreesint indeg = (int)(m_angle * 180 / Math.PI);// Get random angle within boundsfloat ang = (float)(m_random.Next(indeg - 15, indeg + 15) * Math.PI / 180);// Get a random speed valuefloat random_speed = (float)(m_random.NextDouble() * 10);// Setup velocities.m_parts.Xv = Math.Cos( (double)ang ) * random_speed;m_parts.Yv = -Math.Sin( (double)ang ) * random_speed;

Radians tend to be decimal numbers, so storing the angle in an 'int' will cause you to lose information. How about determining its angle first in degrees, and THEN converting it into radians? Like so:

// Get random angle within bounds (then convert to radians)float ang = (float)(m_random.Next(m_angle - 15, m_angle + 15) * Math.PI / 180);

Particles look nifty so far, BTW. Keep it up! :)

What I am actually doing is converting the angle to degrees because the random function requires integers for bounds. After I get the random angle in degrees i convert it back to radians. Then I get a random speed and then plug it all into the x and y velocities.

##### Share on other sites
you can get floats from rand pretty easily though

float f;

f = (float)(rand() % (RAND_RANGE * PRECISION) - (RAND_RANGE * PRECISION) / 2);

##### Share on other sites
Quote:
 Original post by KriscWhat I am actually doing is converting the angle to degrees because the random function requires integers for bounds. After I get the random angle in degrees i convert it back to radians. Then I get a random speed and then plug it all into the x and y velocities.

Doh. And again my conversions whack me around like a sack of onions! Sorry about that, I had misread the order of your PI/180 and 180/PI equations. :)