ForumsProgramming ForumHow to make a perfect hitTest in as2?

33 14439
Oseb
offline
Oseb
29 posts
20

Hi everybody!
Im working on a game, but when i make a hitTest its not work perfectly (stops before reach the object). I use this code:

onClipEvent (enterFrame) {
if (_root.MC1.hitTest(this)) {
_root.MC1._y-= 5;
}
}

Any idea how to make it? Please explain it please.

  • 33 Replies
Programpro
offline
Programpro
567 posts
185

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
Oseb
offline
Oseb
29 posts
20

Here is the link of my image:

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

arobegamr
offline
arobegamr
130 posts
540

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.

Oseb
offline
Oseb
29 posts
20

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.

Oseb
offline
Oseb
29 posts
20

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
}


I think that this code was ok but i need it without x coordinate. But when i deleted the X coordinate (the first 200) then it didnt work.

arobegamr
offline
arobegamr
130 posts
540

No, you would again be checking collision between symbol/symbol, which would not be a perfect collision, but a collision between bounding boxes. If you want to detect the corners of your circle, these are your points, in order clockwise starting from the top-left
(_x means circle._x, _y means circle._y)

(_x+Math.cos(-2.356)*radius,_y+Math.sin(-2.356)*radius)
(_x+Math.cos(-0.785)*radius,_y+Math.sin(-0.785)*radius)
(_x+Math.cos(0.785)*radius,_y+Math.sin(0.785)*radius)
(_x+Math.cos(2.356)*radius,_y+Math.sin(2.356)*radius)

Oseb
offline
Oseb
29 posts
20

HitTest between a circle and a rectangle. I think this code is ok for it:

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

But the only problem is that this is one point i want that if my circle reach Y = 200 then he stops thats all i want to do. But when i delet the first 200 of the code then its wont work, so how scould this code sounds?

arobegamr
offline
arobegamr
130 posts
540

So, you want your character to not be able to cross the line y=200?

Oseb
offline
Oseb
29 posts
20

yes

arobegamr
offline
arobegamr
130 posts
540

Alright, so your code will differ based on where the character's registration point is.

Center: if(circle._y+circle._height/2>200){
Top: if(circle._y+circle._height>200){
Bottom: if(circle._y>200){

Oseb
offline
Oseb
29 posts
20

Thanks!

And after these codes (which i add to the symbols in my circle symbol) i add this code:

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

And if yes i still have one last problem, that when he reach 200 he will bounce and bounce back (if i hold the down arrow button). And what i want is that when my character reach Y=200 then he stops and doesn't bounce back. I swear its my last question.

arobegamr
offline
arobegamr
130 posts
540

Actually, you don't need the hitTest function at all in this case. I assume that you are placing the code to move the character after the code to detect if the player is below the point.

Try this:
if(Key.isDown(Key.DOWN)){
if(this._y+5>200){
this._y=200
}else{
this._y+=5
}
}

Oseb
offline
Oseb
29 posts
20

AWESOME!
Worked well. Thanks
My only last problem is with add gravity
Thanks again

arobegamr
offline
arobegamr
130 posts
540

If you want to add gravity:

onClipEvent(load){
this.jumping=1
this.jump=0
}
onClipEvent(enterFrame){
if(this.jumping){
this.jump-=.5
if(this._y-this.jump>200){
this._y=200
this.jumping=0
this.jump=0
}else{
this._y-=this.jump
}
}else{
if(Key.isDown(Key.UP)){
this.jump=5
this.jumping=1
}
}
}

Oseb
offline
Oseb
29 posts
20

Thanks!
You are pretty good in AS2. That technic is good for walls but nt good for moving objects what about them?
Thanks Again you really helped me a lot!

Showing 16-30 of 33