Data Realms Fan Forums
http://45.55.195.193/

Boids Troubles - Help Please
http://45.55.195.193/viewtopic.php?f=73&t=16069
Page 1 of 2

Author:  zalo [ Fri Jul 31, 2009 9:18 pm ]
Post subject:  Boids Troubles - Help Please

So basically what it tries to do is create 4 boids when you fire (including self).

But after a little bit of boiding around, they all drift apart from eachother, even
though you can see the muzzleflashes are pointing in different directions than
where they are drifting.

Why does the emitting suddenly stop pushing the emitter?

Attachments:
File comment: This is a little more complicated than just showing you the code.
Boids.rte.rar [3.02 KiB]
Downloaded 213 times

Author:  Kyred [ Sat Aug 01, 2009 7:17 am ]
Post subject:  Re: Boids Troubles - Help Please

This is the only emission that I saw with a PushesEmitter = 1. It's set to stop emitting after 3 seconds.
Code:
   AddEmission = Emission
      EmittedParticle = MOPixel
         CopyOf = Air Blast
      ParticlesPerMinute = 2000
      StartTimeMS = 500
      StopTimeMS = 3000
      BurstSize = 2
      Spread = 0.5
      MaxVelocity = 3
      MinVelocity = 3
      PushesEmitter = 1

Author:  zalo [ Mon Aug 03, 2009 5:42 pm ]
Post subject:  Re: Boids Troubles - Help Please

Awesome. It works!

Btw is there a way to make them just Orient to Velocity upon
spawning so I won't have to use that "OrienttoVel" variable
that messes it up?

Author:  robolee [ Mon Aug 03, 2009 6:14 pm ]
Post subject:  Re: Boids Troubles - Help Please

in function create(Self)
self.RotAngle = math.atan(self.Vel.X/self.Vel.Y);
?

Author:  zalo [ Tue Aug 04, 2009 4:02 am ]
Post subject:  Re: Boids Troubles - Help Please

Cool, all I need to do is make it so you can dynamically keep adding Boids to the system.

Thanks guys *Off to learn tables and stuff*.

Author:  zalo [ Tue Aug 04, 2009 6:20 am ]
Post subject:  Re: Boids Troubles - Help Please

How can I go through an Array/Table and add together all of
a variable from the objects from that table so then I can divide
that by the number of objects in the table? :grin:

Author:  Grif [ Tue Aug 04, 2009 6:58 am ]
Post subject:  Re: Boids Troubles - Help Please

for pointer in table do
local sum = sum + pointer.value;
local average = sum / getn(table);
end

Author:  zalo [ Tue Aug 04, 2009 5:20 pm ]
Post subject:  Re: Boids Troubles - Help Please

