Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Advanced Collision Detection Algorithms
#4
Most of this stuff is described in math(geometry?) textbooks. Especially circle collisions and stuff.

I wrote this for a crappy android(https://play.google.com/store/apps/detai...Hobo&hl=en) game I made a (long)while back: http://slexy.org/raw/s21Bsi6YvL


The hobo was a circle. Cars were polygons. To check if a circle overlaps with a polygon I just iterated over each vector in the polygon and checked if it(the vector) overlapped with the circle. There is a tiny optimisation where before checking all the vectors in the polygon I first check if the the circle is inside the rectangle at all (since you can do that pretty fast).

Code:
        for (int x = 0; x < 4; x++)
        {
            List<Vehicle> cars = this.cars[x];
            int len = cars.size();
            for (int i = 0; i < len; i++) {
                Vehicle car = cars.get(i);
                if (OverlapTester.overlapCircleRectangle(car.bounds, hobo.circleBounds)) {
                    for (int j =0; j < car.boundingPolygon.length - 1; j++)
                    {
                        if (OverlapTester.testCircleSegment(hobo.circleBounds, car.boundingPolygon[j], car.boundingPolygon[j+1]))
                        {
                            hobo.hitCar();
                            listener.hit();
                            car.hit();
                        }
                    }
                }
            }
        }
    }

Code might be terrible, but it worked :).

Edit: Looking over that code, you should stop iterating over the vectors which make up the polygon if you've already found an intersection. God damn it I suck at coding T_T
[Image: doty7Xn.gif]

10 ʏᴇᴀʀs sɪɴᴄᴇ ɪʀᴄ ɢᴏᴏᴅ.ɪ ᴡᴀʟᴋ ᴛʜʀᴏᴜɢʜ ᴛʜᴇ ᴇᴍᴘᴛʏ sᴛʀᴇᴇᴛs ᴛʀʏɪɴɢ ᴛᴏ ᴛʜɪɴᴋ ᴏғ sᴏᴍᴇᴛʜɪɴɢ ᴇʟsᴇ ʙᴜᴛ ᴍʏ ᴘᴀᴛʜ ᴀʟᴡᴀʏs ʟᴇᴀᴅs ᴛᴏ ᴛʜᴇ ɪʀᴄ. ɪ sᴛᴀʀᴇ ᴀᴛ ᴛʜᴇ sᴄʀᴇᴇɴ ғᴏʀ ʜᴏᴜʀs ᴀɴᴅ ᴛʀʏ ᴛᴏ sᴜᴍᴍᴏɴ ᴛʜᴇ ɢᴏᴏᴅ ɪʀᴄ. ɪ ᴡᴀᴛᴄʜ ᴏᴛʜᴇʀ ɪʀᴄ ᴄʜᴀɴɴᴇʟs ʙᴜᴛ ɪᴛ ɪs ɴᴏ ɢᴏᴏᴅ. ɪ ᴘᴇsᴛᴇʀ ᴢᴏʀᴛ ᴀɴᴅ ᴛʀʏ ᴛᴏ ʀᴇsɪsᴛ ʜɪs sᴇxɪɴᴇss ʙᴜᴛ ɪᴛ ɪs ᴀʟʟ ᴍᴇᴀɴɪɴɢʟᴇss. ᴛʜᴇ ᴇɴᴅ ɪs ɴᴇᴀʀ.ɪ ᴛʜᴇɴ ᴜsᴜᴀʟʟʏ ʀᴇᴀᴅ sᴏᴍᴇ ᴏʟᴅ ɪʀᴄ ʟᴏɢs ᴀɴᴅ ᴄʀʏ ᴍʏsᴇʟғ ᴛᴏ sʟᴇᴇᴘ.


Reply
Thanks given by: A-Man


Messages In This Thread
RE: Advanced Collision Detection Algorithms - by Boop - 07-07-2014, 04:56 PM



Users browsing this thread: 1 Guest(s)