0
我寫一個塔防遊戲我的簡介CS2類座標。我有這樣一段代碼查找映射爲一個敵人單元上的下一個座標:Rectangle2D.contains()不能正確檢測邊界
def findNextCoordinate(): (Double, Double) = {
val numAccessibleCoordinates = 8
val angle = 360/numAccessibleCoordinates
val pixelsPerFrame = speed/MainGame.fps //hypotenuse
val currentCoor = coordinates
def getAccessibleCoordinates(c: (Double, Double)): Array[(Double, Double)] = {
val points = Array.fill(numAccessibleCoordinates)((0.0, 0.0))
for (i <- 0 until numAccessibleCoordinates) {
val currAngle = angle * i
val x = math.cos(currAngle) * pixelsPerFrame
val y = math.sin(currAngle) * pixelsPerFrame
points(i) = ((currentCoor._1 + x), (currentCoor._2 + y))
}
points
}
val accessiblePoints = getAccessibleCoordinates(coordinates)
var nextCoor = accessiblePoints(0)
var distFromFinish = Coordinate.distanceBetween(coordinates, MainGame.finishCoordinates)
for (pt <- accessiblePoints) {
val ptDistFromFinish = Coordinate.distanceBetween(pt, MainGame.finishCoordinates)
var containsTower = Tower.containsTower(pt)
println(containsTower)
if (ptDistFromFinish <= distFromFinish && !containsTower) {
nextCoor = pt
distFromFinish = ptDistFromFinish
}
}
nextCoor
}
「座標」是存儲用於給定的蠕變(此代碼段是在蠕變的方法的座標變量類)。
我遇到的問題是碰撞檢測。爬行穿過我的塔樓。 (這裏是Tower.containsTower方法:)
def containsTower(coor:(Double,Double)):Boolean = {
var flag = false
for (t <- MainGame.towers) {
if (t.tower.contains(coor._1,coor._2)) flag = true
}
flag
}
其中t.tower是一個java Rectangle2D對象。當蠕變穿過它時,此方法返回true,但我沒有得到有效「阻塞」包含塔(Rectangle2D)對象的任何座標的效果。
任何人都可以提供一些線索,爲什麼我得到這個行爲?