Page 1 of 1

questios about an isometric game

Posted: Sun Apr 28, 2013 6:55 pm
by rokit boy
i have 2 questions

1 is "layers" or z_index. how can i make 1 sprite draw behind or infront of another given sprite.

2nd is how would i make a 2d coords to 3d coords function?

i have 1 function called coords3dto2d which is this:

Code: Select all

function coords3dto2d(x,y,z)
	return (x-y)*tilesize/2,(x+y)*tilesize/2/2-z*tilesize/2+(love.keyboard.isDown(" ") and math.random(-2,2) or 0)
end
how would i make 2d coordinates like 25.2,51 back to 3d coordinates like 1,2,1

if there is any more info i need to add please tell me

Re: questios about an isometric game

Posted: Sun Apr 28, 2013 9:53 pm
by Plu
Regarding the first, as far as I know draw calls overwrite older calls, so in order to draw things in the right order, start drawing stuff with the lowest Z-value and work up from there and they should appear properly.

Regarding transforming 2d coordinates to 3d coordinates... you don't. There's data missing in the 2d coordinate so you won't be able to properly calculate a coordinate in a 3d space from them. You'll need to internally keep track of both sets of coordinates for each object and use the 3d coordinate for calculation and the 2d coordinate for drawing.

Re: questios about an isometric game

Posted: Mon Apr 29, 2013 8:08 pm
by rokit boy
Plu wrote:Regarding the first, as far as I know draw calls overwrite older calls, so in order to draw things in the right order, start drawing stuff with the lowest Z-value and work up from there and they should appear properly.

Regarding transforming 2d coordinates to 3d coordinates... you don't. There's data missing in the 2d coordinate so you won't be able to properly calculate a coordinate in a 3d space from them. You'll need to internally keep track of both sets of coordinates for each object and use the 3d coordinate for calculation and the 2d coordinate for drawing.
for the last one i mistyped, i meant to turn something like 25.5, 51 to 1,2. without the z.
i have tried so many times

Re: questios about an isometric game

Posted: Mon Apr 29, 2013 9:05 pm
by micha
From your code the formula from 3d to 2d is

Code: Select all

x2 = (x3-y3)*tilesize/2
y2 = (x3+y3)*tilesize/2/2
(x2,y2) are the 2d-coordinates, (x3,y3) are the 3d-coordinates.

You can solve this for x3 and y3 by multiplying the first by 2/tilesize and the second by 4/tilesize.
You get (this are equations, not lua-code)

Code: Select all

2*x2/tilesize = x3-y3
4*y2/tilesize = x3+y3
and then add/subtract

Code: Select all

4*y2/tilesize+2*x2/tilesize = 2*x3
4*y2/tilesize-2*x2/tilesize = 2*y3
and then make it a bit nicer:

Code: Select all

x3 = (2*y2+x2)/tilesize
y3 = (2*y2-x2)/tilesize

Re: questios about an isometric game

Posted: Tue Apr 30, 2013 6:33 pm
by rokit boy
micha wrote:From your code the formula from 3d to 2d is

Code: Select all

x2 = (x3-y3)*tilesize/2
y2 = (x3+y3)*tilesize/2/2
(x2,y2) are the 2d-coordinates, (x3,y3) are the 3d-coordinates.

You can solve this for x3 and y3 by multiplying the first by 2/tilesize and the second by 4/tilesize.
You get (this are equations, not lua-code)

Code: Select all

2*x2/tilesize = x3-y3
4*y2/tilesize = x3+y3
and then add/subtract

Code: Select all

4*y2/tilesize+2*x2/tilesize = 2*x3
4*y2/tilesize-2*x2/tilesize = 2*y3
and then make it a bit nicer:

Code: Select all

x3 = (2*y2+x2)/tilesize
y3 = (2*y2-x2)/tilesize
thanks!