Sign in to follow this  
Dolmur

Drawing cubes in a 2D system

Recommended Posts

I'm working on a mobile game for BlackBerry, and the game field contains cubes seen from a fixed perspective. So far I have been drawing the cubes by simply drawing a bitmap at the correct location, and the bitmap was created by taking the coordinates of the vertexes from a source picture. However, I now need to create frames of a cube rolling from one face to another, all the while maintaining a fixed perspective. I think the best way to approach this is to calculate where the vertexes should be and then use built in drawLine functions to create the cube around those points. What I am stuck on is how to calculate those points in multiple frames during a rotation. Any advice and/or examples would be appreciated.

Share this post


Link to post
Share on other sites
How familiar are you with 3D graphics theory / matrix math? You take your vertices in 3D and rotate them by multiplying by a rotation matrix. Then you bring the vertices to "view space" which means the vertex positions from the camera's point of view (multiply vertices by a view matrix. Then you take those vertices in view space and multiply them by a projection matrix to get them in 2D screen space.


EDIT: are you coding in Java or C++? I'm sure Java has a 3D api called JSR-184. Is OpenGL ES not available on Blackberry?

Share this post


Link to post
Share on other sites
I'm not familiar with 3D graphics theory, but I do have a pretty good understanding of matrix math in general including affine transforms. I'm programming in Java, but JSR-184 is not supported on BlackBerry, and OpenGL was just introduced and thus is only supported on the newest OS. Since my game is targeting older devices as well, I can't use it.

Could you point me to any tutorials or samples of the process you described?

Thanks!

Share this post


Link to post
Share on other sites
A quick search on Google gave me this result:

http://robertokoci.com/world-view-and-projection-matrix-unveiled/

Keep looking up those concepts in search engines... "world matrix", "view matrix", "projection matrix". It also helps to try out an existing 3d api like openGL and DirectX. Once you know how to build your own world, view, and projection matrices, you'll be all set for what you want to do. When I get home tonight, I'll try to reply with a more detailed example.

Share this post


Link to post
Share on other sites
At heart, perspective transformation can be summarized as "divide x and y by z." You do some affine transformations to rotate your cube about its center and then move it so that the camera is now at the origin; then you divide by z. That's basically it.

Note that there are two conventions for transformation matrices which are transposes of each other; one treats vectors as column and the other treats them as rows. gsamour's link uses the less-common row convention. Just transpose everything to make it agree with other sources you read.

I think the graphics forum FAQ has a number of good entry-level references.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this