Reputation: 743
Im developing smaall Windows Game using Xna game studio..
My question is i got two objects like Ship and The Land Tile... Imagine that ship has rectangle call shiprectangle and land has rectangle called landrectangle.. rectangle represents the current position of the object..
The land tiles are loded in random places in map.. i just want to know if ship is collied in land tile and ship have to ignore the land and go to another derection..
Simply the meaning is i dont want to see ship is going on lands... Thats the idea...
private void HandleLandCollition()
{
foreach (LandTile landtile in landtiles)
{
if (ship.rectangle.Intersects(landtile.rectangle))
{
//Code Here
}
}
}
Upvotes: 0
Views: 511
Reputation: 1117
If you want to create realistic collision response you should use Farseer Physics library. It's Box2D port, optimized for XNA.
http://farseerphysics.codeplex.com/
If not you can use @vinzBad solution.
Upvotes: 1
Reputation: 689
A basic version could be like this:
Via the Rectangle.Intersect - Method you get the rectangle where both of the rectangles intersect.
Now you can move the ship away from the landtile using the values from the intersection-rectangle.
In the shown case you just can subtract the Width and Height - values from the X and Y values of the Ship, to move the ship away from the landtile.
Upvotes: 1