# DirectX - Issue with tiles on a 2D map.

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

## Recommended Posts

Hi,

I am a starting to learn to code with DirectX in C++, and have hit a bug for which I am unable to find a solution.

In this part of my code, I am attempting to fill a vector of objects (models with x,y,z positions, rotations and scaling) to create a 2D map of square tiles (First block of code).

Once these have been put in the vector, I call a loop to render the objects one by one.

After using the debugger, I can assure you that the X and Y values are correct at the rendering stage, and that all tiles have a pointer to their respective model.

In order to try to debug this problem, I have added a piece of code to make it so that squares with even X values are stones, and squares with odd X values are grass.

In the image #1, I have changed the values of my loop so that the map tiles X and Y values have an interval of [-4,3].

In the image #2, I have changed the values of my loop so that the map tiles X and Y values have an interval of [-3,3].

I have also tried increasing the jump in my loops so that they increase by 2 each time, but it didn't fix the issue.

Looking at this problem, I can only think that I am missing a key piece of information about DirectX that is causing this bug.

EDIT:

In the event that somebody would like to have a look at the full code, my project is on Github.

The crucial pieces of information are (I think) in the WorldClass Initialize() function, or in the GraphicsClass Render() function.

https://github.com/Zilmerx/GameProjectFinal

Code #1 :

	// Test #2.
for (int y = -3; y <= 3; y++) // -3 to 3, 7 values.
{
for (int x = -3; x <= 3; x++) // -3 to 3, 7 values.
{
std::unique_ptr<ObjectClass> obj;
if (x % 2 == 0) // Is Even
{
obj = std::make_unique<ObjectClass>(model1);
}
else            // Is Odd
{
obj = std::make_unique<ObjectClass>(model2);
}

obj->SetPosition(x, y);

obj->Initialize();

m_Map.push_back(std::move(obj));
}
}

Image #1

Image #2

Edited by Zilmerx

##### Share on other sites

Maybe you could use such boolean expression:

if (x % 2 == y % 2)

##### Share on other sites
std::unique_ptr<Object> obj;
if (x % 2 == y % 2) // Is Even
{
obj = std::make_unique<Object>(model1); // Stones
}
else                // Is Odd
{
obj = std::make_unique<Object>(model2); // Grass
}


Produces this with and interval of [-4,3].

Produces this with and interval of [-3,3].

After having read some more, I have a feeling that this could be related to occlusion culling.

Maybe the "black" squares are in fact being culled because they are seen as being behind the other ones ?

Edited by Zilmerx

##### Share on other sites

so you're trying to draw a checkerboard?

as in:

i=0

j=0

for x=0 x<width, x++

for y=0 y<width y++

{

if i==0

{

vector[j]=(x,y,green)

i=1

}

else

{

vector[j]=(x,y,gray)

i=0

}

j++

}

Edited by Norman Barrows

##### Share on other sites

For a checkerboard:

if ((x & 1) ^ (y & 1))

DoThis ();

else DoThat ();

Because x and y are starting negative you'll want to offset them before doing the bitwise ops; assuming starting at -3 it becomes ((x + 3) & 1).

##### Share on other sites

For a checkerboard:

bitwise operands - the slick way to do it!

##### Share on other sites

Some pretty sexy solutions to make a checkerboard pattern, but the problem I have is that it makes a checkerboard pattern without me actually wanting to have one  :lol:.

Re-reading my first post, I notice that I didn't give a lot of information as far as what the issue in those images is.

Let me clarify, and update with some progress that I have made in regards as to isolating the issue :

I am trying to make a 2d map of tiles like this one (that I have managed to create with a lot of duct-tape : Putting the objects twice in a row in my vector of objects).

If we look at the piece of code that I have pasted in my first post, this is the pattern that should appear on my screen, but I get the results posted in my first post.

Only one tile on two gets rendered correctly in my image, which is why doubling each object in my vector makes it "work..".

Obviously this is not a very valid solution, so I am looking for a way to only have one object for each tile.

While digging for the bug I have found that my problem is probably situated at the level of my shader or my way of using my objects.

In order to render my tiles, I make many "objects" which contain a pointer to a unique model, and store them in a vector, like this.

Then I loop through the vector and render each model with the texture attached to it.

Is this a valid technique ?

Edited by Zilmerx

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633697
• Total Posts
3013406
×