I am trying to implement Artificial intelligence in my actionScript3 game using vectors from box2d. I am copying vector, multiplying, normalizing ... 10 times each update and because of so much calculations game starts to lagg. How do i reduce that lagg, bescause game almost freezes. Is that a bug or i am not doing it right.
I tried setting them all to null but problem still exist.
Code: Select all
for (var i:int = 0; i < obstacles.length; i++)
{
var velocityTemp:b2Vec2 = velocity.Copy();
velocityTemp.Normalize();
var forward:b2Vec2 = b2Vec2.Make(velocityTemp.x, velocityTemp.y);
var diff:b2Vec2 = b2Vec2.Make(obstacles[i].getPosition().Copy().x - position.x,obstacles[i].getPosition().Copy().y - position.y);
var dotProduct:Number = b2Math.Dot(diff, forward);
if (dotProduct > 0)
{
var ray:b2Vec2 = b2Vec2.Make(forward.Copy().x * 10, forward.Copy().y * 10);
var projection:b2Vec2 = b2Vec2.Make(forward.Copy().x * dotProduct, forward.Copy().y * dotProduct);
var dist:b2Vec2 = b2Vec2.Make(projection.Copy().x - diff.x, projection.Copy().y - diff.y);
var distance:Number = dist.Length();
ray = null;
projection = null;
dist = null;
distance = 0;
}
}
velocityTemp = null;
velocity = null;
position = null;
forward = null;
diff = null;
dotProduct = 0;
}I tried setting them all to null but problem still exist.