Thursday, December 24, 2009

Programming, simulate a 3D terrain using a 2D axis. Have trouble rotating camera to view whole terrain

I want to simulate a 3D terrain using a 2D axis. I can convert 2D points to 3D points but I have trouble rotating the camera to look at the terrain at a X degree angle from a point above the terrain. The following pseudo code shows my logic.





// converting 2d to 3d points


// this algorithm assumes the


// origin starts from the middle


// not top left with a z axis that increases


// as it comes out towards the camera





//it's distance away from the origin along the z axis


camera = 256





//Corner A


x1 = -10;


y1 = 0;


z1 = 10;





//Corner B


x2 = 10;


y2 = 0;


z2 = 10;





//Corner C


x3 = 10;


y3 = 0;


z3 = -10;





//Corner D


x4 = -10;


y4 = 0;


z4 = -10;





//Looks like this, yahoo answers may screw


//the formatting though


// D-------------------C


// |############|


// |############|


// |############|


// |############|


// A-------------------B





//plotting point A,B,C,D


//------------------------------------


//Corner A


camera_distance = camera - z1;


new_x1 = x1 * camera / camera_distance;


new_y1 = y1 * camera / camera_distance;


pixel_set(new_x1, new_y1);





//Corner B


camera_distance = camera - z2;


new_x2 = x2 * camera / camera_distance;


new_y2 = y2 * camera / camera_distance;


pixel_set(new_x2, new_y2);





//Corner C


camera_distance = camera - z3;


new_x3 = x3 * camera / camera_distance;


new_y3 = y3 * camera / camera_distance;


pixel_set(new_x3, new_y3);





//Corner D


camera_distance = camera - z4;


new_x4 = x4 * camera / camera_distance;


new_y4 = y4 * camera / camera_distance;


pixel_set(new_x4, new_y4);


//------------------------------------





The trouble occurs now. At the moment we're looking at the front of a 3D square with no hight that goes out towards the negative direction of the z axis. Because objects get smaller the further there away from the camera suggests that the interval DC can not be seen because it's hidden by the interval AB. What I want to do is move the camera up and then rotate the camera X degree down so I can view the whole square from an areal view, like Age Of Empire's kind of thing. Any suggestions would be greatProgramming, simulate a 3D terrain using a 2D axis. Have trouble rotating camera to view whole terrain
Hello,





You are on the right track concerning the conversion from 2D space to 3D space. It seems you are trying to create a 3D workspace where you want to manipulate your object. I would recommend you to use Transformation matrices. Transformation matrices which are widely used in 3D Graphics will allow you to do what you wanted quickly and efficiently.





You can, rotate, scale, shear, reflect, project, etc your shape in 3D space easily using a transformation matrix. I wont explain how to do each manipulation since it is heavily documented on the net if you do a simple search.





Take a look a wikipedia for quick reference regarding some simple transformations:


http://en.wikipedia.org/wiki/Transformat鈥?/a>





The one you would need is shearing in the direction you would like. You would need to translate all the points wrt to the transformation matrix you are currently using.





Good luck!

No comments:

Post a Comment