# Drawing cubes in a 2D system

This topic is 3031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 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 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.

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 34
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631701
• Total Posts
3001808
×