Public Group

# Designing a Game - Questions

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

## Recommended Posts

It's the weekend after midterms, one of those rare weekends when I have no homework! As such, I decided to start a game. I'll try to ask questions API-independent, though I plan to use Direct3D, and the code will be C++. The game will be 2D, top-down, and it will be more or less a simple GTA remake. Since this is my first game independent of a guide, I'm likely to have a lot of questions, and I plan on adding them as I think of them in this thread. Question 1: I'm planning on using sprites, sort of. Every person and car will actually be not a square with a texture of what it looks like, but rather the set of textured triangles that make up the shape of the object. This is because I want to animate parts of a sprite, such as rotating the tires when a car turns, or translating the feet of a person as he walks. My question is about how I can accomplish turning the tires of a car specifically, or, more generally, drawing and transforming an object that is underneath another. I thought about making my game world in several planes - that is, the ground is on the plane z=0, the feet of people and tires of car will exist on the plane z=1, and the top of the person and the top of the car will exist on the plane z=2. Then, when a car is turning, I simply apply a rotation matrix to the triangles that make up the tires, draw the transformed tires, and then draw the car on top. Doing this will eliminate the need to draw 50 different version of each car and each person in every fathomable state or position. Is this the right way to approach this? Then, if I use orthographic projection, will everything line up all right?

##### Share on other sites
Sounds like a good idea - the original 2D GTA basically did the same with the cars, rotating individual sprites rather than having about a bajillion predrawn images. I've got something similar with my shooter in that enemies can be a bunch of sprites that animate seperately.

If you draw in ortho perspective then the z coord won't make any difference to the visible size, meaning everything in the same x,y position will line up correctly. If you use the depth buffer you can then just throw everything at the graphics card and it'll get the depths right for you. However this means you have to have hard-edged polys (no semi-transparent or antialiased sprites). If you want that as well you'll have to get the drawing order right yourself (probably by just sorting on the z).

Also you probably don't want to use a rotation matrix for each sprite/poly, do the transformation manually and spit out big chunks of triangles at a time to keep the graphics card busy.

##### Share on other sites
Quote:
 Original post by OrangyTangSounds like a good idea - the original 2D GTA basically did the same with the cars, rotating individual sprites rather than having about a bajillion predrawn images. I've got something similar with my shooter in that enemies can be a bunch of sprites that animate seperately.If you draw in ortho perspective then the z coord won't make any difference to the visible size, meaning everything in the same x,y position will line up correctly. If you use the depth buffer you can then just throw everything at the graphics card and it'll get the depths right for you. However this means you have to have hard-edged polys (no semi-transparent or antialiased sprites). If you want that as well you'll have to get the drawing order right yourself (probably by just sorting on the z).Also you probably don't want to use a rotation matrix for each sprite/poly, do the transformation manually and spit out big chunks of triangles at a time to keep the graphics card busy.

Thanks for that help. But what do you mean, "do the transformations manually"?

What I imagine you meant is, instead of:
D3DXMatrixRotation(&r, ...);   // rotate tireD3DXMatrixTranslation(&t, ...);   // move tire to correct position under car (Front-Left)D3DXMatrixMultiply(&world, &r, &t);   // combine transformationsDevice->SetTransform(D3DTS_WORLD, &world);    // set transformDrawFirstTire();D3DXMatrixTranslation(&t, ...);   // second tire, move to Front-RightD3DXMatrixMultiply(&world, &r, &t);Device->SetTransform(D3DTS_WORLD, &world);DrawSecondTire();// Rear tires don't rotate, so they're part of the "car top"D3DXMatrixTranslation(&t, ...);   // Move car top to center of tires and on top of the tiresD3DXMatrixMultiply(&world, &car_rotation, &t);Device->SetTransform(D3DTS_WORLD, &world);DrawCar();

Do you mean that instead of the preceding code segment, I should do something more like this?
D3DXMatrixRotation(&r, ...);   // Rotation of front tiresD3DXMatrixTranslate(&t, ...);  // Translation of front-left tireD3DXMatrixMultiply(&m, &r, &t);D3DXVECTOR3* OriginalTireCoords = GetTireCoords();D3DXVECTOR3* TransformedTireCoords = new D3DXVECTOR3[ size_of_2_tires ];D3DXVec3TransformCoordArray(     &TransformedTireCoords[0], // output transformed tire coords to beginning of array     sizeof(D3DXVECTOR3), // stride (output stream)     OriginalTireCoords,  // pointer to source array     sizeof(D3DXVECTOR3), // stride (input stream)     &m,  // transformation matrix     number_of_coords_in_1_tire );  // number of elements in arrayD3DXMatrixTranslate(&t, ...);  // Translation of front-right tireD3DXMatrixMultiply(&m, &r, &t);D3DXVec3TransformCoordArray(     &TransformedTireCoords[start_of_second_tire], // output transformed tire coords to just beyond first tire     sizeof(D3DXVECTOR3), // stride (output stream)     OriginalTireCoords,  // pointer to source array     sizeof(D3DXVECTOR3), // stride (input stream)     &m,  // transformation matrix     number_of_coords_in_1_tire );  // number of elements in array// perhaps add the transformed coords of the car top to the TransformedTireCoords array// set up a vertex buffer for the tire coords, along with whatever other initialization work is neededDrawTires();

Or am I misunderstanding what you had in mind?

Also, is the above code snippet a sensible solution?

##### Share on other sites
I'm not familiar with D3D, but that looks like what I was meaning - lots of start-stop drawing of single polys can add a certain amount of overhead, calculating the final vertex positions into an array for batched rendering tends to be somewhat quicker. Also, you might want to be careful with using 'new' in your often-called rendering code as it's not a cheap operation, putting stuff on the stack or preallocating it would probably be a better idea.

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

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013599
×