Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hello everyone I have this problem that I have tried everything I could think of. The problem: I am making a 2D game with parallax effect but I am using 3d space so am not simulating the parallax but letting the perspective projection take care of it for me. now the problem i have my own game editor where I design the levels, in this editor I use just images and I set a Z value for each layer. however I want the layers to show in the game engine exactly as I set them in the game editor, in short I want perspective projection to do parallax but without changing their scale or offset/position.

obvious solution is to scale them up and offset them but the problem how to calculate their offset? with scaling I tried object->Scale(object.Z/view.Z) and seems to return them to their real size but their positions are still wrong. I tried object->setPositionX(object->getPosition().x*(object.Z/view.Z)) and seems to be aligned except they all seems shifted.

I have tried unprojecting and tried to convert from world matrix to screen matrix and find some ratios and so on.

If anyone have any idea or anyway how this could be done in an elegant/mathematical way , I will be most grateful.

Thank you all in advance.

share|improve this question
1  
Could you add a screenshot or something, I'm not exactly sure what you currently have and what you want to achieve. X/Y should be the same as in your editor and Z depends on your projection parameters. You shouldn't need to scale or offset anything –  Archy Jan 28 '13 at 12:44
    
why handling those layers as 3d objects when you want precisely a 2D behaviour ? Handle the 3D part as 3D and 2D part... as 2D ! –  GameAlchemist Jan 28 '13 at 12:55
    
thanks guys for the answers, but what i want to achieve here is to pre-calaculate the required size of an image that if it was to be projected it would give the real image size as in the game editor. –  user2017987 Jan 28 '13 at 13:09
    
I'm still not entirely sure what the problem is. You want to know the size and position of a xy plane that fills a specific screen area for a given depth value? –  Archy Jan 28 '13 at 13:27
    
It seems to me that instead of perspective projection you could use an ortho projection with a rotating view axis. But a screenshot would definitely help. –  Sam Hocevar Jan 28 '13 at 13:36
show 1 more comment

1 Answer

The easiest way to do that is to use an orthogonal projection when rendering, but do your own perspective divide on the origin of the objects.

If you want to use a perspective project when rendering, you'll need to reverse the effects of the assumed original perspective projection used when editing, and apply the new perspective projection for the current camera when playing.

In your editor, you have a camera. This camera transforms from the object coordinates into screen coordinates. Now, in your editor, this is an orthogonal camera, but you want to pretend that it actually was a perspective camera. If you take the screen coordinates, run them backwards through the camera transform for the perspective camera, you get new object coordinates out. This transform is a transform that you want to apply to your objects.

What happens when you render:

screen_position = screen_from_world_matrix * world_position

What you want to happen in your transform pipeline, is add another few steps, to transform between these various spaces: playscreen, playworld, editscreen, and editworld. editscreen has the same coordinates as the original orthogonal editor camera, and the pretend perspective editor camera, as that's the space where we'll be doing the "trick" of pretending that a different camera is being swapped out.

playscreen_position =
    playscreen_from_playworld_matrix *
    playworld_position

playworld_position =
    playworld_from_editscreen_matrix *
    editscreen_from_editworld_matrix *
    editworld_position

playworld_from_editscreen_matrix is the inverse of editscreen_from_playworld_matrix, which happens to be the screen_from_world_matrix of the pretend camera!

The good news is that all of these matrixes can be concatenated into one:

playscreen_from_editworld_matrix = 
    playscreen_from_playworld_matrix *
    playworld_from_editscreen_matrix *
    editscreen_from_editworld_matrix

Or if you just want to apply the editworld-to-playworld transform:

playworld_from_editworld_matrix =
    playworld_from_editscreen_matrix *
    editscreen_from_editworld_matrix

Then transform your objects using that matrix!

share|improve this answer
    
what i want is to pre scale and position the layers before the game loop start in such a way that those scaled and translated images would appear after applying the perspective projection to them as in the game editor "their real size". if that is not clear i will try to make some diagram –  user2017987 Jan 28 '13 at 15:14
    
could you explain in more details i read your post many times but i cant understand. thanks alot :). –  user2017987 Jan 28 '13 at 16:17
    
It's elementary linear algebra. Basic matrix math. Read up on how matrices are used to transform coordinate spaces, and how they can be composed to transform coordinates from one space to another. Then come back with a more specific question if you still don't understand. –  ccxvii Jan 28 '13 at 16:25
    
i didnt ask u to explain the math to me i asked u explain your post. i know math and i know linear algebra and i know how matrices used to convert from one space to another and i know that u dont understand my question. –  user2017987 Jan 28 '13 at 16:34
    
Okay, I tried to do a step by step explanation. Hope that helps. –  ccxvii Jan 28 '13 at 17:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.