Skip navigation

1st things first

the code

var startVector:b2Vec2 = b2o.GetWorldCenter();
var endVector:b2Vec2 = Char.playerChar.b2o.GetWorldCenter();
var castFix:b2Fixture = WorldManager.b2w.RayCastOne(startVector, endVector);
var input:b2RayCastInput = new b2RayCastInput(startVector, endVector);
var output:b2RayCastOutput = new b2RayCastOutput();
castFix.RayCast(output, input);

var lambda:Number = 1;
lambda = output.fraction;

var point1:Point = new Point(startVector.x * WorldManager.world_scale, startVector.y * WorldManager.world_scale);
var point2:Point = new Point(
(endVector.x * lambda + (1 - lambda) * startVector.x) * WorldManager.world_scale,
(endVector.y * lambda + (1 - lambda) * startVector.y) * WorldManager.world_scale);
var distance2p:Number = Math.sqrt(Math.pow((point1.x - point2.x) , 2) + Math.pow((point1.y - point2.y) , 2) );
distance2p = Math.round(distance2p + 10);
var realDist:Number = Math.round(b2Math.Distance(b2o.GetWorldCenter(), Char.playerChar.b2o.GetWorldCenter()) * WorldManager.world_scale);

tempg.graphics.clear();
tempg.graphics.lineStyle(1, 0xff0000, 1);
tempg.graphics.moveTo(startVector.x * WorldManager.world_scale, startVector.y * WorldManager.world_scale);
tempg.graphics.lineTo( (endVector.x * lambda + (1 - lambda) * startVector.x) * WorldManager.world_scale,
(endVector.y * lambda + (1 - lambda) * startVector.y) * WorldManager.world_scale);

if (distance2p == realDist)
{
aiTargetVec = Char.playerChar.b2o.GetWorldCenter().Copy();
}
if (aiTargetVec)
{
var folowVect = aiTargetVec.Copy();
folowVect.Subtract(b2o.GetWorldCenter());
folowVect.Normalize();
folowVect.Multiply(0.3);

mForce = folowVect;
}

if (mForce.x != 0 || mForce.y != 0)
{
b2o.ApplyImpulse(mForce, b2o.GetWorldCenter());
}
return true;

it is just the significant part of the update function. as:
b2o – b2Body of the current character (character class is named Char)
WorldManager.world_scale – it’s my constant for the physics scale
aiTargetVec – scopes to the current object
tempg – a temporary sprite for the “laser”

so from var startVector:b2Vec2 = b2o.GetWorldCenter(); to lambda = output.fraction; is basically the setup of the ray casting. point1 and 2 are the calculated start and end points of the ray calculated in the stage space. if the ray hits another object the distance between the points gets shorter.

lambda is the ratio the beam has traveled to it’s destination. because it will hit the outline of the player it will be always shorter with the distance between the player center and the hit point. thus i had to add 10pixels to it to get a correct comparison: distance2p == realDist.

– each time this returns true drops the position vector of the player into the aiTargetVec. the next lines basically move the zombies towards the player with an impulse of 0.3 each frame.

– if the distance2p == realDist returns false the aiTargetVec does not get updated so the zombies go to the last known location of the player and stage there like … well … brainz-les zombies.

it caps around 100 zombies each running towards you and bumping into each other (by my standards that’s 60% cpu usage).

have fun!

(the code is not optimized. the garbage collector works overtime)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: