[Game] mathematical and physical algorithms in Game Development: Collision Detection of circles and balls

Source: Internet
Author: User

1. Circle Collision Detection

Condition of circle collision:
When two circles are tangent or intersection, we think that the two circles should begin to collide.


Mathematical representation:

Computer Implementation:

Collision between two circles
Bool collisionbetweencircles (circle C1, circle C2) <br/>{< br/> bool hit; <br/> float distanceofcenter = SQRT (POW (c1.center. x-c2.center.x), 2) + <br/> POW (c1.center. y-c2.center.y), 2); <br/> float distanceofradius = c1.radius + c2.radius; <br/> If (distanceofcenter <= distanceofradius) <br/>{< br/> hit = true; <br/>}< br/> else <br/>{< br/> hit = false; <br/>}< br/> return hit; <br/>}

 

2. Ball collision

Collision Condition of the ball:
When two balls are tangent or intersection, we think the two balls should begin to collide.

Mathematical representation:

Computer Implementation:

Collision between two balls
Bool collisionbetweensphere (sphere S1, sphere S2) <br/>{< br/> bool hit; <br/> float distanceofcenter = SQRT (POW (s1.center. x-s2.center.x), 2) + <br/> POW (s1.center. y-s2.center.y), 2) + <br/> POW (s1.center. z-s2.center.z), 2); <br/> float distanceofradius = s1.radius + s2.radius; <br/> If (distanceofcenter <= distanceofradius) <br/>{< br/> hit = true; <br/>}< br/> else <br/>{< br/> hit = false; <br/>}< br/> return hit; <br/>}From http://www.cnblogs.com/HelloCG/archive/2009/01/14/1375573.html

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.