Mixing 2D with 3D

Started by
9 comments, last by EGD Eric 20 years, 1 month ago
Hey there, I''m working on a 2D game with 3D models. Its an asteroids clone. I''ve run into some difficulties. Before I started the project, someone told me: "Use pre-rendered images, not 3D models. 3D and 2D are not meant to mix". I didn''t know what he was talking about, but now I do. To sum it up: How do I measure a "screen" in projection mode? If I want to know how many "Screens" the player is from a point, that would be easy to do in orthographic mode: everything''s in pixels, but how do you do that in Projection mode? Orthographic mode is necessary for background textures, because I like working in pixels for 2D stuff. Its really easy to fit a texture to fit the whole screen. Has anyone here ever built a 2D/3D game and run into the same kind of difficulties?
Advertisement
the width/height of your screen depends on the distance to the viewer, which is easily calculated since the view frustum is a truncated pyramide...
so if you know where you need the width to be measured you just need to compute the corners of that pyramide and interpolate between them ...
our new version has many new and good features. sadly, the good ones are not new and the new ones are not good
Can you help me out with that? I'm no good at math.

What are my variables?

1: The camera's Z value (The parameter I entered for the z value in gluLookAt)

2: The FOV is usually 45%.

I know how to get the length of the base of an isosceles triangle knowing only the height and one of the angles, but how does this work with a 3D frustum?

Edit: Does the aspect ratio interfere with this at all?

[edited by - EGD Eric on March 20, 2004 1:14:26 PM]
the fov-a is usually 45 DEGREES, not 45%...


so the angle hs and hs on the other side is 45 degrees - and that one between h and hs is 45/2 = 22.5 degrees.
the width of the frustum at a certain position therefore is sin(22.5 degrees)*distance_to_camera
our new version has many new and good features. sadly, the good ones are not new and the new ones are not good
are't DEGREES and % the same thing?


Please help me clarify this, because I'm confused:

I would have thought you would have to measure half the width
using tan(22.5) * distance_to_camera. Since you're using Opposite/adjacent. Half the bottom of the width is the opposite, the angle is the top of the pyramid (angle between hs and h), the adjacent is the height. I'm looking for the Opposite/hypotenuse thing that you're using but I can't find that opportunity in that shape.

How did you put the picture there?

Another question: Since a screen is always wider than it is tall, do I have two values to get? width_X and width_Y? The "aspect ratio" is 4/3 (or 1.3333) in my program (and probably all of them)

[edited by - EGD Eric on March 20, 2004 3:05:55 PM]
% = Percent, usually ranges from 0-100
Degrees = Measure of an angle, 0-360 usually
Of course!! What was I thinking? I meant degrees, I just got the symbol mixed up. damnit, there should be a degrees symbol in ascii and on the keyboard.
half the height of your frustum is tan(fov/2), multiply with aspect to get the width. thats for a distance of 1, so the result has to be multiplied with your actual distance. but if the game is happening in 2d you can still try and use ortho. unless your objects have a lot of depth it shouldnt look too different.
f@dzhttp://festini.device-zero.de
Yeah, I was thinking the same thing. But then I thought: If everything is in pixel coordinates, and there are many screens, and each object translate''s to its own position before drawing itself, wouldn''t the numbers range from something like: 0 - 1,000,000,000? I don''t think ints can go that far. I''d have to use doubles for the values.

Whenever I use ortho mode to place text or interface items on the screen, I use this function to go into ortho mode:



void Enable2D(){    int viewPort[4];        // Get the current viewport dimensions       glGetIntegerv(GL_VIEWPORT, viewPort); // Select the projection matrix       glMatrixMode(GL_PROJECTION);  // Push the current projection matrix       glPushMatrix();    //save matrix   glLoadIdentity();   // Setup ortho mode       glOrtho(0, viewPort[2], 0, viewPort[3], -1, 1);   // Select the modelview matrix         glMatrixMode(GL_MODELVIEW);    // Push the current modelview matrix       glPushMatrix();       glLoadIdentity(); //reset modelview matrix}    


I then use another function to get out of ortho mode. Great thing about this is, I can place stuff on the screen like debug text or interface that won''t get left behind when the player moves along. Until recently I thought that I could just follow the player along with gluLookAt, and everything in the world would have a fixed position. Now I''m thinking that maybe I should go with the classic 2D style of scrolling (which I''ve never done before: this is the first game I''ve ever made).

Would using that function above work for a 2D game? because everything''s in pixel coords, objects could have coordinates of anything from 0 to 1000000, seeing as there are many screens.


Can anyone give me some theory on how a 2D game like mine could be done? Maybe I just draw the player in the center, use a tile map for the world, then just move everything as the player moves. Including the other 3D ships. Maybe, whenever a 3D object is drawn, I go into perspective mode then back into 3D mode. My objects do have depth, because they tilt as they turn. Its also easier to rotate a 3D asteroid than it is to do a pre-rendered animation.

EGD Eric: Maybe you can help me out with something. I don''t know exactly what you''re asking. Could your question be asked this way: If i know the depth of my object, how do i find out the span of the x coordinates for my frustum at that depth?

This topic is closed to new replies.

Advertisement