PDA

View Full Version : Collision Detection



owi
09-13-2007, 08:01 PM
Hi! What do you guys use for Collision Detection? I have sevral books on Flash game programming, and this topic is really badly covered.

For example in the book "Game Development with Actionscript" you find this:
---
Q. Collision detection is a big part of physics, why didn't you cover it in detail?

A: A. There are plenty of other books that cover this subject in detail. Try Macromedia Flash MX Game Programming, published by Premier Press.

---

if it's covered it barly mention the hitTest function, at the moment I use the collision detection described in emanuelle's tutorial. http://www.emanueleferonato.com/2006/11/18/flash-game-creation-tutorial-part-2/

It's ok, but do you guys have any other tutorials on the subjet? And what do you guys use for collision detection?

If your wondering what I'm making and why I need a more advanced Collision Detection...


I'm making a small one-screen/on screen racing game. Very much like Super Sprint
http://www.thelegacy.de/pics/screen/0/001-SuperSprint-CPC.gif

I have 8 tracks of my own design up and going, but they're all made out of squares, so that the in house collision detection in flash will work. If you look at the pic of the Super sprint track you'll see why a squared collision detection wont work.

I guess I'll have to hard code the collision detection for a track like that?

NTD
09-15-2007, 01:19 PM
Hi,

Totally agree. The hit test method is, to be polite, ... weakly defined in Actionscript up to version 8. I can't say for CS3.... don't have it. The standard hitTest method allows you to check the bounding box of the moiveclip or with the shapeFlag parameter, the graphic contents of the movieclip. However, the graphic content method does not work as expected. It will return misfired hitTests for shapes other than a square or circle.You have to be creative when using the method. One method many use for complex hit shape tests is to use multiple small movieclips lining the border of the target shape then using an array,a for loop, and the hitTest method to check the clips lineing the border of the complex shape. You can use the getBounds method with the hitTest to more precisely define unsual shaped objects.... such as a lake....arrow key motion...

http://ntdesigns.net/quickDemo/boatBounds.swf



this.onEnterFrame = function() {
with (_root.boat) {
if (_root.map.hitTest(getBounds(_root).xMax, _y, true)) {
_x -= 5;
Speed = 0;
}
if (_root.map.hitTest(getBounds(_root).xMin, _y, true)) {
_x += 5;
Speed = 0;
}
if (_root.map.hitTest(_x, getBounds(_root).yMax, true)) {
_y -= 5;
Speed = 0;
_root.character._y -= 5;
}
if (_root.map.hitTest(_x, getBounds(_root).yMin, true)) {
_y += 5;
}
}
}


I even tried to write a prototype hitTest method that would incorporate odd sized shapes with a bit of math, but it turned out less precise than I was hoping...

http://ntdesigns.net/demoFiles/hitTestProto.swf

For your project, I think that several movieclips lining the border and a for loop should do the trick.


for (i=0; i<totalWalls+1; i++) {
if (_root["wall"+i].hitTest(_root.car)) {
trace(["wall"+i]+" was hit");
speed = -speed;
}
}

... the above in an enterframe event...

Hope it helps
NTD