camera position

in a top down 2d game, how do u calculate the camera position, so that when the player is in the middle of the and flying around the cam stays centered on the spaceship or whatever, but when they go to the edge of the map the screen stops moving and the player moves around

It isn’t that hard really. The ship is always moving relative to the level.
Move the camera as much as you can to center on the ship position and draw what is left.

This is taken straight out of my 4KShooter, it gives camera look ahead, based on both the direction you are looking, and the velocity of the ship. (I tried using ‘where you are looking with the mouse’, but that was very disorienting)
If you want it without the look ahead, just remove the dx8,dy8, shipSin64, shipCos64.


float tx = Math.max(Math.min((SCREEN_WIDTH/2)-x-dx*8+shipSin*64,0),-(MAP_WIDTH-(SCREEN_WIDTH)));

float ty = Math.max(Math.min((SCREEN_HEIGHT/2)-y-dy*8-shipCos*64,0),-(MAP_HEIGHT-(SCREEN_HEIGHT)));

Both tx, and ty will always be negative, because they are the World->Camera transform.

and here is how to use it, (adapted from 4KShooter) :-


g.translate((int)tx,(int)ty);

for all your objects
{
g.drawImage(image, x,y,null);
}

will tx and ty represent the top corner or the center of the screen?

[quote]will tx and ty represent the top corner or the center of the screen?
[/quote]
top corner, negated.

ok… ill have to play with it alot to get it to work lol…

right now ive got a red box that moves around., and adding the camera position thing doesnt work correctly …