tags:

views:

95

answers:

2

I have created some houses in my XNA world using code like this in my loadContent method:

house.Model = Content.Load<Model>("Models\\oldgreekhouse"); 
house.Position = new Vector3(0, 0, 0);
house.Rotation = new Vector3(0f, 0f, 0f);
house.Scale = 0.125f;

Then I call the Dram() method to make it shown in my virtual city. However,I have problem is that I can go inside the building which I dont want,how could I do this?I know something called BoundBox,but not sure whether it will work for my case.

Thanks a lot.

+2  A: 

Check for the position of the "house" and for the position of your "human" like

if(human.rectangle.X + human.rectangle.width >= house.rectangle.x && human.rectangle.X <= house.rectangle.x + house.rectangle.width)
{
// the human entered here, just disallow any walking action here
}

Note, that you should add also the checking for the Y-cordinates. Also you can use some bounding box, I also heared about, but never used. Hope this will help you.

cevik
sorry, thought about 2D... I should stop working and go sleepin'
cevik
+2  A: 

For buildings a bounding box would be a fairly good approximation for collision detection. The best way to calculate a bounding box is by using a custom content processor.

see: http://www.harding.edu/dsteil/xna/notes/Making%20Bounding%20Boxes%20For%20Models.htm

Once you have a model loaded with a bounding box you need to translate the the boxes to positions that correspond to your object locations. You can the test with some code like

humanBoundingBox.Intersects(houseBoundinBox)

Grant