Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MythicFrost

macrumors 68040
Original poster
Mar 11, 2009
3,944
40
Australia
Hey all, I'm having a little trouble measuring the distance of a path in a game. Well, not so much that part, but calculating the distance of an enemy from the current way point.

The quoted part can be ignored if you're not interested in the brief explanation. I've found the problem to be with the method at the very bottom, which you can skip to if you like.

Let's say I've got a 10x10 map and each tile is 32x32. On this map I've got several way points (e.g, 2,1), and my enemies spawn and follow these way points. I'm working on tower AI and trying to get the towers to target the enemy closest to reaching the end of the path.

Each waypoint on the map is of my CCWayPoint class, and in it is a variable called distanceFromLastWayPoint which stores the distance between it and the way point before it, measured using the getDistanceBetweenFrameAandFrameB method below. I do that for all waypoints on the map, and I add all those values up and store them in a variable called totalDistanceOfPath, and each enemy has a variable with that same name, set to that value in totalDistanceOfPath.

Each enemy has a variable called distanceTravelled, and each time it passes a waypoint, it increases distanceTravelled by that waypoint's distanceFromLastWayPoint variable.

This is my getDistanceToTravelForEnemy method...

Code:
- (double)getDistanceToTravelForEnemy:(CCEnemy *)enemy {
    double distanceToTravel = 0;
    
    distanceToTravel = enemy.totalDistanceOfPath-enemy.distanceTravelled;

    return distanceToTravel;
}
It works correctly, but obviously it only updates when an enemy passes a waypoint. If I add this code below, just above the return line, the results start getting screwy:

Code:
CCWayPoint *currentWayPoint = [enemy.wayPoints objectAtIndex:enemy.currentWayPointIndex];
CGRect currentWayPointRect = CGRectMake(currentWayPoint.col*gameInfo.tileSize, currentWayPoint.row*gameInfo.tileSize, gameInfo.tileSize, gameInfo.tileSize);
    
double distanceBetweenEnemyAndCurrentWayPoint = [self getDistanceBetweenFrameA:enemy.frame andFrameB:currentWayPointRect];

NSLog(@"cwp: %f dec: %f", currentWayPoint.distanceFromLastWayPoint, distanceBetweenEnemyAndCurrentWayPoint);
    
distanceToTravel -= (currentWayPoint.distanceFromLastWayPoint - distanceBetweenEnemyAndCurrentWayPoint);

This code is supposed to get the distance between the enemy and the waypoint, and then adjust distanceToTravel accordingly, so you see a smooth continual decrease in distanceToTravel until it reaches the end of the path. And whilst it does decrease, it also can jump around by values of 100 when turning corners, and when approaching corners it can begin to increase by a small amount.

I believe that the getDistanceBetweenFrameAandFrameB method is the problem here, as I find distanceBetweenEnemyAndCurrentWayPoint which stores the return value from it, is sometimes higher in value than currentWayPoint.distanceFromLastWayPoint, which shouldn't be possible as it's in-between the current waypoint and the waypoint before it. It obviously can't be in the middle of the two, and behind the latter at the same time.

I'm not sure what's wrong with that method though, and I'm hoping someone will be able to help me figure it out. The method is below:

Code:
- (double)getDistanceBetweenFrameA:(CGRect)frameA andFrameB:(CGRect)frameB {
    double enemyX = frameA.origin.x + (frameA.size.width/2);
    double enemyY = frameA.origin.y + (frameA.size.height/2);
    
    double towerX = frameB.origin.x + (frameB.size.width/2);
    double towerY = frameB.origin.y + (frameB.size.height/2);
    
    double diffX = enemyX-towerX;
    double diffY = enemyY-towerY;
    
    double calc = pow(diffX, 2) + pow(diffY, 2);
    
    double curDistance = sqrt(calc);
    
    return curDistance;
}

Would appreciate any help!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.