Saturday, September 18, 2010

Keeping Their Distance

I've refined the code that determines the placement of the kids. Now the code ensures that the kids aren't placed to close. Every kid placed after the first one is checked for whether or not they are in the vicinity of any of the kids that have already been placed.

Here's a bit of code:
public bool DistanceCheck()
{
disCheck = false;

for(int i=0; i<antal-1;i++)
{
if ( ((kids[i].transform.position.x-temp.transform.position.x)*(kids[i].transform.position.x-temp.transform.position.x)) + ((kids[i].transform.position.y-temp.transform.position.y)*(kids[i].transform.position.y-temp.transform.position.y)) < 200)
{
disCheck = true;
}
}
return disCheck;
}

And a video with the latest improvement:

To sum up, reds are crying, blues are about to and green kids are okay for now. When the crying kids are tapped they turn happy (no paedophile thoughts intended).

2 comments:

  1. Slight optimization... store the temp values

    kids[i].transform.position.x-temp.transform.position.x

    into a temp variable. then u don't need to recalculate it when you square it. Similarly with the y :).

    ReplyDelete