2012-10-17 135 views
0

林發展smaall的Windows遊戲..碰撞檢測中使用XNA XNA遊戲工作室

我的問題是我有兩個對象,如船舶和土地瓷磚......試想一下,船上有矩形呼叫shiprectangle和土地稱爲矩形landrectangle .. rectangle表示對象的當前位置..

地圖瓷磚被隨機放置在地圖中..我只想知道船是否在地塊瓷磚和船隻必須忽略土地,去以另一種方式..

只是意思是我不想看到船正在土地上...這就是想法...

private void HandleLandCollition() { foreach (LandTile landtile in landtiles) { if (ship.rectangle.Intersects(landtile.rectangle)) { //Code Here } } }

+0

你的問題是什麼? –

+0

嘿Efran ..我只是想知道如果船舶與陸地瓷磚相處還是很熱的話可以轉船..那些船舶和陸地瓷磚物體是分開的..即時新的這個領域先生.. – tharindlaksh

+0

爲什麼不使用Box2D ? –

回答

1

基本版本可能是這樣的:

經由Rectangle.Intersect - 你得到的方法其中兩個矩形的相交矩形。

A picture of two intersecting rectangles.

現在你可以遠離landtile使用從路口矩形值移動船。

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

在圖示的情況下,你只可以減去寬度和高度 - 從X和船舶的Y值值,以使船從landtile路程。

1

如果你想創建逼真的碰撞響應,你應該使用Farseer Physics庫。它是Box2D端口,針對XNA進行了優化。

http://farseerphysics.codeplex.com/

如果不是,您可以使用@vinzBad解決方案。