Skip to main content
GameDev.net gamedev.net
🔒 Locked

[SOLVED] About perspective: 2D foreground with 3D background

Started by wondersonic2 Jun 8, 2010 at 4:53 AM 5 replies 2.9k views
Original Post
wondersonic2
wondersonic2
Hello,
For the (open source) 2D game I'm working on, I wish to add some 3D backgrounds with perspective!

You can see this effect here:
at the 3rd minute and 17th second: a 3D corridor with perspective.

My game being a 2D shoot'em up, I already have pixel perfect (2D) collision detection. So I'm planning to have the corridor ground drawn in 2D (useful for collision detection) but the background will be a 3D object (3 textured quads).

So far, I've successfully implemented orho2D and 3D perspective switch in my render method but now I wish to make these 2 elements (2D corridor ground and 3D background) move at the same speed and here comes my problem...

Do you have any idea of the maths involved behind this? I guess, it's all about projection setup and efficient speed ratio between 2D and 3D world?

After working on it during 2 days, I wonder if the simplest way wouldn't be to implement everything in 3D and to enhance my collision detection to work with 3D objects by clipping z-coordinates (display object surfaces only where z is 0). If so, what are the opengl commands to perform that?

Thanks for your comments! I really need some light here :)

Regards,
WS

[Edited by - wondersonic2 on June 15, 2010 3:34:15 AM]
Zakwayda
Zakwayda
I think the simplest solution would probably be to make the game fully 3-d, and just have the '2-d' part of the game all take place in the same plane (more or less, at least).

I don't know what method you're using for collision detection, so I can't really comment directly on that. Whatever method you're using though, I'd be surprised if it couldn't be adapted to work alongside a 3-d perspective projection.
wondersonic2
wondersonic2
Quote:
Original post by jyk
I think the simplest solution would probably be to make the game fully 3-d, and just have the '2-d' part of the game all take place in the same plane (more or less, at least).

I don't know what method you're using for collision detection, so I can't really comment directly on that. Whatever method you're using though, I'd be surprised if it couldn't be adapted to work alongside a 3-d perspective projection.


Thanks for your reply.

My collision detection process is explained here: http://code.google.com/p/tf4r/wiki/PixelPerfectCollisionDetection

Now, I've seen with a friend who told me that implementing everything in 3D is maybe harder than 2D foreground + 3D background because in this case, and regarding the previous method for collision detection, I'll have to manage the rendering of the 3D background for common rendering and for collision detection (2 differents things). For example, for collision detection, I could use plane clipping but this clips the vertices (am I right?) since afterwards, I don't get anything displayed (and thus no collision possible); instead I would require some "pixel plane clipping" but I don't know how to do it now T-T.

wondersonic2
wondersonic2
One idea, I just had: If I can't make the 3D background move at the speed of the 2D foreground tiles... I'm sure I can make the 2D foreground tiles move at the speed of the 3D foreground.

I'll try tomorrow to implement this :)
DensitY
DensitY
Quote:
Original post by jyk
I think the simplest solution would probably be to make the game fully 3-d, and just have the '2-d' part of the game all take place in the same plane (more or less, at least).


I Echo this very much.

It'll be easier to render your 2d objects as billboard quads with a fixed depth (Y = 0, I normally have Z pointing up). You can treat the collision etc the same as before (no change basically!). All you're changing is how its drawn.

Your 3d artists can just work around the 2d game field being on Y = 0 and model around it. As a bonus they can create things like 3d tunnels for you to fly through and it'll all render correctly, without any special tricks.



wondersonic2
wondersonic2
Quote:
Original post by DensitY
Quote:
Original post by jyk
I think the simplest solution would probably be to make the game fully 3-d, and just have the '2-d' part of the game all take place in the same plane (more or less, at least).


I Echo this very much.

It'll be easier to render your 2d objects as billboard quads with a fixed depth (Y = 0, I normally have Z pointing up). You can treat the collision etc the same as before (no change basically!). All you're changing is how its drawn.

Your 3d artists can just work around the 2d game field being on Y = 0 and model around it. As a bonus they can create things like 3d tunnels for you to fly through and it'll all render correctly, without any special tricks.


Hmmm and how do you then implement collision detection with say some 3D walls? :)

Indeed, that's the point here ;) If you have some ideas, don't hesitate :)
wondersonic2
wondersonic2
Ok, I've finally found some way to do that.

First of all the 3D perspective view must be set as this:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float) SCREEN_HEIGHT / (float) SCREEN_WIDTH, 0f, 100.0f);

The important thing here is the z near value: 0f

Thus if the objects/camera move only on the X and/or Y axis, the dimension of objects in a given z plan are always the same size.

Now during collision detection phase, I just draw the parts of 3D objects that can collide with 2D objects.

For a ground square (with perspective), I can draw with limited z depth:

normal rendering: draw depth from 1 to -1
inside collision detection method: draw depth from 1 to 0.5

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.