{"id":770,"date":"2009-09-17T23:25:58","date_gmt":"2009-09-17T13:25:58","guid":{"rendered":"http:\/\/www.vectorstorm.org\/?p=770"},"modified":"2009-09-18T00:22:59","modified_gmt":"2009-09-17T14:22:59","slug":"collision-bugs-are-the-best-bugs","status":"publish","type":"post","link":"https:\/\/www.vectorstorm.com.au\/2009\/09\/17\/collision-bugs-are-the-best-bugs\/","title":{"rendered":"Collision bugs are the best bugs"},"content":{"rendered":"
So that problem I mentioned yesterday — having roads end up being selected as though they were in front of objects which they were actually behind — eventually turned out to be a nasty little leak of temporary collision result data which was being generated as part of an optimisation.<\/p>\n
Basically, when I’m trying to figure out what you’re pointing your cursor at, here’s what I do: \u00a0I create an imaginary line which starts at the camera, and travels out in a direction through the world which is determined by the 2D position of your cursor within the game window. \u00a0I then test that line against all the polygons in the terrain, all the polygons in the roads, all the polygons in the buildings, all the polygons in the toons, etc. \u00a0For each polygon that the line hits, I keep track of where it hit, and how far from the camera the polygon was. \u00a0Whichever polygon got hit closest to the camera,\u00a0that’s the one that I consider you to be pointing at.<\/p>\n
Now, doing this sort of test-every-polygon collision test takes a lot of CPU power, and so I’ve made a lot of optimisations to avoid testing polygons which I don’t absolutely need to test. \u00a0One of these optimisations (which is being used on everything but the terrain) is to create an invisible “bounding box” around each object in the world, and before testing any of the polygons in those objects, I test whether the line hits the bounding box at all, and if not, then I know that I don’t have to test any of the polygons inside the box; \u00a0the line didn’t hit the box, so it couldn’t possibly hit any of the polygons inside the box.<\/p>\n
The bug that I fixed today was that anything which used this “bounding box” optimisation (which is almost everything) wasn’t calculating “point where I hit the polygon” and “distance from that point to the camera” as they were supposed to; \u00a0they were calculating “point where I hit the polygon” and “distance from where I hit the bounding box to the camera”. \u00a0Most of the current placeholder graphics in the game are cubes which exactly match their bounding boxes, and so this bug wasn’t noticeable on them; \u00a0the only exceptions were the roads, which undulate and have bounding boxes which are much larger than the roads themselves, often entirely surrounding the objects which travel on the roads. \u00a0Since the collision system was accidentally using the distance-to-bounding-box instead of distance-to-polygon, the roads were taking priority over those objects which looked like they were closer, but were actually encompassed within the roads’ bounding box.<\/p>\n
This bug would have affected buildings and all sorts of other things as well, once full procedural building generation is implemented. \u00a0But it’s fixed now.<\/p>\n
Also, my head hurts. \u00a0Too much debugging of collision code will do that.<\/p>\n","protected":false},"excerpt":{"rendered":"
So that problem I mentioned yesterday — having roads end up being selected as though they were in front of objects which they were actually behind — eventually turned out to be a nasty little leak of temporary collision result data which was being generated as part of an optimisation. Basically, when I’m trying to…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[24,25],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/po9WK-cq","_links":{"self":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/770"}],"collection":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/comments?post=770"}],"version-history":[{"count":0,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/770\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/media?parent=770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/categories?post=770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/tags?post=770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}