Game Development Reference
In-Depth Information
def
draw_bounding_box
$window
.
rotate(object
.
direction, x, y)
do
w
=
@body
.
width
h
=
@body
.
height
$window
.
draw_quad(
x
-
w
/ 2
, y
-
h
/ 2
,
Gosu
::
Color
::
RED
,
x
+
w
/ 2
, y
-
h
/ 2
,
Gosu
::
Color
::
RED
,
x
+
w
/ 2
, y
+
h
/ 2
,
Gosu
::
Color
::
RED
,
x
-
w
/ 2
, y
+
h
/ 2
,
Gosu
::
Color
::
RED
,
100
)
end
end
# ...
end
Result is pretty good, we have tank shaped box, so we will be using body image dimensions
to determine our bounding box corners:
Tank's bounding box visualized
us, and we need to perform these calculations on our own. We have four points that we
want to rotate around a center point. It's not very difficult to find how to do this. Here is a
Ruby method for you:
module
Utils
# ...
def
self
.
rotate
(angle, around_x, around_y,
*
points)
result
= []
points
.
each_slice(
2
)
do
|
x, y
|
r_x
=
Math
.
cos(angle)
*
(x
-
around_x)
-
Math
.
sin(angle)
*
(y
-
around_y)
+
around_x
r_y
=
Math
.
sin(angle)
*
(x
-
around_x)
+
Math
.
cos(angle)
*
(y
-
around_y)
+
around_y