Forums

ForumsProgramming Forum

How to make a perfect hitTest in as2?

Posted May 29, '11 at 5:39pm

PixelSmash

PixelSmash

578 posts

Oseb: Your code isn't ok, what you're effectively doing is setting variables in your hittest coding - _root._x = 200. Depending on the outcome of setting this it will return either true or false I believe, but not any usable Number. Either remove the =200, or try getting your x and y values from somewhere else

 

Posted May 29, '11 at 8:18pm

arobegamr

arobegamr

130 posts

I think I know what you're trying to do, Oseb, but if you want to detect collision between the object and the coordinate (200,200), then you need to remove the _root._x=

Just have this as you're code:
onClipEvent(enterFrame){
if(this.hitTest(200,200,true)){
this._y-=5
}

 

Posted May 30, '11 at 11:05am

Oseb

Oseb

29 posts

Thanks arobegamr!
But i have problem with the tird line: this._y-=5 if i add this then when my character reach the point he will be bounce back and if i hold the button then he will hit the wall perfectly, now i want to do the same. I mean don't dounce back just hit it.

 

Posted May 30, '11 at 11:13am

Oseb

Oseb

29 posts

onClipEvent(enterFrame){
if(this.hitTest(200,200,true)){
this._y-=5
}

Now i want this code with only y cordinate in. Then how is it writen?
Because i deleted the first 200 but then the second one became the firts one (its became from Y to X coordinate). I want that my char wont go under Y 200 (its like a floor).

 

Posted May 30, '11 at 11:57am

Programpro

Programpro

588 posts

onClipEvent(enterFrame){
if(this.hitTest(_root._x=200,_root._y=200,true)){
this._y-= 5;
}
}


Okay, firstly, _root is simply a Movie Clip. It refers to the stage itself. You know how you can place symbols within symbols? Well, all of the flash game's symbols are being placed inside _root. So if you say _root._x = 200, you're going to move EVERYTHING over by 200, because you're changing the origin (anchor point) of the stage.

I think you may have meant "this._x", or just "_x"; in most cases both mean the same thing, and refer to the x-coordinate of which movie clip your function is written for. Or better yet, just use numbers:

this.hitTest(200,200,true);

That will check against the point at x=200, y=200.

Also, what specifically are you trying to collide? Sometimes, a bounding circle (a circle around the object that collides with other objects) can be MUCH better than a bounding box. It's also much easier to write yourself and much more efficient:

Say that every object has a radius r that you assign them. This is the radius of their bounding circle. To check whether two bodies overlap, all you need to do is check the distance between them against the sum of their radii; if the radii add up to more than the distance between them, then there's no way for them not to overlap. Sorry, this diagram should make it clearer:

https://lh6.googleusercontent.com/-2o3uJx1a8U0/TeO7mUWdpCI/AAAAAAAAAA8/DWpFsTksgH4/BoundingCircle_Diagram.jpg

You could also set the radius up arbitrarily as half of the width of the MovieClip, so that you don't have to define variables. If you had one MovieClip named my_mc (I should say "if you had one reference to a MovieClip", but that's more technical Object-Oriented talk) and another named enemy_mc, the collision code within the my_mc object would look like:

onClipEvent(enterFrame) {

if(Math.sqrt((_x-_root.enemy_mc._x)^2 + (_y-_root.enemy_mc._y)^2) > (_width/2 + _root.enemy_mc._width/2)) {
//do this stuff if we overlap
}

}


This is pretty sloppy code; some easy optimizations would be to use ( )*( ) instead of ( )^2, because the exponent operator is slower than the multiplication one. Also, instead of ( )/2 + ( )/2 it could have been (( ) + ( ))/2. I just wrote it out so it would be easier to read.

So yeah, this uses a simple Pythagorean distance equation and checks it against the sum of their "radii". Circular bounding-boxes can provide some of the easiest and smoothest collisions you'd need, although they don't work in all occasions. If you want me to continue expounding on their use, let me know!
 

Posted May 30, '11 at 1:04pm

Oseb

Oseb

29 posts

http://kepfeltoltes.hu/view/110530/ma_www.kepfeltoltes.hu_.jpg


I want that my blue circle stops on the black wall. If my wall intance name is wall and my character's is char. Then with this code how is it sounds like?

onClipEvent(enterFrame) {
if(Math.sqrt((_x-_root.wall_char._x)^2 + (_y-_root.wall_char._y)^2) > (_width/2 + _root.wall_char._width/2)) {
_root.char._y-= 5;
}
}

this?

 

Posted May 30, '11 at 1:12pm

Programpro

Programpro

588 posts

Then with this code how is it sounds like?

onClipEvent(enterFrame) {
if(Math.sqrt((_x-_root.wall_char._x)^2 + (_y-_root.wall_char._y)^2) > (_width/2 + _root.wall_char._width/2)) {
_root.char._y-= 5;
}
}

this?


Sorry, sorry, I was unclear. My code works only for circle-on-circle collisions.

For circle-rectangle or rectangle-rectangle something more will be needed. Hmm...

Could you describe what you want in more detail? I can't see the picture
 

Posted May 30, '11 at 1:41pm

Oseb

Oseb

29 posts

Here is the link of my image:

http://kepfeltoltes.hu/view/110530/ma_www.kepfeltoltes.hu_.jpg

 

Posted May 30, '11 at 1:46pm

arobegamr

arobegamr

130 posts

Rectangle-rectangle collisions are performed by cross-checking each line segment algebraically, (the way you would solve linear systems in your math class), with the extra step of making sure that the intersection of the two lines lies on the segment.

Rectangle-circle collisions are more difficult, and done by finding the intersection between each segment on the rectangle with the line perpendicular to that segment and passing through the center of the circle, then checking if the point lies within the circle. (If the distance between the point and the center of the circle is less than the circle's radius)

Remember that collision detection and collision reaction are two completely different operations, the latter being the more complicated of the two.

 

Posted May 30, '11 at 1:50pm

Oseb

Oseb

29 posts

And if i put symbols (left-right bottom-top) to my circle and make hittest for them? i think its works? Sry i now i dont have enought time to it.

 
Reply to How to make a perfect hitTest in as2?

You must be logged in to post a reply!