Forum Topic: AS: Collisions

(11,268 views • 27 replies)

This topic is 1 page long.

<< < > >>
None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 6/28/05 01:47 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,037

-INTRO-
So, what is a collision?

A collision is to check whether 2 objects are touching.

Flash has a built-in collision detection function called hitTest(). It has some uses, but we are concentrating more here on advanced stuff.

-hitTest()-
Hit Test? Well, to start it has the following form

instance.hitTest(instance)
or
instance.hitTest(pointx, pointy, shapeflag)

The first method tests whether 2 symbols are touching and returns true if they are. There are huge problems with this, though. Say you have 2 rectangles. One named "r1" and one named "r2". On r1 put the following code:

onClipEvent(enterFrame){
_x = _root._xmouse
_y = _root._ymouse
if(hitTest(_root.r2){
trace("xx")
}

This should work fine. When the 2 rectangles collide, it traces xx to the output window.

Now change the shape of the rectangles. Make it like a blobshape or something.

The code won't work as well, because it draws a rectangle around your objects when using this function.

The second way is like so. hitTest(x,y,shapeflag)
Set shapeflag to true and the point to 275, 200

if(hitTest(275,200,true){
trace("xx")
}

If the symbol is touching the point (275,200) it traces "xx" to the output window. The problem is is that this only works with a single point, but leaves out the bounding box of the symbol (the virtual rectangle drawn around it)

So how do we make it better? We code our own functions or use simple tricks.

-TRICKS USING hitTest()-

You can use mutiple points on an object to test against another object.

OR You can hitTest against a symbol inside another symbol that is invisible and shrunk a little.

-ADVANCED HIT TEST-

*Circle vs Circle
Let's see.... To accurately test 2 circles together, all we need to do is find the distance between them. Make 2 circles, 1 named c1 and 1 named c2 Put this code on c1

onClipEvent(enterFrame){
_x = _root._xmouse
_y = _root._ymouse
radius = _width/2
radius2 = _root.c2._width/2
dist = Math.sqrt(Math.pow(_x-_root.c2._x, 2)+Math.pow(_y-_root.c2._y, 2))
if(dist<radius+radius2){
trace("xx")
}

Test your movie. If the circles are hitting, it will trace "xx". The word hitTest is not found in that code once. What is does is finds the distance using the pythagorean therom. d=[radical]deltax[squared]+deltay[squared]
If the distance is less than the sum of the radiuses, the circles are touching.

By using math you can test collision detection against almost any shapes.

For further reading:
Beizer Curve Collision
http://www.gotoandplay.it/_articles/2004/07/collisions.php
http://www.gotoandplay.it/_articles/2004/07/3dBalls.php
http://www.gotoandplay.it/_articles/2004/09/collisions.php
http://www.gotoandplay.it/_articles/2003/09/collisions.php

~a GG tutorial


None

Dancing-Thunder

Reply To Post Reply & Quote

Posted at: 6/28/05 01:50 PM

Dancing-Thunder NEUTRAL LEVEL 04

Sign-Up: 04/19/05

Posts: 278

Holy shit! Another AS thread! I still liked this becuase it helped me a lot! Thanks.


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 6/28/05 01:52 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,037

you're welcome.


None

Denvish

Reply To Post Reply & Quote

Posted at: 6/28/05 01:55 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

Heh, AS: threads seem to be going wild today... nice to see it catching on, and the Flash forum finally starting to have a decent collection of specific Actionscripting threads.

Collision detection was actually already covered (AS: Collision Detection by BleeBlap), but I guess there's no harm in having two different explanations of the same code. You covered some points that BleeBlap missed, and vice versa.

- - Flash - Music - Images - -

BBS Signature

None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 6/28/05 01:59 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,037

yeah, I guess. I just looked and saw the double shape flag hitTest thing and was like wow.


None

Dipshitprod

Reply To Post Reply & Quote

Posted at: 10/8/05 03:57 PM

Dipshitprod LIGHT LEVEL 07

Sign-Up: 03/13/05

Posts: 390

to start off, the code with the two retangles isn't workign =/


None

Rantzien

Reply To Post Reply & Quote

Posted at: 10/8/05 04:41 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

At 10/8/05 03:57 PM, Dipshitprod wrote: to start off, the code with the two retangles isn't workign =/

He just missed a bracket and an end parenthese (sp?):

onClipEvent(enterFrame){
_x = _root._xmouse;
_y = _root._ymouse;
if(hitTest(_root.r2)) trace("xx");
}

BBS Signature

None

nokotin

Reply To Post Reply & Quote

Posted at: 2/23/06 03:07 PM

nokotin NEUTRAL LEVEL 11

Sign-Up: 06/27/05

Posts: 165

finnaly a good hit test thread/ tut thing cool!


None

Trunks

Reply To Post Reply & Quote

Posted at: 2/23/06 03:09 PM

Trunks DARK LEVEL 22

Sign-Up: 07/31/05

Posts: 4,167

Great job, really explained it in detail. There were a few suntax errors, was that intentional (so nobody can copy and paste)? Just wondering.

Another AS thread to add to my favorites.

'05 users for the win :3

BBS Signature

None

gamemaster50001

Reply To Post Reply & Quote

Posted at: 3/15/06 10:15 PM

gamemaster50001 LIGHT LEVEL 07

Sign-Up: 03/27/05

Posts: 125

Thank you so much! this is just the help i needed with 2 circles! thanks again!


None

Tolomus

Reply To Post Reply & Quote

Posted at: 3/30/06 05:37 PM

Tolomus NEUTRAL LEVEL 07

Sign-Up: 11/17/05

Posts: 110

Hey all,

I'm having some problems of my own with the hittest...

When using:

bc=1000; //bulletcount
onMouseDown=function(){
bc++;
if(bc>1050){bc=1000;}
duplicateMovieClip("bullet", "b"+bc, bc);
with(_root["b"+bc]){
spd = 20;
_x=gun._x;
_y=gun._y;
_rotation = _root.gun._rotation;
}
_root["b"+bc].onEnterFrame=function(){
with(this){
if (_rotation>180) {
_y += (spd*Math.cos( Math.PI/180*_rotation));
_x -= (spd*Math.sin( Math.PI/180*_rotation));
} else {
_y -= (spd*Math.cos( Math.PI/180*_rotation));
_x += (spd*Math.sin( Math.PI/180*_rotation));
}
if(_x>Stage.width || _x<0 || _y<0 || _y>Stage.height){
this.removeMovieClip();
}
}
}
}

onMouseMove=function(){
with(gun){
this.Xd =_root._xmouse-_x;
this.Yd =_root._ymouse-_y;
radAngle = Math.atan2(Yd, Xd);
_rotation = int((radAngle*360/ (2*Math.PI))+90);
}
}

I used this ^ AS as above to duplicate a 'bullet' instance from the gun in my project. Yet, when trying to combine this with a hittest, it doesn't work. That is, the original bullet MC has the following code in it, and if that is in contact with 'enemy' it registers a hit... however the duplicates do not register. I made it so that it plays a MC if the hittest registers, and as I say the original bullet does register, but the duplicates, on hitting the target, do not cause the MC 'hitbox' to play.

onClipEvent (load) {
xArray = new Array();
yArray = new Array();
for (x= _root.bullet.getBounds(_root).xMin; x<=_root.bullet.getBounds(_root).xMax; x++) {
for (y=_root.bullet.getBounds(_root).yMin; y<=_root.bullet.getBounds(_root).yMax; y++) {
if (_root.enemy.hitTest(x, y, true)) {
xArray = xArray.concat(x);
yArray = yArray.concat(y);
}
}
}
for (z=0; z<xArray.length; z++) {
if (_root.bullet.hitTest(xArray[z], yArray[z], true)) {
_root.hitbox.gotoAndPlay("2");

break;
}
}
}

Why is this? I'm not very advanced with AS, but I'm trying to learn and any help with a fix to these codes, or advice to understand better this AS, would be much appreciated.

Thanks in advance,

James.


None

Tolomus

Reply To Post Reply & Quote

Posted at: 3/31/06 06:50 AM

Tolomus NEUTRAL LEVEL 07

Sign-Up: 11/17/05

Posts: 110

Any help please?


None

ninja-pirate-monkey

Reply To Post Reply & Quote

Posted at: 12/7/06 08:48 PM

ninja-pirate-monkey EVIL LEVEL 11

Sign-Up: 08/24/06

Posts: 349

damn it this didnt help me
i tryed 1 tut but it didnt work
i tried the ultimate tut collab nd that jst let me psh it
cn u tell me how to just make a god damn wall

ninja-pirate-monkey

BBS Signature

None

Hawkfire

Reply To Post Reply & Quote

Posted at: 12/10/06 08:57 PM

Hawkfire LIGHT LEVEL 04

Sign-Up: 11/20/06

Posts: 131

what he said


None

2MuchCaffeineGames

Reply To Post Reply & Quote

Posted at: 12/10/06 09:48 PM

2MuchCaffeineGames LIGHT LEVEL 03

Sign-Up: 06/11/06

Posts: 6

In response to ninja-pirate-monkey and Hawkfire, You can just put

onClipEvent (enterFrame){if (hittest(_level0.mc) == true)

in the object you want to hittest, and then replace mc with the object you want to hit against. Then you add the code to stop the object. If you are using a variable to control movement, make that var = 0, or if you are using some simple this._x+=4 junk or something like that, you can add

this._x-=4;}}

to the end. That is just for the rectangle around the object. no problems for squares. If that doesn't work, check the semicolons and paranthesis. If that doesn't work, it could just be my program.

If i wasn't clear enough,

onClipEvent(enterFrame){if(hittest(_level0.mc ) == true){_root.variable = 0;}}

edit that code to fit your situation


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 12/10/06 10:38 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

At 12/10/06 09:48 PM, 2MuchCaffeineGames wrote: onClipEvent (enterFrame){if (hittest(_level0.mc) == true)

you dont have to put == true since the == operater and the hittest function both return a boolean which is what the if() reads. also hitTest() not hittest()

if (hitTest(_level0.mc))

works just fine

Some times my "L" key decides not to work.


Thinking

calsterman

Reply To Post Reply & Quote

Posted at: 5/22/07 02:39 PM

calsterman EVIL LEVEL 07

Sign-Up: 11/12/06

Posts: 3

HELP

in many games you drive a movieclip and when another movieclip (enemy) collides with each other. but instead of a box saying touching etc. its plays to a different frame where it may say "game over" or "your dead".

I want to know how to do this. could you reply in email either with a flash file on a example game or a fully explanation on how to do it. please .

please could you reply A.S.A.P

MANY THANKS CALUM


None

knugen

Reply To Post Reply & Quote

Posted at: 5/22/07 03:52 PM

knugen LIGHT LEVEL 35

Sign-Up: 02/07/05

Posts: 4,632

At 5/22/07 02:39 PM, calsterman wrote: please could you reply A.S.A.P

You do realize that you're reviving a thread that has been dead for six months?


None

senior-twinki

Reply To Post Reply & Quote

Posted at: 7/12/07 09:02 PM

senior-twinki LIGHT LEVEL 10

Sign-Up: 06/26/07

Posts: 1,592

could you post a link to an SWF?


None

Dr34m3r

Reply To Post Reply & Quote

Posted at: 7/12/07 09:04 PM

Dr34m3r LIGHT LEVEL 20

Sign-Up: 03/07/06

Posts: 3,580

At 7/12/07 09:02 PM, senior-twinki wrote: could you post a link to an SWF?

Why the hell did you bump this?

Warshark is fucking awesome.
XBL Gamertag: ZeeAk

BBS Signature

None

mechias3

Reply To Post Reply & Quote

Posted at: 7/23/08 07:38 PM

mechias3 NEUTRAL LEVEL 04

Sign-Up: 03/28/07

Posts: 152

What do you mean by trace XX? I don't get it.


Goofy

zrb

Reply To Post Reply & Quote

Posted at: 7/23/08 07:46 PM

zrb LIGHT LEVEL 11

Sign-Up: 08/08/06

Posts: 4,511

At 7/23/08 07:38 PM, mechias3 wrote: What do you mean by trace XX? I don't get it.

If the action works, it will show XX in the output menu.

School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature

None

Cerebral-Overload

Reply To Post Reply & Quote

Posted at: 7/24/08 07:25 AM

Cerebral-Overload EVIL LEVEL 14

Sign-Up: 05/12/08

Posts: 120

Another great tutorial glaiel this helped me a lot.

BBS Signature

Goofy

mechias3

Reply To Post Reply & Quote

Posted at: 7/24/08 08:28 PM

mechias3 NEUTRAL LEVEL 04

Sign-Up: 03/28/07

Posts: 152

Could you explain each part of the actionscript, so I can figure out how to put it in my game? It's so confusing for me. Partly because I'm probably because I'm a total nubcake at actionscript.


None

zrb

Reply To Post Reply & Quote

Posted at: 7/24/08 08:31 PM

zrb LIGHT LEVEL 11

Sign-Up: 08/08/06

Posts: 4,511

At 7/24/08 08:28 PM, mechias3 wrote: Could you explain each part of the actionscript, so I can figure out how to put it in my game? It's so confusing for me. Partly because I'm probably because I'm a total nubcake at actionscript.

Bumping this old thread won't do you any good.

School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature

None

mechias3

Reply To Post Reply & Quote

Posted at: 7/24/08 09:06 PM

mechias3 NEUTRAL LEVEL 04

Sign-Up: 03/28/07

Posts: 152

Huh? what's bumping?


None

zrb

Reply To Post Reply & Quote

Posted at: 7/24/08 09:10 PM

zrb LIGHT LEVEL 11

Sign-Up: 08/08/06

Posts: 4,511

At 7/24/08 09:06 PM, mechias3 wrote: Huh? what's bumping?

When you post a reply in a topic. But in this case, its from 2005 so you shouldn't bother replying.

School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature

None

LordOfKetchup

Reply To Post Reply & Quote

Posted at: 7/25/08 01:23 AM

LordOfKetchup FAB LEVEL 17

Sign-Up: 05/10/04

Posts: 1,064

At 7/24/08 08:28 PM, mechias3 wrote: Could you explain each part of the actionscript, so I can figure out how to put it in my game? It's so confusing for me. Partly because I'm probably because I'm a total nubcake at actionscript.

If this is hard for you, you need to go back to the basics first. You need to learn what the heirarchy is, that will help you out a lot in understanding code

.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 05:28 PM

<< 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!