I have noticed that sketching a box and filling a box leads to different sized boxes. This is a bit of a headache when dealing with hit detection. for example if I have a circle that I am sketching and a rectangle I am drawing, I want to know if those two intersect for purposes of animation. Is there anyway around this? A simple solution would be to set the line width to zero, but that is not an option.
You need to add lineWidth into your calculations for stroked objects.
Calling fillRect(0,0,60,60)
gets you a 60x60
box starting at (0,0)
, but if you are stroking you must add half of the line width to each side.
so with a lineWidth of 6
calling strokeRect(0,0,60,60)
really gets you a 66x66
box that starts at (-3,-3)
.
The math can be slightly different due to mitering but for most applications you can ignore that.
One simple solution that may or may not fly with your program is to, for every filled object, call fillRect AND strokeRect. The object will become slightly larger, but it will be consistently sized with your stroked object. The validity of this approach is of course entirely up to how you've written things so far.