简体   繁体   中英

How to create a circle collision detection? c++

Hi I would like to know how to create a circle collision which also acts as a sensor. For example, when the player enter the enemy's circle radius, the enemy will start chasing the player. Based on what I have tried, I am only using a collision box which is the RECT SpriteRect . But that wont be accurate enough for the circle. I was thinking about using vector by using D3DXVECTOR2 but I am not good at transformation. Please assist me on how can I achieve this.. 在此处输入图片说明

Is point (xa, ya) within the circle at (xc, yc) with radius r?

Remember pythagoras? - It determines the distance between two points, and all you need to determine if something is within a circle with a given radius, is to determine if the distance to the center of the circle, is less than the radius:

bool IsPointInCircle(float xa, float ya, float xc, float yc, float r)
{
   return ((xa-xc)*(xa-xc) + (ya-yc)*(ya-yc)) < r*r;
}

Collision detection between two circles is just as easy, you just give 'r' as the sum of the radius of your two circles. Becuase, that'll be the distance between the two centers when the circles touch.

To do collision detection among squares and circles, you need to calculate distance from line to point. Circles are pretty much the easiest shapes to do collision detection on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM