Jump to content
  • Advertisement
Sign in to follow this  
v0dKA

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by OrangyTang
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.


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

What I imagine you meant is, instead of:

D3DXMatrixRotation(&r, ...); // rotate tire
D3DXMatrixTranslation(&t, ...); // move tire to correct position under car (Front-Left)
D3DXMatrixMultiply(&world, &r, &t); // combine transformations
Device->SetTransform(D3DTS_WORLD, &world); // set transform
DrawFirstTire();

D3DXMatrixTranslation(&t, ...); // second tire, move to Front-Right
D3DXMatrixMultiply(&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 tires
D3DXMatrixMultiply(&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 tires
D3DXMatrixTranslate(&t, ...); // Translation of front-left tire
D3DXMatrixMultiply(&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 array

D3DXMatrixTranslate(&t, ...); // Translation of front-right tire
D3DXMatrixMultiply(&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 needed

DrawTires();



Or am I misunderstanding what you had in mind?

Also, is the above code snippet a sensible solution?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!