java - Rectangle2D.contains() not properly detecting coordinates in boundaries -
i writing tower defense game intro cs2 class. have piece of code find next coordinate on map enemy unit:
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)) (i <- 0 until numaccessiblecoordinates) { val currangle = angle * 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) (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 }
"coordinates" variable stores coordinates given creep (this code snippet method in creep class).
the problem running collision detection. creep walk right through towers. (here tower.containstower method:)
def containstower(coor:(double,double)):boolean = { var flag = false (t <- maingame.towers) { if (t.tower.contains(coor._1,coor._2)) flag = true } flag }
where t.tower java rectangle2d object. method returns true when creep walk through it, , yet i'm not getting desired effect of "blocking off" coordinate contains tower (rectangle2d) object.
can shed light why i'm getting behavior?
your default case go along x
when there's no better option staying are. you'll run right tower default. should change default else, or use like
getaccessiblecoordinates.filternot( containstower ).minby{ pt => coordinate.distancebetween(pt, maingame.finishcoordinates) }
assuming there @ least 1 accessible coordinate.
by way, pathing not in general easy problem, , solution above tend leave oscillating solutions blocked creep run , forth between 2 closest spots. that's better running tower when other move take farther away are.
Comments
Post a Comment