PDA

View Full Version : [F8] Circular HitTest for Sonar/Radar



steve_b
January 24th, 2007, 08:24 PM
I am trying to make a sonar/radar that can detect when an object when it comes into its area. The sonar travels in an expanding circle, and the standard hittest obviously wont cut it. It is possible to hover an object outside the sonar range, yet still be detected (see attached file - you may have to rename is .swf). Please excuse its poor quality.

Ive done a mockup in Flash 8 with the effects but obviously I need to do this for real in actionscript to add to my confusion. I have the hitTest on the block as there maybe many objects in radar range in the final version...

So my 2 main questions are:

1) How can i make the expanding circle in actionscript to represent the sonar
2) How can I get a circular hitTest setup on the above

Any and all help appreciated :)

Steve

SacrificialLamb
January 24th, 2007, 08:43 PM
for circles i use this function i made.


drawCircle("circle", 0, 0, 100, 0x000000, 1, _root);
function drawCircle(mc, x:Number, y:Number, r:Number, col:Number, depth:Number, path:MovieClip):Void {
mc = path.createEmptyMovieClip(mc, depth);
mc.lineStyle(0, col);
mc.moveTo(x+r, y);
mc.beginFill(col);
gap = 7.2;
for (d=2; d<(50)+1; d += 2) {
ang = (7.2*d)/180*Math.PI;
stes = (7.2*(d-1))/180*Math.PI;
sr = r;
ex = x+Math.cos(ang)*r;
ey = y+Math.sin(ang)*r;
sx = x+Math.cos(stes)*r;
sy = y+Math.sin(stes)*r;
mc.curveTo(sx, sy, ex, ey);
}
endFill();
}
I would use a "a^2+b^2=c^2" distance code and test distance's against radius. You might not want early exclusion code if there are a lot of things that you are testing

steve_b
January 25th, 2007, 08:14 AM
Thanks for the response, ill look up the methods you have suggested. Will post again if I come into difficulty

steve_b
January 25th, 2007, 09:06 AM
Right, ive toiled with this to no joy. I tried applying a looped _xscale and _yscale to the circle and the circle disappears off screen, without growing. Im really sorry if this is a basic problem, I really dont have any actionscript experience...

SacrificialLamb
January 25th, 2007, 02:42 PM
Looped? As in a for loop? You don't want that. Put it in a EnterFrame some thing like.

r = 1;
maxr = 100;
onEnterFrame = function () {
drawCircle("circle", (Stage.width/2), (Stage.height/2), r, 0x000000, 1, _root);
if (r>maxr) {
r = 1;
} else {
r+=3
}
};
function drawCircle(mc, x:Number, y:Number, r:Number, col:Number, depth:Number, path:MovieClip):Void {
mc = path.createEmptyMovieClip(mc, depth);
mc.lineStyle(0, col);
mc.moveTo(x+r, y);
gap = 7.2;
for (d=2; d<(50)+1; d += 2) {
ang = (7.2*d)/180*Math.PI;
stes = (7.2*(d-1))/180*Math.PI;
sr = r;
ex = x+Math.cos(ang)*r;
ey = y+Math.sin(ang)*r;
sx = x+Math.cos(stes)*r;
sy = y+Math.sin(stes)*r;
mc.curveTo(sx, sy, ex, ey);
}
}
Put this code in a frame and it will work (I have tested it)

steve_b
January 27th, 2007, 01:32 PM
Thanks again Sacrificial_Lamb, with your help I have managed to crack this :hugegrin:

Aquilonian
January 29th, 2007, 07:09 PM
You can give a radius property to two points and find the distance of those points using the pitagorean theorem, and if this distance is less then the summ of the two radius the two circles are coliding
I dont know if this is want you want, anyway