简体   繁体   中英

How do I HitTest two rotating objects properly? (A way to avoid bounding boxes)

I took an intro level flash course in college this semester and our final task was to make a mini-flash game. I had to make a pipe-dream type game where there are a number of levels, and in each level you have to align the pipes so that the water flows and then you can pass to the next level.

I successfully made the first level, but upon making the second level, where I placed a lot of curved pipes (by curved pipes I mean the attached image: ![Curved Pipe]: ( http://imgur.com/mwpXAMn ) ) I discovered that the method I use to decide when a level is complete is not working properly.

I was using HitTestObject, basically, I was testing whether 2 objects, Pipe_1, and Pipe_2, were intersecting. If all pipes intersected in the correct way, then procession to the next level is granted.

The problem with this I discovered is that flash has bounding boxes for movie clips you make, and that HitestObject uses bounding boxes to test for hits. Therefore, when you rotate a leftpipe so that it does not touch a straight pipe on screen, the bounding boxes still touch and it returns "collision" when in fact it is not actually touching on screen.

I looked up and found that you can use HitTestPoint but I can't figure out how to somehow make dynamic variables (that change upon rotation of object) that store one or two specific points on the leftpipe, say the two ends of it.

Once If I figure out how to get these values into a variable correctly, then I can figure out how to do HitTestpoint.

Also, I know of the LocaltoGlobal function but no matter what I try it keeps coming up with:

"Scene 1, Layer 'Layer 1', Frame 1, Line 30 1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.geom:Point."

meaning I don't know the correct code to store an x and ay coordinate as dynamic variables.

edit: ok since a person asked, I hunted this piece of code off the web and this is the one I was trying to play around with but to no avail:

How to use HitTest for 2 rectangles, r1 is rectangle 1 r2 is rectangle 2.

var r1width:Number = 135.0; //width of retangle 1 whith rotation 0
var r2width:Number = 93.0; //width of retangle 2 whith rotation 0

var p1:Object = {x:(r1width/2), y:(r1width/2)};
var p2:Object = {x:(-r1width/2), y:(r1width/2)};
var p3:Object = {x:(-r1width/2), y:(-r1width/2)};
var p4:Object = {x:(r1width/2), y:(-r1width/2)};

r1.localToGlobal(p1);
r1.localToGlobal(p2);
r1.localToGlobal(p3);
r1.localToGlobal(p4);

var p5:Object = {x:(r2width/2), y:(r2width/2)};
var p6:Object = {x:(-r2width/2), y:(r2width/2)};
var p7:Object = {x:(-r2width/2), y:(-r2width/2)};
var p8:Object = {x:(r2width/2), y:(-r2width/2)};

r2.localToGlobal(p5);
r2.localToGlobal(p6);
r2.localToGlobal(p7);
r2.localToGlobal(p8);

if((r2.hitTest(p1.x, p1.y, true))||(r2.hitTest(p2.x, p2.y, true))||(r2.hitTest(p3.x,   
p3.y, true))||(r2.hitTest(p4.x, p4.y, true)))
{
    trace('collision');
}

if((r1.hitTest(p5.x, p5.y, true))||(r1.hitTest(p6.x, p6.y, true))||(r1.hitTest(p7.x, 
p7.y, true))||(r1.hitTest(p8.x, p8.y, true)))
{
trace('collision');
}

I did not write this code and it does not work. I'm not sure what "Object" is because I've never used it before, I'm assuming in this case it's sort of acting like a coordinate pair.

Also, this code is to hittest 2 rectangles, whereas I'm using an L-shaped pipe, so the x/y calculation would be quite different I imagine.

This code above gives the same error that I posted before:

Implicit coercion of a value with static type Object to a possibly unrelated type flash.geom:Point.

and it gives it first on line r1.localToGlobal(p1);

除了使用Object之外,还需要像这样使用Point:

var p1:Point = new Point(r1width/2, r1width/2);

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