views:

407

answers:

1

I am adding "layer" objects to the stage with a depth value.

I have then created my own camera class. When I tell the camera to move to the right what Im actually doing is telling each layer object to move to the left.

The distance that the layer moved to the left is based on the value of its depth variable...

var fCameraDepth = 1;
var fTan:Number = Math.tan( fCameraMovement / fCameraDepth );
oLayer.x += fTan * fLayerDepth

This works well and gives me a really nice parallax effect. The problem I'm having is that I want to be able to tell the camera to look at a movie clip on any layer but I'm having trouble figuring out how to convert the movie clips coordinates to the cameras depth.

Im trying something like this...

var fCameraDepth = 1;
var fCameraPosition:Number = oCamera.x;

// the layer will have a + or - x val compared to the camera so we 
// need to take that into account when getting the targets position
var fTargetPosition:Number = oActor.x + oActor.getLayer().x;
var fTargetDepth:Number = oActor.getLayer().getDepth();

var fTan:Number = Math.tan( fTargetPosition / fTargetDepth );

var fTargetPositionAdjusted:Number = fTan * fCameraDepth;

oCamera.x = fTargetPositionAdjusted;

But the camera just wanders off somewhere (no where near the movie clip)

Can anyone get their head around this?

A: 

Camera depth should be relative to the layer -- I.E., fCameraDepth - fTargetDepth.

Make the equation that links camera position to object position. Then reverse the equation algebraically. My guess is that it will involve atan.

For a simpler implementation, I might recommend that depth be simply a variable between 0 and 1. Objects at 0 are infinitely far in the distance, and objects at 1 are at the camera's depth. Doing this will result in far easier math, that doesn't involve tangent or arc tangent.

HanClinto