Game Development Community

[SOLVED] Within field of camera view ?

by Bob Dobbs · in Torque X 2D · 12/02/2010 (8:48 am) · 1 replies

Hey folks,

I'm trying to calculate a method to determine if an sprite is within the current camera view. For example simple camera mounted to 2D Animated character sprite, as the character sprite moves check to see if an item sprite is within the current camera view....

float cameraX = _camera.Position.X;
float cameraY = _camera.Position.Y;
float cameraXLimit = _camera.SceneMax.X;
float cameraYLimit = _camera.SceneMax.Y;

if (myItem.Position.X > cameraX && myItem.Position.X < cameraXLimit && myItem.Position.Y > cameraY && myItem.Position.Y < cameraYLimit)
{
Console.WriteLine("Item is in view - CameraX" + cameraX + " CameraY" + cameraY + " within limit of " + cameraXLimit + "," + cameraYLimit);
}
else
{
 Console.WriteLine("item is OUT of  view - CameraX" + cameraX + " CameraY" + cameraY + " within limit of " + cameraXLimit + "," + cameraYLimit);
}

Anyone have any ideas ? Reason for asking is because of lag with spawned sprites. Currently my thinking was if I was to set a sprite to invisible and stop animation & movement (simple AI Chase) while it is OUT of camera view I might save a bit processing power and memory...or would I be wasting as much processing power checking to see if sprites are in or out of view....


#1
12/02/2010 (12:02 pm)
Found a way albeit a bit shabby.

float cameraX = _camera.Position.X;
float cameraY = _camera.Position.Y;

float cameraXLimitMax = _camera.SceneMax.X;
float cameraXLimitMin = _camera.SceneMin.X;

float cameraYLimitMax = _camera.SceneMax.Y;
float cameraYLimitMin = _camera.SceneMin.Y;


if ((myaxe.Position.X > cameraXLimitMin && myaxe.Position.X < cameraXLimitMax) && (myaxe.Position.Y > cameraYLimitMin && myaxe.Position.Y < cameraYLimitMax))
{
    Console.WriteLine("Item is in view");
}
else
{
    Console.WriteLine("Item is OUT OF view ");
}