Uniform Volume Distribution ? Optimization |
game_maker | Hi
if you have a particle system and you want it as a Uniform Volume Distribution (when you set dx and dy and dz (field width) <> 0
so if you put dy = 0 it will be a surface in xz - plane (snow & rain is a good application)
you can do a "Uniform Volume Distribution" in form of three loops
[code]
N = 125
dn = n ^ (1/3)
for z = 0 to dn -1
for y = 0 to dn -1
for x = 0 to dn -1
.......statements ...........
next
next
next
[/code]
But in particle System We can't make three loops ,,, it must be ONE loop ,,, the quastion is HOW to this with Optimization ?
[code]
Dim N As Long, dN As Long
Dim Dx As Long, Dy As Long, DZ As Long
Private Sub Form_Load()
Me.Show: Me.Caption = "Uniform Volume Distribution ; By Yazeed Al-Deligan"
Me.Width = 11111: Me.Height = 7777
Me.BackColor = RGB(122, 150, 223)
N = 100
Dx = 6000
Dy = 6000
DZ = 0
VD
End Sub
Private Sub VD()
Dim Px As Long, Py As Long, Pz As Long
Dim I As Long, R As Byte, G1 As Long, G2 As Long
R = ((Dx <> 0) + (Dy <> 0) + (DZ <> 0)) * -1
If (R = 0) Then dN = 1 Else dN = Int(N ^ (1 / R))
For I = 0 To N - 1
G1 = IIf(Dx = 0, 1, dN)
G2 = IIf(Dy = 0, 1, dN)
Px = Dx * (0.5 - (I Mod dN) / dN)
Py = Dy * (0.5 - ((I \ G1) Mod dN) / dN)
Pz = DZ * (0.5 - ((I \ (G1 * G2))) / dN)
Me.Circle (5000 + Px, 3000 + Py), 100
Print I
DoEvents
Next
End Sub
[/code]
Is it Optimized .... Is working on all cases ,,,,, I need to be sure 100% becouse Particle System make no jokes you know !!!
second question : i have seen some system have energy , bounce ,,,ect (where this came from and what are the equation or its just an expression for behavior ie( constant or variable with system or particle) ,,,, I mean everyone made his own system or its standard ?!! |
Sr. Guapo | What are you trying to do exactly? Do you just want to make a 3D particle system with random values/coordinates? A little more detail may help... [:)] |
game_maker | Yes,
I am sorry I think that I started to write the code before giving enough information about what I want to do......
Hmmmm.... in particle system there is a shape of source providing particles ....like point, sphere, and box
Usually it's started from point source
And what I want to is Uniformly Volume Distribution (Box)
And I am not so sure about my code because it contains a power calculation witch will slow the performance [(1/3) and square root (1/2)]
By the way I should use y = sqr(x) instead of y= x ^ (1/2) by logic
Did I clarify my problem ? [8)] |
Eric Coleman | I'm not really sure what you're trying to do either.
If you want a uniform distribution of particles, then simply pick random coordinates in the volume.
If you want a box, then use
[code]
For n = 1 to NumberOfParticles
X = (maxX - minX) * Rnd + minX
Y = (maxY - minY) * Rnd + minY
Z = (maxZ - minZ) * Rnd + minZ
Next[/code]
or if you want a sphere, then use
[code]
For n = 1 to NumberOfParticles
X = (maxX - minX) * Rnd + minX
Y = (maxY - minY) * Rnd + minY
Z = (maxZ - minZ) * Rnd + minZ
If (X * X) + (Y * Y) + (Z * Z) <= Radius * Radius Then
'Use particle coordinates
Else
'don't use particle coordinates, out of bounds of the sphere.
End If
Next[/code] |
cjb0087 | use x ^ .5, always pre-pointify you fractions |
game_maker | Hi
Eric ,Yes your code gives UnUniform Box Distribution becouse your are using random values ,,, I want it to be Uniform becouse it should be uniform ,, I think
cjb0087 : I am using now sqr(x) [:)] |
Eric Coleman | If you want to optimize your original loop, then you might want to calculate G1 and G2 before the loop begins because their values don't change in the loop.
Actually, the code I gave does give a "uniform" distribution of particles. The particle density (versus particle location) is what is uniform in my example. A "non-uniform" distribution would be where the density of the particles is different in different locations of the volume. For example, the following bit of code would generate a non-uniform particle distribution with a higher concentration of particles along the plane Z = minZ
[code]
For n = 1 to NumberOfParticles
X = (maxX - minX) * Rnd + minX
Y = (maxY - minY) * Rnd + minY
Z = (maxZ - minZ) * Rnd + minZ
Z = ((Z - minZ) / (maxZ - minZ))^2 * Z + minZ
Next
[/code] |
game_maker | hi
I understand that if the density is constant (ie does not change with position ) then it is uniform ,,, but I can't see why is your code is uniform ! [B)] becouse :
1- rnd
2- x & y & z has no relation while the should be becouse of the density
3- i have test it
about G1,G2 you are right [:)] |
Eric Coleman | The reason it is uniform is because the RND function is uniform. If you plot a histogram of the RND function, it will be a horizontal line. |
game_maker | Still not sure [?]
OK there is a chance that the rnd function gives 0.5 (assume) then what will be the second number for rnd take make the Distribution Uniform?
i.e. (1, x, 0.5, 0), there is no solution, that's one
If I have the first number of rnd and we assumed it's uniform then I can calculate any further rnd output numbers, right? Witch conflict with the idea of rnd ...... that's two
Third, if you edit my code to your code the result won't be uniform [:)] |
Eric Coleman | As I said in an earlier post, the code I gave generates uniform "density", not uniform "position".
Consider a container that is filled with air. The atoms are uniformly distributed in the volume, which means the amount of air at the top of the container is the same as the amount of air at the bottom of the container. However, their positions are not in a grid pattern.
If you want particles in a grid, then use the code you posted. |
game_maker | hmmmmm
good ,,, the problem is I don't know what is better to use ,,, what do seggest me to choose (uniform density only or with position) for particle system ? |
Sr. Guapo | What kind of particle system? Most likely you will want a random system, it would look more realistic, but it depends what you need it for... |
game_maker | you are right .... it's better to use random values
Soo , I am going to use Eric code
[code]
Dim N As Long
Dim Dx As Long, Dy As Long, DZ As Long
Private Sub Form_Load()
Me.Show
Me.Width = 11111: Me.Height = 7777
Me.BackColor = RGB(122, 150, 223)
N = 2500
Dx = 6000
Dy = 6000
DZ = 0
VD True
End Sub
Private Sub VD(bSphere As Boolean)
Dim Px As Long, Py As Long, Pz As Long
Dim I As Long, dM As Single
dM = -IIf(Dx > Dy, (Dx > DZ) * Dx + (DZ > Dx) * DZ, (Dy > DZ) * Dy + (DZ > Dy) * DZ) / 2
dM = dM * dM
For I = 0 To N - 1
InitP:
Px = Dx * (0.5 - Rnd)
Py = Dy * (0.5 - Rnd)
Pz = DZ * (0.5 - Rnd)
If bSphere Then
If (Px * Px) + (Py * Py) + (Pz * Pz) > dM Then GoTo InitP:
End If
Me.Circle (5000 + Px, 3500 + Py), 300
Print I
DoEvents
Next
End Sub
[/code] |
game_maker | another qeustion [?]
[code]
if (condition1 and condition2) then ....
[/code]
if condition1 is False does vb check condition2 !!!
if Not then I should write it as
[code]
if condition1 then
if condition2 then
or
if condition2 then
if condition1 then
[/code]
what to put inside and what to put outside depens on 2 things (%possibility to be flase or ture and faster to perform)
What do you think ? |
Sr. Guapo | I am not sure how VB does it. I know in c++ and Java, there are seperate operators to do both:
& - evaluate both conditions
&& - if the first condition is false, skip the rest
I think VB probably skips, but I am not sure... |
game_maker | nice ,,,you can test that in c or java by assigning
if (a=5) mean assigning (not comparing)
if(a==5) here it's comparing
soo in C for example
[code]
void main (void)
{
int a = 1;
if (0 && (a=2)) a=3;
cout << a << '\n' ;
}
[/code]
OutPut is 1 so it's skips as you told me
we can't apply this test for vb becouse = will be taken as comparing ,,, and we can't use a sub (it must be a real condition or a function)
[code]
Dim Y As Long
Private Sub Form_Load()
If False And X Then MsgBox "Mission Impossible"
MsgBox IIf(Y = 0, "It's Skipps", "Please No!")
End Sub
Private Function X() As Boolean
Y = 1
End Function
[/code]
the result is "Please No!" [B)] |