ForumsProgramming ForumHow to make a perfect hitTest in as2?

32 29974
Oseb
offline
Oseb
29 posts
Nomad

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.

  • 32 Replies
arobegamr
offline
arobegamr
130 posts
Nomad

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
Nomad

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
Nomad

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
Nomad

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
Nomad

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
Nomad

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

Oseb
offline
Oseb
29 posts
Nomad

yes

arobegamr
offline
arobegamr
130 posts
Nomad

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
Nomad

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
Nomad

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
Nomad

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

arobegamr
offline
arobegamr
130 posts
Nomad

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
Nomad

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!

Oseb
offline
Oseb
29 posts
Nomad

I have two more questions.
1) how to make moving wall, i mean my main character hits the moving object. Here i think those coordinates dont work.

2) arobegamr you said this code.
if(this._y>200){
this._y=200
}
But this code makes that my charecter stops at y=200 but i want that my character stops when Y=200 X>100 X<200? I tryed it many ways and i couldnt find the good code.

mightybob
offline
mightybob
360 posts
Peasant

Why not just

If(still_object.hitTest(moving_object2.x, moving_object2.y, true))
{
//your code
}

Of course for this to work you need registration point where it's gonna hit.
Be sure to add in _root.'s or a movieClip var.

Showing 16-30 of 32