Skip to main content
GameDev.net gamedev.net
🔒 Locked

noob dx8/9 problem

Started by 0nighthawk0 May 11, 2004 at 5:36 AM 9 replies 500+ views
Original Post
0nighthawk0
0nighthawk0
Hi all, i was wondering if you could help me. Now this may seem like a cmpletely newbie question and as i''m a bit of a noob at this that would be about right. Right, i''m currently coding a very simple breakout clone using the directx 8.1 API. I have probably the most simple problem in the world for you guys to solve, but for me my brain keeps frying when i approach it. To mention what i have done so far: I have used a vertex buffer to create the bat and mapped a texture to it, the bat works perfectly, movement v.basic animation ..the works. The problem is rendering the ball and the blocks, now your probably wondering why? Well every example i see of this game is done using pens and brushes in dx5,6 etc. This is all very well and i would love to use them but i cannot find a single example of someone creating this 2d game in dx8 or 9, with simple collision detection. Here is a possible problem, i create 10 blocks in a line by mapping a texture to a certain location on screen in my rendering code, how then do i go about destroying just one of those blocks? Where are its boundaries so i can delete it once the ball hit it? These may seem like simple problems and if so i really need help trying to solve them. Thanks in advance.
^nh^
SKREAMZ
SKREAMZ
in what format are you holding your level data in? i would sugest a 2d array

int level[30][30];

and using a value to tell you if a block is there or not like so

#define BLOCKTYPE_NONE 0
#define BLOCKTYPE_GREEN 1
#define BLOCKTYPE_RED 2
#define BLOCKTYPE_BLUE 3
#define BLOCKTYPE_WHITE 4
#define BLOCKTYPE_YELLOW 5

i hope this helps
Blaaaaa Blaaaa Blaa errrrr!!!! Bla?
0nighthawk0
0nighthawk0
ok i''ll give that a whirl and see how i do, thanks

any other suggestions on this also appreciated.
^nh^
nonpop
nonpop
A harder way which would also probably be overkilling would be to store the blocks in a linked list. Every node would then contain the bounding box of the block and other info. But since I assume you''re using fixed size blocks I''ll second the array method.
0nighthawk0
0nighthawk0
This is currently how i was creating the blocks.

//Green Blocks
for (int i = 0; i<=32;i=i+1)
{
D3DXMatrixTranslation(&matWorld,-16.0 + i ,+2.0f,0.0f);
D3DDevice->SetTransform( D3DTS_WORLD, &matWorld );
D3DDevice->SetTexture0,D3DTextureGBrick);
D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);
}

The problem with this method is the complexity of creating the collision detection code, again maybe i have just been working to long and need a break but i am going to need a way of comparing ball & block location.

At the moment as the "Green" blocks are fixed on a certain Y axis, i had thought about comparing bally axis to block yaxis, and then if true ballx to blockx, but then i have to somehow clear the block from the screen. Any ideas on an efficient method for doing this?

The array suggestion was a good idea, ut the view matrix one actually echoes kind of what i have in front of me, sorry if i didnt supply enough info before :/
^nh^
nonpop
nonpop
You need to store the level data somehow (ie. which blocks are destroyed/not destroyed). In fact I think the array and matrix suggestions are basically the same thing, only from different perspectives. Here''s how you could do it:

// Initialize all to ''true'' to indicate

// the blocks are not destroyed

bool green_blocks[32];

// Render code:

//Green Blocks

// NOTE: I changed the loop condition (i<=32) to (i<32) since

// I assumed you want to draw 32 blocks, not 33

for (int i = 0; i<32;i=i+1)
{
// Render the block only if it''s not destroyed

if (green_blocks[i])
{
D3DXMatrixTranslation(&matWorld,-16.0 + i ,+2.0f,0.0f);
D3DDevice->SetTransform( D3DTS_WORLD, &matWorld );
D3DDevice->SetTexture0,D3DTextureGBrick);
D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);
}
}

0nighthawk0
0nighthawk0
Wow thanks!, that really was a pretty simple solution now i look at it, and i''ve been racking my brains about it for nearly three days!

Thanks again, i''m sure i''ll be posting more questions very soon.

^nh^
superpig
superpig
quote:
Original post by SKREAMZ
#define BLOCKTYPE_NONE 0
#define BLOCKTYPE_GREEN 1
#define BLOCKTYPE_RED 2
#define BLOCKTYPE_BLUE 3
#define BLOCKTYPE_WHITE 4
#define BLOCKTYPE_YELLOW 5


Incidentally, I wouldn''t recommend this; you can achieve the same thing using an enum, and the result would be typesafe and wouldn''t mess with the preprocessor. (You''d still be able to convert to int if you needed to).
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
Supernat02
Supernat02
"then i have to somehow clear the block from the screen."

If you are clearing and updating the screen every frame (I assume you are, unless you wanted a cool ball trail to form), then this is kind of automatic for you if you do what nonpop suggested. The very next frame after the ball has entered that block, the block will be dead and not displayed anymore.

In this case you probably don''t need this kind of an optimization, because you aren''t storing a ton of quads in video memory. It definitely doesn''t hurt and is not a bad way to go, don''t get me wrong. I''m just giving another option that you could try if you are up for a different challenge.

You still want to batch quads according to texture. However, you could create a static vertex buffer with all of the quads in that buffer that will ever be on a screen at one time. i.e. starting in the top left corner halfway down the screen to the right side for instance, create the vertex buffer full of quads, as if the top portion of the screen is full of quads (the blocks).

Then, create an index buffer at program start. Every frame, clear the index buffer (or you could optimize this too since you know you won''t be adding blocks, but you''ll always be removing them), then batch the textured quads together by looking at your 2D array.


enum enumcolor {RED, BLUE, GREEN, YELLOW};

class carray
{
bool alive;
enumcolor color;
int startIndex;
};

BlueList->Clear();
RedList->Clear();

if (array[0][0].alive)
{
switch (array[0][0].color)
{
case BLUE:
BlueList->Add(0,0);
break;
case RED:
RedList->Add(0,0);
break;
etc...
}
}


Now you have a blue list and a red list. You loop through the lists and add indices to the index buffer based on startIndex.

Then you call one DrawIndexedPrimitive call per texture. Again, in a game so simple, this kind of optimization doesn''t help much. It would just be a good coding exercise.

Chris
Chris ByersMicrosoft DirectX MVP - 2005
0nighthawk0
0nighthawk0
I see what your saying, but unfortunatly i have already spent the entire afternoon building specific code around the earlier suggestion.

However, when i come around to rehashing this and making it better than something quite this basic i shall have a look at implementing the above suggestion.
^nh^

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.