Camera problem when mixing 2d and 3d
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.
I decided to make a picture to better explain this conundrum:
Clearly the formula for placing the models into the world is wrong...
Clearly the formula for placing the models into the world is wrong...
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!
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!
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.
Yes, it is an XNA project. Find out more here.
I am also glad that I can contribute a bit for a change. Still, what I am most glad about is that I finally got this problem nailed.
I am also glad that I can contribute a bit for a change. Still, what I am most glad about is that I finally got this problem nailed.
your project looks really interesting. I hope you can get this done soon. Do you have any demo or video to show your progress so far? Good luck with your project.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement