2d point to 2.5d Point

833 views Asked by At

I've made a game that lets a player move around in 2d space. The current view is from above, looking directly down at the player. What I'd like to add is the ability to transition the view from above into a 2.5d perspective. Basically I want to take the top view and turn it into a horizon. Think of it like going from the view in Frogger to a more FPS style view.

So how do I take the X and Y coordinates and convert them into a pseudo 3d point? I've seen some examples on how to turn a 2d point into a 3d point, but I don't know how to apply that to something that's completely 2d.

Anybody have any examples or code that could help me accomplish this?

3

There are 3 answers

2
Lucas Reis On

You can set the height of the item by object. Something like "the floor has height 0; a character has height 1" and so on.

This is the simplest you can get; for more complex height mappings I think you have no choice but have a 3D model for each item on screen.

3
comingstorm On

For a 2.5d projection (otherwise known as an orthographic projection), the simplest method is to choose a 2d vector for the third dimension, and use it as weights to add the depth to the width and height:

if third-dimension weighting parameters are:  vec2d( a, b ),
then:  vec3d( x, y, z )  -->  vec2d( x + a*z, y + b*z )

The exact choice of ( a, b ) is up to you, but you might want to start with ( 0.5, 0.5 ). In any case, it doesn't make sense for the absolute value of either a or b to be larger than 1.0.


To go back the other way, from 2d to 3d, is necessarily ambiguous, depending on the z coordinate:

a given 2d point:  vec2d( x, y )
can correspond to any 3d point of the form:  vec3d( x - a*z, y - b*z, z )

If your user clicks a particular ( x, y ) location on the screen, this may correspond to a location on several different objects (at different z depths). For purposes like this, you may want to maintain a bounding box in screen space for any objects that are clickable in this way....

4
raam86 On

What your are looking for is a translation from R^2 to R^3. You should apply a transformation for each point in space seeing it as a vector

Matrix multiplication

(I would suggest making that an interface)

Now I assume each object in your game has some kind of (x,y) pair of coordinates. You are working in R^2 so you basic vectors for the whole space are e1(0,1) and e2 (1,0) now you want to translate that to 3d, remembering this is linear you can scale it be any scaler (number).

So this is given by

Now in order to generate 'z' you will have to do matrix multiplication.

In r^3 e1 will be (0,1,0) e2 will be (1,0,0) and e3 will be (0,0,1)

this will set your view point to ground level similar to the answer above me.

Now check this wolfram out I set out completely arbitrary values

http://www.wolframalpha.com/input/?i=matrix&a=C.matrix-_Calculator.dflt-&a=FSelect_**MatricesOperations-.MatrixOperations-&a=*FP.MatricesOperations.matricesop-matrixmult&f4=%7B%7B79%2C4%7D%2C%7B8%2C8%7D%7D&f=MatricesOperations.theMatrix1%7B%7B79%2C4%7D%2C%7B8%2C8%7D%7D&f5=%7B%7B1%2C0%2C5%7D%2C%7B0%2C1%2C5%7D%7D&f=MatricesOperations.theMatrix2_%7B%7B1%2C0%2C5%7D%2C%7B0%2C1%2C5%7D%7D

Matrix multiplication is a simple algorithm (in the original sense) which you can find online.

An which was returned from this function as a 3d vector can then be drawn.

Formula from wikipedia

CC WIKI

You do the same disregarding the blue part.

for x'(100,0)*(1,0,0) y'(0,100) *v(0,1,0)

Easily found on google

google