开发者

Is it possible to make a polygon bounding box?

开发者 https://www.devze.com 2023-02-18 06:31 出处:网络
(first off, just to be sure, a hexagon is a polygon with 6 points, think honey-comb or Civilisation 5)

(first off, just to be sure, a hexagon is a polygon with 6 points, think honey-comb or Civilisation 5)

I'm making a game that uses a hexagonal grid, but I can't seem to get the bounding box right, Javas API only tells me about开发者_运维技巧 rectangular "BB" (getBounds and getBounds2D).

I've drawn the shapes and aligned them as they should be, but I want the cells to be exactly adjacent to eachother, either that the edges go into eachother or that they are right next to eachother, but never even 1 pixel between them. This is impossible to do right now when the BB is rectangular.

If this is completely impossible, maybe there's a way to reduce the components size but still draw the full thing? That way you could have a regular rectangle that spans in the middle of the hexagonal, although it leaves the two "points" useless, which is not good.

Thanks <3


A bounding box is by definition, a box (aka rectangle). So no, it is not possible to make a non-rectangle bounding box.

It is possible to make a non-rectangle window, clipping area, ... but the box that bounds such a thing will always be a "box". This is because a lot of routines do peliminary checks (overlapping, etc) on the bounding box, and then do fine grained checks on the actual shapes. Fine grained checks are computationally expensive, so a quick check (do these rectangles overlap?) can act as a filter for which candidates deserve the extra clock cycles.

That said, what you're going to do with a bounding box is an entirely different thing:

  1. If you are going to use the coordinates of the bounding box to determine if something needs to be drawn to the screen, then draw it if even part of the bounding box is visible. Then use the clipping routines (which can clip to any shape) to ensure that just the bits you want visible are shown.

  2. If you are going to use the coordinates of the bounding box to designate a click listener, then do so. Implement within the hexagon listener the extra code to determine if the click was within the hex or just within the bounding box. If it's outside the hex, then discard.

Basically without knowing a bit more about what you are trying to accomplish, it's not possible to offer a reasonable solution; however, many routines do bounding box checks first as a quick and dirty means of reducing the number of calls, and then do more detailed checks to see if the bounding box solution was a false positive (think object collision).

0

精彩评论

暂无评论...
验证码 换一张
取 消