0

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 } } }

1
  • Hey Efran.. i just want to know hot to turn the ship otherway if the ship is collied with the land tile.. those ship and land tile object are seperate.. im new to this field sir.. Commented Oct 17, 2012 at 5:50

2 Answers 2

1

A basic version could be like this:

Via the Rectangle.Intersect - Method you get the rectangle where both of the rectangles intersect.

A picture of two intersecting rectangles.

Now you can move the ship away from the landtile using the values from the intersection-rectangle.

A picture of two intersecting rectangles. With arrows showing how to move one rectangle out of the other one.

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.