# 3D TILE COLLISION

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

## Recommended Posts

Hi guys

Ive been working on a 3D version of a 2D tile map and using the terrain in riemers flightsim tutorial(link below) as a starting foundation. Ive modified the terrain to have multiple textures and  I also added a car model on the terrain using a getheight method(code below)the last thing Im trying to figure out is how to recognize what set of tiles the car is colliding with(ex. 0 would be  grass, 1 would be sand, etc,..) in order to use that info to create a dust  or grass debris particle impact effect, if anyone could provide any advice it would be much appreciated.

Thankyou

The GETHEIGHT Method

   public float GetExactHeightAt(float xCoord, float zCoord)
{
bool invalid = xCoord < 0;
invalid |= zCoord < 0;
invalid |= xCoord > floorPlan.GetLength(0) - 1;
invalid |= zCoord > floorPlan.GetLength(1) - 1;
if (invalid)
return 10;

int xLower = (int)xCoord;
int xHigher = xLower + 1;
float xRelative = (xCoord - xLower) / ((float)xHigher - (float)xLower);

int zLower = (int)zCoord;
int zHigher = zLower + 1;
float zRelative = (zCoord - zLower) / ((float)zHigher - (float)zLower);

float heightLxLz = floorPlan[xLower, zLower];
float heightLxHz = floorPlan[xLower, zHigher];
float heightHxLz = floorPlan[xHigher, zLower];
float heightHxHz = floorPlan[xHigher, zHigher];

bool cameraAboveLowerTriangle = (xRelative + zRelative < 1);

float finalHeight;
if (cameraAboveLowerTriangle)
{
finalHeight = heightLxLz;
finalHeight += zRelative * (heightLxHz - heightLxLz);
finalHeight += xRelative * (heightHxLz - heightLxLz);
}
else
{
finalHeight = heightHxHz;
finalHeight += (1.0f - zRelative) * (heightHxLz - heightHxHz);
finalHeight += (1.0f - xRelative) * (heightLxHz - heightHxHz);
}

return finalHeight;
}


What I have so far

 protected override void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Escape))
this.Exit(); // Exit with ESC key

float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

float treshold = 0.0f;
float terrainHeight = GetExactHeightAt(hummer.Position.X, -hummer.Position.Z);
if (hummer.Position.Y < terrainHeight + treshold)
{
Vector3 newPos = hummer.Position;
newPos.Y = terrainHeight + treshold;
hummer.Position = newPos;
}

base.Update(gameTime);

}



portion modified so far

 public void SetUpVertices()
{
int cityWidth = floorPlan.GetLength(0);
int cityLength = floorPlan.GetLength(1);

List<VertexPositionNormalTexture> verticesList = new List<VertexPositionNormalTexture>();
for (int x = 0; x < cityWidth; x++)
{
for (int z = 0; z < cityLength; z++)
{
int imagesInTexture = 11;
int imagesInTexture1 = 3;
if (floorPlan[x, z] == 0)
{
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z), new Vector3(0, 1, 0), new Vector2(0, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(0, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture, 1)));

verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(0, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture, 1)));
}

if (floorPlan[x, z] == 1)
{
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z), new Vector3(0, 1, 0), new Vector2(0, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(0, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture1, 1)));

verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(0, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture1, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture1, 1)));
}
}
}

cityVertexBuffer = new VertexBuffer(device, verticesList.Count * VertexPositionNormalTexture.SizeInBytes, BufferUsage.WriteOnly);

cityVertexBuffer.SetData<VertexPositionNormalTexture>(verticesList.ToArray());
}



and the initialize method

 protected override void Initialize()
{
graphics.PreferredBackBufferWidth = 500;
graphics.PreferredBackBufferHeight = 500;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
Window.Title = "Riemer's XNA Tutorials -- 3D Series 2";
floorPlan = new int[,]
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,0,0,0,1,0,0,0,0,0,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,0,0,0,1,0,0,0,0,1},
{1,0,1,1,0,0,0,0,1,1,0,0,0,1,1},
{1,0,0,0,0,0,0,0,1,1,0,0,0,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};

base.Initialize();
}


a visual example of what Im trying to do is in the youtube link below, in the video with each footstep taken the character emits

particles based from whatever texture is being impacted.

• 18
• 29
• 11
• 23
• 16