Returned directory
The collision check of this module is only limited to the detection of points and rectangles, (parallel) Rectangles and rectangles, points and circles, and circles. Therefore, this module is also very simple and can be directly combinedCodeLet's take a look:
Point and rectangle:
/**
* Collision between points and rectangles
**/
This. Col_point_rect =Function(Pointx, pointy, rectobj ){
Return(Pointx> rectobj. X & pointx <rectobj. Right | pointy> rectobj. Y & pointy <rectobj. Bottom );
}
When a point is inside a rectangle, we think they produce a collision.
Rectangle and rectangle:
/**
* Collision between rectangles
**/
This. Col_between_rects =Function(Rectobja, rectobjb ){
Return(Rectobja. right> rectobjb. X & rectobja. right <rectobjb. right | rectobja. x> rectobjb. X & rectobja. x <rectobjb. right) & (rectobja. bottom> rectobjb. Y & rectobja. bottom <rectobjb. bottom | rectobja. Y <rectobjb. bottom & rectobja. bottom> rectobjb. Y ));
}
In short, the collision of a rectangle depends on the position of the four points of the rectangle relative to the other rectangle. In the futureArticleIn sprite objects, we usually obtain the rectangles of the sprite object to detect the collision between objects and other objects. Therefore, the collision between a rectangle and a rectangle is also the most commonly used collision.
/**
* Collision between points and circles
**/
This. Col_point_circle =Function(Pointx, pointy, circleobj ){
Return(Math. Pow (pointX-circleObj.x), 2) + math. Pow (pointY-circleObj.y), 2) <math. Pow (circleobj. R, 2 ));
}
If the point is inside the circle, the point and the circle are considered to have a collision.
Circle and circle:
/**
* Collision between a circle and a circle
**/
This. Col_between_circles =Function(Circleobja, circleobjb ){
Return(Math. pow (circleobja. x-circleObjB.x), 2) + math. pow (circleobja. y-circleObjB.y), 2) <math. pow (circleobja. R + circleobjb ). r, 2 ));
}
The collision between a circle and a circle depends on the comparison between their center distance and the sum of their radius.