Data Realms Fan Forums
http://45.55.195.193/

What does this particle code do?
http://45.55.195.193/viewtopic.php?f=73&t=15432
Page 1 of 1

Author:  numgun [ Fri Jun 19, 2009 3:30 pm ]
Post subject:  What does this particle code do?

I'm cutting up the particle accelerators code into pieces to try make a more stable and better version of it and after looking at some of the Lua nuclear scientist code, I ran into this part:

Code:
         local power = (50 * (2 + self.Scale));
         for particle in MovableMan.Particles do
            if particle.PresetName ~= nil and particle.PresetName ~= "Particle Beam Emission Dummy"  then
               if math.abs(particle.Pos.X - self.Pos.X) < power and math.abs(particle.Pos.Y - self.Pos.Y) < power then
                  local distVec = self.Pos - particle.Pos;
                  particle.Vel = particle.Vel + distVec:SetMagnitude(distVec.Magnitude/50);
               end
            end
         end


I'm guessing it draws the particles to the position of self, but how?
what is the local power var for and what exactly is this code doing?
How does it move the said particles and where?

Author:  411570N3 [ Fri Jun 19, 2009 3:39 pm ]
Post subject:  Re: What does this particle code do?

Code:
         local power = (50 * (2 + self.Scale));--Has the power of pull increase as the ball grows larger.
         for particle in MovableMan.Particles do
            if particle.PresetName ~= nil and particle.PresetName ~= "Particle Beam Emission Dummy"  then --As long as the particle exists and isn't a presumably effect particle
               if math.abs(particle.Pos.X - self.Pos.X) < power and math.abs(particle.Pos.Y - self.Pos.Y) < power then --As long as particle is within square centred on orb or possibly radius, I don't know what math.abs does
                  local distVec = self.Pos - particle.Pos; --Makes a new vector of the difference in position between the orb and the affected particle
                  particle.Vel = particle.Vel + distVec:SetMagnitude(distVec.Magnitude/50); --Add the above defined vector divided by 50 to the particle's velocity, propelling it in the orb's direction.
               end
            end
         end

Author:  numgun [ Fri Jun 19, 2009 3:48 pm ]
Post subject:  Re: What does this particle code do?

Thanks alot, now I think I got it now. :smile:
This code was pretty much like the gravitation code piipu used in his lua guns except this also included the scale increase that would increase the radius...

sweet.

Author:  zalo [ Fri Jun 19, 2009 4:04 pm ]
Post subject:  Re: What does this particle code do?

If only it did draw something.

Then we could get our Draw Functions to work, and it would be great.

Author:  Kyred [ Sun Jun 21, 2009 12:39 am ]
Post subject:  Re: What does this particle code do?

Numgun wrote:
I'm guessing it draws the particles to the position of self, but how?
what is the local power var for and what exactly is this code doing?
How does it move the said particles and where?

Code:
         local power = (50 * (2 + self.Scale));
         for particle in MovableMan.Particles do
            if particle.PresetName ~= nil and particle.PresetName ~= "Particle Beam Emission Dummy"  then
               if math.abs(particle.Pos.X - self.Pos.X) < power and math.abs(particle.Pos.Y - self.Pos.Y) < power then
                  local distVec = self.Pos - particle.Pos;
                  particle.Vel = particle.Vel + distVec:SetMagnitude(distVec.Magnitude/50);
               end
            end
         end


Heh, a bit late for this, but I'll explain the math anyway.

local power = (50 * (2 + self.Scale));
The local variable power, represents the suction power of the accelerator. It determines the radius the suction starts at. When you first start charging the weapon, power is equal to 105 (50 * (2 + 0.1)). At full charge, the radius increases to 175 (50 * (2 + 1.5)).

if math.abs(particle.Pos.X - self.Pos.X) < power and math.abs(particle.Pos.Y - self.Pos.Y) < power then
This actually could be simplified to: (particle.Pos - self.Pos).Magnitude < power. The engine could would handle the subtraction of individual vector components. math.abs() means "Absolute Value of". Since magnitude is always positive, we need the absolute value.

local distVec = self.Pos - particle.Pos;
Just like 5 - 2 will give you the numeric distance between 5 and 2, 'vector a' - 'vector b' will give you a 'vector c' pointing from b to a (or a to b, if you flip them around). The magnitude of c will equal the distance between a and b. [positions can be considered vectors with a starting point at (0,0) and an ending point at the position].

In this case, 'vector c' is distVec. Since distVec points from the particle's position to the orb's position, all we have to do is add this vector to the particle's velocity vector to give it a natural pull effect towards the orb. distVec's magnitude is divided by 50 to scale the the pull down to a speed that isn't retardedly fast (oxymoron?).

particle.Vel = particle.Vel + distVec:SetMagnitude(distVec.Magnitude/50);
The end result of all this is: the greater the distance between the particle and the orb, the greater the pull on the particle.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/