Sign in to follow this  
captain_crunch

Camera problem when mixing 2d and 3d

Recommended Posts

I have a top-down game with a tilemap which is drawn using sprites. Scrolling is permitted along the x- and y-axis. On top of the tilemap I have 3d models representing vehicles. I have succesfully set up a 3d camera that follows the '2d camera' so that the 3d models stay inside their tiles when the player scrolls. The 3d camera is looking straight down the z-axis, using an orthogonal perspective. Now, however, what I'm really after is a view that is partially from the side, partially from the top. To implement this, I displace the 3d camera position a bit along the y-axis, while having the aimpoint stay the same. But the problem is that now the models start to drift outside their designated tile positions in the y-direction! They are only positioned correctly when they are in a certain portion (at the top) of the viewport/screen. What could be wrong about this setup? I'd appreciate any suggestion. I can also provide code if necessary. A fix I would rather not have to make is to go back to a straight top-down camera and draw every 3d model partially tilted on its side.

Share this post


Link to post
Share on other sites
Alright, I think I have it.

I set the model's position in world space as before (by multiplying the tile position with the tile dimensions). This position coordinate is used in all movement code and anything else.

But when rendering the model, I need to compensate for the distortion shown in the diagram. I do this by multiplying the y-position with a factor based on the distance from the camera target point. Trigonometry was useful here...

Here is the code:

float yDistanceFromCameraTarget = Location.Y - CameraTarget.Y;

float correctedYLocation = (float)(CameraTarget.Y + yDistanceFromCameraTarget / Math.Cos(MathHelper.PiOver2 - CameraViewingAngle));


Now the models are drawn exactly where I want them!

Share this post


Link to post
Share on other sites
I am also planning to convert my 2d tile based game with this 3d effect. Is your project written in XNA? Is this game going to be open source? I am glad that you are sharing this info after you figure out the method.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this