Skip to main content
gamedev.net
🔒 Locked

Perspective grid

Started by Guy Meh Mar 6, 2009 at 8:21 PM 2 replies 750+ views
Original Post
Guy Meh
Guy Meh
I'm trying to draw a 2D perspective grid. My results so far is here. As you can see, I've figured out how to do the vertical lines, but I don't know how to do the horizontal lines. Lines near the top should be closer together than the lines at the bottom, but I don't know what distance function to use. I've read of a method that works by finding the grid's center, dividing the grid into upper and lower halves, and recursively finding the centers of these new halves to determine the positions of the horizontal lines. The problem is that the number of grid spaces on a side has to be a power of two, which is either too little or too much for my purposes.
Zakwayda
Zakwayda
Is there any reason you can't use a traditional pipeline for this, of the sort used by graphics APIs such as OpenGL and Direct3D? That would probably be the most flexible way to do it.
Guy Meh
Guy Meh
Because I'm using a 2D library for a 2D game. :P

Suffice it to say that drawing the grid tilted is mainly for show. ;)
Aschrak
Aschrak
I don't know exactly, how you make the grid, but I would represent the grid like in the image
Point D is the bottom of the grid, the vector u is a unit vector that points to the top of the grid. (in my image u isn't a unit vector, it's already u*d)
Point A is the eye point and the red lines are the coordinate system (the origin is in the middle of the picture). The vertical line is the y-axis, the horizontal line is the z-axis.

The coordinates of points on the green grid line can be computed with

P = D + u*t

where t is the distance between P and d. For example:

G = D + u * (1 * d)

where d is the regular distance between the horizontal grid lines.

now we can use the similar triangles ADH and AD'H' to compute the y coordinate of our intersection point D' because the distance D'H' is the y coordinate(in my picture y of D' is negative).

y = x/xD * yD

where:
y = D'H'
x = AH'
yD = DH
xD = AH

Soooo, I hope this can help you and that my English wasn't too bad, so you can understand everything.

Topic Locked

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

Sign in to reply to this topic.