SnkTest
January 17, 2018, 6:06am
#1
Hi,
I have to check if my point is contained in my spite. So i wrote a something like that:
if(my_sprite->getBoundingBox().containsPoint(point))
my_sprite has a shape of rectangle and is rotating in another function.
But the problem is that getBounding box doesnt return what i want. I will describe my issue:
zhangxm
January 17, 2018, 6:42am
#2
The comment of Node::getBoundingBox()
is
Returns an AABB (axis-aligned bounding-box) in its parent's coordinate system.
So the behavior is correct. What you need is obb
, which is missed. You should implement it yourself.
SnkTest
January 17, 2018, 9:00am
#3
is there any reference link or hint for this?
zhangxm
January 17, 2018, 9:27am
#4
The source code are
Rect Node::getBoundingBox() const
{
Rect rect(0, 0, _contentSize.width, _contentSize.height);
return RectApplyAffineTransform(rect, getNodeToParentAffineTransform());
}
Rect RectApplyAffineTransform(const Rect& rect, const AffineTransform& anAffineTransform)
{
float top = rect.getMinY();
float left = rect.getMinX();
float right = rect.getMaxX();
float bottom = rect.getMaxY();
Vec2 topLeft = PointApplyAffineTransform(Vec2(left, top), anAffineTransform);
Vec2 topRight = PointApplyAffineTransform(Vec2(right, top), anAffineTransform);
Vec2 bottomLeft = PointApplyAffineTransform(Vec2(left, bottom), anAffineTransform);
Vec2 bottomRight = PointApplyAffineTransform(Vec2(right, bottom), anAffineTransform);
float minX = min(min(topLeft.x, topRight.x), min(bottomLeft.x, bottomRight.x));
float maxX = max(max(topLeft.x, topRight.x), max(bottomLeft.x, bottomRight.x));
float minY = min(min(topLeft.y, topRight.y), min(bottomLeft.y, bottomRight.y));
float maxY = max(max(topLeft.y, topRight.y), max(bottomLeft.y, bottomRight.y));
return Rect(minX, minY, (maxX - minX), (maxY - minY));
}
It returns minX and minY instead to get AABB. You can implement the same way without using minX and minY.
SnkTest
January 18, 2018, 1:22pm
#5
thank you for guide .
http://www.jeffreythompson.org/collision-detection/poly-rect.php
this link is very helpful for it.
i got my solution