Forum Topic: HIttesting

(116 views • 9 replies)

This topic is 1 page long.

<< < > >>
Questioning

joshwa11

Reply To Post Reply & Quote

Posted at: 6/26/09 08:20 AM

joshwa11 NEUTRAL LEVEL 04

Sign-Up: 11/03/07

Posts: 15

Hey guys, i was working on rebuilding my game and i have noticed my hittests dont seem to be terribly accurate, most of the time the hittest seems to work on the maximum perimeter, for example if i have a triangle it would still be treated as a full square, anybody got any idea how i could make it more accurate?


None

Draglino

Reply To Post Reply & Quote

Posted at: 6/26/09 08:33 AM

Draglino DARK LEVEL 03

Sign-Up: 11/07/07

Posts: 45

Are you using AS3 ? in as3 you can make hitTestPoint other then hitTestObject


None

LonLonRanch

Reply To Post Reply & Quote

Posted at: 6/26/09 08:36 AM

LonLonRanch LIGHT LEVEL 17

Sign-Up: 05/22/05

Posts: 320

I'm assuming that this is in AS2?

The problem with hitTest is that it only calculates the bounding box of the graphic and not the actual pixels. That's where specifying the pixel comes in handy. Check out the liveDocs for more information. Basically, with hitTest you can do:

hitTest(x, y, shapeFlag) : Boolean
hitTest(target) : Boolean

so instead of checking the bounding box, you can do a hitTest check for a specific point, like:

hitTest(box._x, box._y, true);

But either way, a triangle is one of the hardest objects to run a successful hitTest with, rectangles and circles are much easier. If you need something more powerful I'd search google for hitTest classes already written that could do this for you. If you use AS3 Troy Gilbert wrote an excellent PixelPerfectHitTest collision class.


None

joshwa11

Reply To Post Reply & Quote

Posted at: 6/26/09 08:42 AM

joshwa11 NEUTRAL LEVEL 04

Sign-Up: 11/03/07

Posts: 15

Afriad im using AS2, would i have to specify every point around the triangle? Trying to avoid using alot of code, minimising lag you see


None

LonLonRanch

Reply To Post Reply & Quote

Posted at: 6/26/09 08:55 AM

LonLonRanch LIGHT LEVEL 17

Sign-Up: 05/22/05

Posts: 320

Unfortunately doing hitTests on objects that are not rectangles or circles takes much more work than most people want to do.

If you're still in AS2 try this: clicky


None

joshwa11

Reply To Post Reply & Quote

Posted at: 6/26/09 09:15 AM

joshwa11 NEUTRAL LEVEL 04

Sign-Up: 11/03/07

Posts: 15

checkForCollision(Goblin_Swordsman_1,man ,alphaTolerance);
if(Collision == true){
_root.text == "hit"
}else{
}

That was my first attempt and it aint working, any idea how thats wrong?


None

LonLonRanch

Reply To Post Reply & Quote

Posted at: 6/26/09 09:36 AM

LonLonRanch LIGHT LEVEL 17

Sign-Up: 05/22/05

Posts: 320

Did you download the file and read the documentation? I'm guessing it's going to take a little effort to implement it into your file.


None

joshwa11

Reply To Post Reply & Quote

Posted at: 6/26/09 09:43 AM

joshwa11 NEUTRAL LEVEL 04

Sign-Up: 11/03/07

Posts: 15

A couple of the files in the download appear to be a format i cant open but yes ive read through the documentation, the demo's also aren't fully functioning, for example CDDemo2 doesn't work like it does on the page, did a similar thing happen when you downloaded it?


None

LonLonRanch

Reply To Post Reply & Quote

Posted at: 6/26/09 10:00 AM

LonLonRanch LIGHT LEVEL 17

Sign-Up: 05/22/05

Posts: 320

I haven't actually used it, I used the AS3 one I mentioned earlier which worked for me, but I don't actually know much about this one, just looked like something you'd be interested in.


None

asgrunt

Reply To Post Reply & Quote

Posted at: 6/26/09 03:31 PM

asgrunt NEUTRAL LEVEL 01

Sign-Up: 05/26/09

Posts: 164

checkForCollision is a static of the CollisionDetection class. So you need to use it like so:

import com.gskinner.sprites.CollisionDetection;
...
CollisionDetection.checkForCollision(mc1 ,mc2);

You will either get a rectangle with the overlapping area or null if no collision did occur between the mcs with the instance names mc1 and mc2.

Of the many files in Skinners' zip you only need the CollisionDetection.as.


All times are Eastern Standard Time (GMT -5) | Current Time: 03:52 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!