Code:
function Create(self)
    if BoidList == nil then
   BoidList = { };
    end
    self.ListNumber = #BoidList + 1;
    BoidList[#BoidList + 1] = self;

   local DirAway = 0;
   local AvgDir = 0;
   local targetdir = 0;
   local DirSum = 0;
   local PosSum = 0;
   self.RotAngle = math.atan(self.Vel.X/self.Vel.Y);
end


function Update(self)
       for i=1, #BoidList do
       if MovableMan:IsParticle(BoidList[i]) then

    for Particle in BoidList do
      local avgx = Particle.Pos.X - self.Pos.X;
      local avgy = Particle.Pos.Y - self.Pos.Y;
      local dist = math.sqrt(avgx ^ 2 + avgy ^ 2);
      if dist < 20 then
         DirAway = -(math.atan2(-(Particle.Pos.Y-self.Pos.Y),(Particle.Pos.X-self.Pos.X)));
      end
    end

   local DirSum = 0;
      DirSum = DirSum + BoidList[i].RotAngle;
      AvgDir =  DirSum / #BoidList;

   local PosSum = 0;
      PosSum = PosSum + BoidList[i].Pos;
      AvgPos =  PosSum / #BoidList;

   local DirToCenter = math.atan2(-(AvgPos.Y-self.Pos.Y),(AvgPos.X-self.Pos.X));
   local targetdir = (DirToCenter + AvgDir + DirAway) / 3;

   if targetdir ~= self.RotAngle then
      self.RotAngle = targetdir;
   end

       --Make sure the missile's thruster is firing.
       if self:IsEmitting() == false then
           self:EnableEmission(true);
       end
   end
   end
end

function Destroy(self)
   BoidList[self.ListNumber] = nil;
end


"Attempted to get Value from Table! Line 21"
I want it to get a value from a table! Why is that so wrong?!
Btw Line 21 is:
Code:
    for Particle in BoidList do

Author:  Mind [ Tue Aug 04, 2009 6:04 pm ]
Post subject:  Re: Boids Troubles - Help Please

Pretty sure it needs to be self.BoidList, not just BoidList

Author:  zalo [ Tue Aug 04, 2009 10:33 pm ]
Post subject:  Re: Boids Troubles - Help Please

Well, I managed to fix the above piece of code using some TLB magic, but there are still some problems.

Image
http://i34.photobucket.com/albums/d144/ ... ump020.jpg

I fixed the table part, but didn't touch anything else, so what's up?

Author:  zalo [ Thu Aug 06, 2009 6:43 am ]
Post subject:  Re: Boids Troubles - Help Please

More progress, still broken:

Code:
function Create(self)
    if BoidList == nil then
   BoidList = { };
    end
    self.ListNumber = #BoidList + 1;
    BoidList[#BoidList + 1] = self;

   local DirAway = 0;
   local AvgDir = 0;
   local targetdir = 0;
   local DirSum = 0;
   local PosSum = Vector(0,0);
--   local PosSum.X = 0;
--   local PosSum.Y = 0;
   local AvgPos = Vector(0,0);
--   local AvgPos.X = 0;
--   local AvgPos.Y = 0;
   self.RotAngle = math.atan(self.Vel.X/self.Vel.Y);
end


function Update(self)
       for i=1, #BoidList do
       if MovableMan:IsParticle(BoidList[i]) then
      local avgx = BoidList[i].Pos.X - self.Pos.X;
      local avgy = BoidList[i].Pos.Y - self.Pos.Y;
      local dist = math.sqrt(avgx ^ 2 + avgy ^ 2);
      if dist < 20 then
         DirAway = -(math.atan2(-(BoidList[i].Pos.Y-self.Pos.Y),(BoidList[i].Pos.X-self.Pos.X)));
      end


   local DirSum = 0;
      DirSum = DirSum + BoidList[i].RotAngle;
      AvgDir =  DirSum / #BoidList;

   local PosSum = Vector(0,0);
--      PosSum = PosSum + BoidList[i].Pos;
      PosSum.X = PosSum.X + BoidList[i].Pos.X;
      PosSum.Y = PosSum.Y + BoidList[i].Pos.Y;
--      AvgPos =  PosSum / #BoidList;
      AvgPos.X = PosSum.X / #BoidList;
      AvgPos.Y = PosSum.Y / #BoidList;

   local DirToCenter = math.atan2(-(AvgPos.Y-self.Pos.Y),(AvgPos.X-self.Pos.X));
   local targetdir = (DirToCenter + AvgDir + DirAway) / 3;

   if targetdir ~= self.RotAngle then
      self.RotAngle = targetdir;
   end

       --Make sure the missile's thruster is firing.
       if self:IsEmitting() == false then
           self:EnableEmission(true);
       end
   end
   end
end

function Destroy(self)
   BoidList[self.ListNumber] = nil;
end


Line 39 Attempting to call Global PosSum (A nil Value!)
Line 39: PosSum.X = PosSum.X + BoidList[i].Pos.X;
:???:

Author:  Grif [ Thu Aug 06, 2009 2:27 pm ]
Post subject:  Re: Boids Troubles - Help Please

A local created in create is only accessible in create afaik.

Author:  Geti [ Thu Aug 06, 2009 9:37 pm ]
Post subject:  Re: Boids Troubles - Help Please

use self.PosSum instead of local.

Author:  robolee [ Fri Aug 07, 2009 7:57 pm ]
Post subject:  Re: Boids Troubles - Help Please

only use local if you only want the variable to be accessed in the if loop it is in as it gets deleted at the end of the loop.

Author:  Grif [ Fri Aug 07, 2009 9:38 pm ]
Post subject:  Re: Boids Troubles - Help Please

*block but hey who's counting

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