Difference between revisions of "Bounds"
Jump to navigation
Jump to search
(Created page with "In Mini Micro, the Bounds class represents a rectangular bounding box. It is used for hit-testing and collision detection, especially with Sprites. {{stub}}") |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | <c>Bounds</c> is a built-in class in the [[Mini Micro]] API. It represents a rectangular bounding box, which can be tested for overlap with other bounding boxes, or for containing a point. This is often used for collision-checking and hit-testing with [[Sprite|Sprites]]. | |
− | { | + | === Methods and properties used with Bounds objects === |
+ | |||
+ | Use these methods on Sprite instances, created with <c>[[new]] Bounds</c>. | ||
+ | |||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! Method or Property !! Purpose | ||
+ | |- | ||
+ | | [[Bounds.x]] || horizontal center of the bounds | ||
+ | |- | ||
+ | | [[Bounds.y]] || vertical center of the bounds | ||
+ | |- | ||
+ | | [[Bounds.width]] || bounding box width | ||
+ | |- | ||
+ | | [[Bounds.height]] || bounding box height | ||
+ | |- | ||
+ | | [[Bounds.rotation]] || bounding box rotation (degrees counter-clockwise) | ||
+ | |- | ||
+ | | [[Bounds.corners]] || returns bounding box corners as a list of <c>[x,y]</c> pairs | ||
+ | |- | ||
+ | | [[Bounds.overlaps]](''b'') || return whether this bounds overlaps bounds ''b'' | ||
+ | |- | ||
+ | | [[Bounds.contains]](''x'', ''y'') || return whether the bounds contains the given point | ||
+ | |- | ||
+ | |} | ||
+ | |||
+ | === Usage Notes === | ||
+ | |||
+ | The [[Bounds.contains|contains]] method may be given ''x'' and ''y'' as two separate arguments; or it may be given an <c>[x, y]</c> list; or it may be given any map containing "x" and "y" keys (such as [[mouse]]). | ||
+ | |||
+ | == Example == | ||
+ | |||
+ | The following example defines a Bounds at the center of the screen, and then enters a loop which draws the bounds yellow when the mouse is over it, and gray when it is not. | ||
+ | |||
+ | <ms>b = new Bounds | ||
+ | b.x = 480 | ||
+ | b.y = 320 | ||
+ | b.width = 200 | ||
+ | b.height = 100 | ||
+ | b.rotation = 45 | ||
+ | while true | ||
+ | if b.contains(mouse) then | ||
+ | gfx.drawPoly b.corners, color.yellow | ||
+ | else | ||
+ | gfx.drawPoly b.corners, color.gray | ||
+ | end if | ||
+ | yield | ||
+ | end while</ms> | ||
+ | |||
+ | |||
+ | See also: [[How to detect a click on a sprite]] | ||
+ | |||
+ | [[Category: Mini Micro]] |
Latest revision as of 16:31, 8 July 2021
Bounds
is a built-in class in the Mini Micro API. It represents a rectangular bounding box, which can be tested for overlap with other bounding boxes, or for containing a point. This is often used for collision-checking and hit-testing with Sprites.
Methods and properties used with Bounds objects
Use these methods on Sprite instances, created with new Bounds
.
Method or Property | Purpose |
---|---|
Bounds.x | horizontal center of the bounds |
Bounds.y | vertical center of the bounds |
Bounds.width | bounding box width |
Bounds.height | bounding box height |
Bounds.rotation | bounding box rotation (degrees counter-clockwise) |
Bounds.corners | returns bounding box corners as a list of [x,y] pairs
|
Bounds.overlaps(b) | return whether this bounds overlaps bounds b |
Bounds.contains(x, y) | return whether the bounds contains the given point |
Usage Notes
The contains method may be given x and y as two separate arguments; or it may be given an [x, y]
list; or it may be given any map containing "x" and "y" keys (such as mouse).
Example
The following example defines a Bounds at the center of the screen, and then enters a loop which draws the bounds yellow when the mouse is over it, and gray when it is not.
b = new Bounds
b.x = 480
b.y = 320
b.width = 200
b.height = 100
b.rotation = 45
while true
if b.contains(mouse) then
gfx.drawPoly b.corners, color.yellow
else
gfx.drawPoly b.corners, color.gray
end if
yield
end while
See also: How to detect a click on a sprite