[Cson original] HTML5 game framework cngamejs development record (collision detection module)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.