Best Way To Get Good Collision Detection?
So I've been googling around for information on detecting collisions in AS3 and I found this page which has what troy claims to be a 'pixel-perfect' collision detection algorithm. http://troygilbert.com/2007/06/25/pi...actionscript3/
I'm just getting into this and find myself wondering if such an elaborate class is needed to see if two oddly shaped objects touch each other. My understanding is that Flash only offers point and bounding box collision detection and that if you have a weirdly shaped object, you need something like this class to really tell if two objects are actually touching.
The class on that page also lacks any real documentation.
Lastly, I'm wondering what sort of performance considerations this might have.
Any general advice about this would be appreciated -- or perhaps a faster/easier/better documented collision detection example.
FlashKit > Flash Help > Actionscript 3.0
Posted on: 08-26-2008, 12:53 AM
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Collision Detection
I have many instances of a movie clip (by using duplicateMovieClip) on my stage, each with a name of "clip1", "clip2", "clip3" etc.
I have many instances of another movie clip, like the one above, but all derived from a different original movie clip. They're named "something1", "something2" etc.
I want to put an "if" statement that performs a "hitTest" between one of the "something" movie clip instances and ANY of the "clip" instances.
This is the code I have inside the onClipEvent (enterFrame) of the "something" instance:
onClipEvent (enterFrame) {
if (this, hitTest(_root.varClip)) {
this.removeMovieClip();
}
}
and I have this code in a frame on the _root level
for (var i = 1; i<=3; i++) {
_root.varClip = ["clip"+i];
}
Nothing is working this way. Is there some other way that I can check the collision using arrays?
Am I mixing up the paths?
I'd really appreciat some help with this. I feel like I'm so close already.
Thanks
Kevin
Collision Detection
Alright troops?
I wonder if any kind (and talented) souls could give me a hand? I'm trying to build a pac-man type maze game and I'm not sure about how to produce collision detection to stop my character goin' through the walls. I'm really not sure where best to begin on this one, so any advice at all is greatly appreciated.
Cheers
Collision Detection On 5
I'm using the collision detection feature on Flash 5, which works nicely, but I don't know how to make a movie detect another instance of itself without detecting itself
To illustrate what I mean, I'm making a battle of Ants. Each ant is created by an Array, and I want their heads to detect the collision with another. So I'm using the following code within the 'head' movie:
flag = this.hitTest(_root.ant);
if (flag == true) { do something... }
What should I use instead of _root.ant?
Thanks!
Collision Detection
Hi,
I am trying to make a game. I have a character and a 3d floorplan he walks around in. Instead of moving the guy, I move the floor so it looks like he is moving, but he stays in the center of the screen. So I got the hit test to work, but I have one big object that my character is colliding with (the map). I know that I can break apart the floor and have separate walls, but I was hoping there is a way to keep using only one object. The object is vector, so why can't flash only see the filled in areas? If anyone can help me with this, I would greatly appreciate it. Thanks.
Stan
Collision Detection
hey guys
i'm trying to create a game where you're a tank and you move around using the mouse (you click where you want to move to and the tank drives in a straight direction towards the mouse)
I know how to make the tank move, the thing i need help on is the collision detection, the tank is a rectangle movie clip and the object i am colliding with is a rectangle too.
this is what i need to happen:
when the tank collides with the object, the tank will stop
there's a varable in the tank MC that if it is set to TRUE the tank will move, if FALSE the tank will stop where it is
the variablie is called 'GO'
i know the basic
if(this.hitTest(_root.object)){
this.go = false;
}
but if i do this, then the tank will still be touching the object, so when the tank touches the object, i would like the tank to be moved 5 pixels away from it so it wouldn't be touching the object
is there anyway i can do this?
Collision Detection
Ok, um i have this video clip that i can move around the screen using the arrow keys on the keyboard. I also have some other graphics on the stage. Now, what i want to do is when the movie clip touches one of these other graphics the movie clip stops and it cant go over the particualr graphic. so i now hae to move the mvie clip around the graphic. Can someone please help me on how to do this. Your help is greatly appreciated. Thank you in adavance for any help you can offer.
Kind Regards,
The Prophecy.
Collision Detection
i want to have a mc follow the cursor and when it collides with another mc an action is done.
Collision Detection
Here's what I'm trying to do:
Go to and play frame 5 when MC1 AND MC2 have both been dragged into a certain area.
I got both MC's to be draggable into a red box but how do I check to see if both are there and if so move on and if not wait until they are?
Thanks
Collision Detection
hello,
i have a MC which carries the follwoing script. when the MC detects collision, corresponding MC instance will start.
onClipEvent (enterFrame) {
this._y+=laserSpeed;
if (this._y<-50) {
this.removeMovieClip();
}
if (this.hitTest(_root.map.ground.target_pro)) {
_root.pro.gotoandstop(2);
this.removeMovieClip();
}
if (this.hitTest(_root.map.ground.target_edu)) {
_root.edu.gotoandstop(2);
this.removeMovieClip();
}
if (this.hitTest(_root.map.ground.target_work)) {
_root.work.gotoandstop(2);
this.removeMovieClip();
}
if (this.hitTest(_root.map.ground.target_ex)) {
_root.ex.gotoandstop(2);
this.removeMovieClip();
}
if (this.hitTest(_root.map.ground.target_ski)) {
_root.ski.gotoandstop(2);
this.removeMovieClip();
}
}
Q1 does anyone know why only four out of five collision detection work? the MC "pro" does not detect collision...i ckecked instance names etc but just don't have a clue why it doesn't work!!
Q2 does anyone know how to create a master script so that i don't have to repeat the same if(this.hitTest...) script? how can "this" (the main MC that holds the script above) find out collided object's instance name??
any help greatly appreciated!
Thanks!
reynard
Collision Detection
does anyone know the ode for collision detection and how to make a rotating objet move in the direction it's pointing?
example :
I I
I I I = wall
I I <O> = object
I I
IIIIIIIIIIII IIIIIIIIIIIII
<O>
IIIIIIIIIIII IIIIIIIIIIIII
I I
I I
I I
I I
i want the user to be able to press UP to go forwards and DOWN to go backwards. He can the press RIGHT / LEFT to choose the direction he will move in...
plz help
Collision Detection
Ok, I'm making a pong game, but the collision detection system I'm using doesn't work too well. The ball will be inside the paddle before it reverses direction and sometimes it just goes straight through. I know there's a better way than just using hitTest(even with the borders on). Anyone have ideas?
Collision Detection Help
Hey--
I'm having a problem with my collision detection. I followed a tutorial and downloaded the fla to double check, but its not working. I have one mc called laser, which's actionscript is "
onClipEvent (load) {
laserMoveSpeed=20;
this._x=_root.spaceship._x+90;
this._y=_root.spaceship._y;
}
onClipEvent (enterFrame) {
if (this._name<>"laser"){
this._x+=laserMoveSpeed;
if (this._x>600){
this.removeMovieClip();
}
for (i=1; i<=_root.numEnemy; i++){
if (this.hitTest( _root["enemy"+i])){
_root.score+=100;
_root["enemy"+i].gotoAndPlay( 2 );
}
}
}
}
"
And the enemy it's killing's movie clip is called enemy one, and its action script is"
onClipEvent (load) {
function reset(){
this._x=600;
this._y=random(200)+100;
enemySpeed=random(4)+1;
this.gotoAndStop(1);
}
reset();
}
onClipEvent (enterFrame) {
if (_root.spaceship.scrollStart){
this._x-=enemySpeed+_root.mainGround.groundSpeed;
} else {
this._x-=enemySpeed;
}
if (this._x<-10) {
reset();
}
if (this.hitTest( _root.spaceship ) ){
_root.gotoAndStop ( "gameOver" );
}
}
"
any help appriciated.
Thanks
Collision And Detection
Can anyone help me?.. I need to know how to simulate molecules leaving a surface within a chamber. The simulation
should behave as real as possible wherein if a molecule hit
the wall of the chamber, it should deflect at the same amgle
Collision Detection
hi all
1. i have drawed some lines with actionscript
2. i have let's say a circle which i move around
now i need to make a collision detection so when the circle does collide with one of the lines it has to stop.
can someone tell me how i can do that.
thx a lot in advance
marcel
Collision Detection
am trying to create game where someone throws ball to hit target. the target is moving. the ball is necessarily embedded in another movie clip. how do i get the x y coordinates to consider the same registration basis. meaning, x for one is not x for the other so i cant have "if ball.x = target.x then"
Collision Detection
Hi,
Is it possible to do pixel perfect collision detection with a clip on a clip in Flash 5? I know its possible using the mouse x,y but need to know if theres a way of doing it between 2 clips.
thanks
phil.
Collision Detection
Could someone please take a quick look at this Fla I have 2 mcs that u control via the keyboard one is with the arrow keys and the other is with W A S D keys.
I have another two movieclips that are a sort of barrier to the others when the green square hits the big blue square u r stuck the same with the other one but this is not the problem.
The problem is that the two keyboard controlled movie clips overlap. Is there a way to when they collide they hold there position on the stage but u are still able to maneuver them just not in the direction of the other MC.
Thanks
Collision Detection.. Help
Hi ya
Im making a fighting game and Im stuck on the actionscripting...
I have a 'health bar' for each player and when player 1 hits player 2 I want player 1s health bar to go up a notch, etc. Ive created the movie clips for each health bar but dont know what action scripting to put in to make it all come together....
Its driving me mad!!!
(Theres only 3 moves for each player and they're all seperate movie clips, incase you were wondering)
Please somebody help!
thankyou
lou
Collision Detection
What is the best way to detect collisions between multiple objects of certain types, for example, any of a "laser beam" movie clip hitting any "bad guy" movie clip on the stage at any time, like in a spaced invaders game? I tried looping through all the laser mc's on the stage and seeing if they hit any of the bad guy clips, but i think it is bogging down the proc so much that it misses most of the hits...
Dave
Collision Detection
What is the best way to do collision detection for a flash game with a bunch of different "bad guy" objects, and "laser gun" objects, or something like that?
Dave
Collision Detection?
Hi!
I'm making this webpage where you drive a car around with the keyboard. When the car drives over certain objects, I want them to light up, and if you press e.g. enter when over one of them, a movie should open.
I have read all kinds of different tutorials about collision detection to try to pick something up there, but I just can't make it work...
Would really appreciate it if someone would be willing to help me out on this one!!
Tnx, Thomas.
Mc Collision Detection
I've followed the basic tutorial on mc collision detection and attempted to ammend it slightly for my needs.... it didn't work.
I've attached the fla file as it's the best way to show what I'm attempting to do:
basically, I need a gotoAndPlay action to occur when 2 mc's collide - the two clips are currenty one stationary and one a drag mc. when one is dragged over the other I need the 3rd mc 'jwooClip' to go to and play frame 2.
I've used the correct actionscript for this part but not sure if it's tied in correctly with the rest of the script????
I know this makes little sense but it will all be clear if you view the fla file.
Any help will be appreciated, even if it's a link to another tutorial (intermediate maybe??)
Collision Detection
Is it possible to use collision detection to make objects bounce off one another?
Help Please (Collision Detection)
I'm working on a very basic RPG engine, and I'm trying to work on collision detection (if you run into an enemy on the map, you go to a battle screen).
I've tried several collision detection methods from several tutorials, but I can't get it to work. I want to play Frame 2 when the black circle (character) hits the red square (hitbox). If someone could try some code in there, and find something that works, I would be very grateful. The file is attached.
Collision Detection
i have looked at flashkit, macromedia help, and some more things, each one gives me a code, alwase the same, but when i put it in my movie it dosent work, my two coliding movie clips are "a" and "b" this is my code
if (a.hitTest(b))
action
1. what is wrong with that?
2. maby im putting the code in the wrong place, so where should i put it?
I Think This Has To Do With Collision Detection
I'm not sure but how would I go about making it possible to make a character your moving with the keyboard walk into a door to a house and upon the character touching the door the screen would change to the interior of the house? If there is a tutorial on this subject point me in that direction, but I wouldn't know what to search for to find this.
Collision Detection
Hi. I'm actually developing an adventure game in flash. The character movement has been programmed. For the collision detection, I would like to ask something. Should I include the collision detection code inside together with the character movement code or include into the map or into another separate frame? By the way, I really need your help. The map I have designed is actually divided into 6 parts namely top left, top middle, top right, bottom left, bottom middle and bottom right. Each part has the size of 550 X 400 pixel. Do u guys have any code for the collision detection for the map? I'm really confused and badly need your help. If there is any, I would be really grateful to you. By the way, could you guys kindly include some comments in it so that I could understand the coding purpose? Thanks alot....
Help With A Certain Collision Detection...
I have this new file i am working on, I shouldnt put it in games because I think it can be solved very easily, it's just help with making the ball not sink into the platform. You can see it easier with this file attached.
The ball sinks into the platform in which it is supposed to bounce on.
Collision Detection Help
i need some help with my collision detection. i thought that i had the code all right but i dont. here is my code:
if (_root.lemon, hitTest(_root.player)) {
gotoAndPlay("gameover")
lemon = my enemy that the code is on
player = me...
"gameover" = the frame that it goes to and plays...
Collision Detection
Hi there!!
I would like to find out how to test a collision detection of two or more circles.
If anyone knows how to do it, please give me a hint.
Collision Detection Help
I'm more than a little stuck as to how to sort out this problem with collision detection I have for a game I intend to make / remake, using flash to sort out the logic problems involved with such matters as collision.
My problem is determining what course of action to take when bouncing off a wall. (see attachment for details)
The simple types are vertical and horizontal walls but sorting out corners appears to be a tricky one, what with there being so many types of them!!
Using Flash MX and actionscript 1.0... or is it 1.1, well it's not 2.0
Collision Detection Help
I made a simple scene where there is a space ship and meteors flying towards the spaceship. The objective is that we drag the space ship out of the way of the meteors and if we don't it would detect the collision and go to the next frame. I made a normal movie clip as my spaceship and the meteor is also s movie clip but inside of the mc it has the animation of the meteors flying by!
All i done so far was the start drag:
on (press) {
this.startDrag()
tried this but it didn't work:
if (this.hitTest(_root.meteor_mc, _root.meteor_mc, true))
trace("hitTest true");
PLEASE CAN SOMEBODY HELP ME! I would like to put the fla file but i don't know how.
Collision Detection.
does any one know how to do it so that when the square is over the circle although it is set to true i would like an action to happen such as jumping to the next scene? I have put my file attached below, if anyone has any ideas. Thank you.
Collision Detection
hi
I have 2 objects and i am duplicating thosse two object and moving then in their oposite direction so they will collide to each other. my problem is that i am not been able to dectect which object colided with which one.
Thanx in aqvance for any sort of help provided to me
[F8] Car Collision Detection
right now the collision detection sucks it just bounces back and it is glitchy.
it just checks 4 points to see if the car has hit a wall. Is there a tutorial on how to make better collision detection?
Help With Collision Detection
hi guys
I want to create a collision detention system where objects move away from each other if they get too close, much like in the link below (done in java I think):
http://www.aharef.info/static/htmlgr...joeprice.co.uk
Could anyone point me in the direction of a potential tutorial for achieving collision detection along these lines?
Many many thanks for your responses
Joe
[AS2 On CS3] Collision Detection
What is the sytax for detecting a collision for:
two movie clips on stage, blue_mc and red_mc. If they collide I want the output window tp read "hit"
Collision Detection Help
What i am trying to do is make a bunch of objects able to be arranged on a screen kind of like a customizable preview layout. Although i have objects that i don't want to be able to overlap.
If i have 3 boxes spread out on the stage and i move one i want it to detect if i overlap one to stop and not let me overlap it. I basically need a collision detection script. The issue is all three boxes can be dragged so it can't be just set values it has to be constantly updatable so that if i move one object (box 1) then move (box 2) it has to detect the position and see if it overlaps and if so not let drag any further.
Any help would greatly be appreciated.
Collision Detection
i'm trying to add collision detection to my bubble, but i can't figure it out.
can someone make me an example of this:
the bubble can't come in the red zone:
i use this code:
Code:
import flash.geom.Point;
bubbel.onEnterFrame = moveBubble;
bubbel.vx = 0;
bubbel.vy = 0;
var decay:Number = 0.95;
var maxSpeed:Number = 30;
var minDistance:Number = 150;
this.onMouseDown = function() {
this.onEnterFrame = blow;
};
this.onMouseUp = function() {
this.onEnterFrame = null;
};
function blow() {
var startP:Point = new Point(bubbel._x, bubbel._y);
var endP:Point = new Point(_xmouse, _ymouse);
var dist:Number = Point.distance(startP, endP);
var distY:Number = bubbel._y-_ymouse;
var distX:Number = bubbel._x-_xmouse;
if (dist<minDistance) {
var speed:Number = (minDistance-dist)/minDistance*maxSpeed;
var angle:Number = Math.atan2(distY, distX);
bubbel.vx += Math.cos(angle)*speed;
bubbel.vy += Math.sin(angle)*speed;
}
}
function moveBubble() {
this._x += this.vx;
this._y += this.vy;
this.vx *= decay;
this.vy *= decay;
if (this._x<this._width/2) {
this._x = this._width/2;
}
if (this._x>Stage.width-this._width/2) {
this._x = Stage.width-this._width/2;
}
if (this._y<this._height/2) {
this._y = this._height/2;
}
if (this._y>Stage.height-this._height/2) {
this._y = Stage.height-this._height/2;
}
}
[cs3] Collision Detection
Here is what I have. Right now I am creating 40 objects with shapes randomly chosen from the library. They move around the screen and bounce of the edges randomly. I also have an object that follows the mouse. I want to be able to detect when the mouse object *not the mouse* comes in contact with one of the 40 objects.
Here is what I haVe tried:
Code:
var basicUnits = 0;
//Places different objects from the library into an array and places them onto the stage.
var tmi = 40;
var instanceNames = new Array("enemy1", "enemy2", "enemy3", "enemy4", "enemy5", "enemy6", "enemy7", "enemy8");
var collisionList = new Array();
for (i=1; i<=tmi; i++) {
var libraryId = instanceNames[random(instanceNames.length)];
_root.attachMovie( libraryId , "circle"+i, i, {x:200, y:50});
collisionList = (i, "circle"+i);
basicUnits++;
}
Code:
//Beginning of collision detection code.
var ship_x = GetProperty ("/ship",_x);
var ship_y = GetProperty ("/ship",_y);
var obj;
for(p = 0; p <= tmi; p++){
//collisionList was created aboe
obj = collisionList[p];
var obj_x = "circle"+ p._x;
var obj_y = "circle" + p._y;
var ship_h = getProperty("ship", _height) / 2;
var obj_h = getProperty("circle" + p, _height) / 2;
var ship_w = getProperty("ship", _width) / 2;
var obj_w = getProperty("circle" + p, _width) / 2;
var distance_x = ship_x-obj_x;
var distance_y = ship_y-obj_y;
var area = (ship_h+ship_w)*(obj_h+obj_w);
if ((distance_x * distance_x) + (distance_y * distance_y) <= area){
trace("collision detected");
}
else{
trace("no collision");
}
}
Right now my trace is constantly reading as "collision detected", even when the mouse object is near nothing. I am going to upload the file, because there is a lot of other code that may be interfering with what I'm trying to do, and I'm not very skilled with Actionscript, so it may help to see everything in the file.
Would there be an easier way to detect collision between these objects? Or why is my code not working. Thank you.
[CS3] [AS2] Collision Detection.
Hello,
I wanted to know if there is a way with which I can get the names of objects hitting another object.
Suppose I have a box and three circles (draggable) on stage. Now, suppose user drags all three circles on the box, I get an Array with a list of their instance names...?
I know how to detect collisions. mc.hitTest(mc2) etc. .. But the situation is that I have to get the a list of object's names which are currently touching the other specific object...
is that possible?
[F8] Collision Detection
Ok, I know you can use hitTest to test an object against a specific movie clip, but say I want to test one object to see if it hits ANY movie clips, how would I do that?
Collision Detection Help
The scene is a movable tree with lots of fruits on it. When moving one of the fruit and if it touches any of the others the collided fruit will fall down to the ground.
Actually all I need is the event.target info. when I am dragging one fruit overlapping other one, how can I ask Flash to tell me whom is the collided one?
Instead of putting all the fruits into an Array and use the ENTER_FRAME event traversing this array for collision detection, I hope to use event driven methods directly in the fruit.
So the following is my basic setup:
Two classes, Fruit and Tree, both with two listeners mouseDown() and mouseUp(), where startDrag() and stopDrag() are defined in them respectively.
The constraint is all the fruit objects must be the children of the tree.
When moving the tree all the fruits get moved as well.
Thanks for the help.
Help Me With Collision Detection
hi, i have been searching for ages for help with this,
i need help with collision detection on angles like so:
http://img227.imageshack.us/my.php?image=diagramra3.jpg
please help me
Help With Collision Detection
I need help with a working out collison of a ball to a flat surface
The method that I am currently using is intercept path that checks for bounding box collisons between the ball and a surface, if there is a bounding box collision then there is a secondary test.
The point on the ball which collides with surface is normally in relation to the normal of the surface, the secondary test uses the normal of the surface to see if this part of the ball has hit the surface.
Code:
if (_root._mcSurface.hitTest (_mcBall2))
{
//Find out the potensial point of collision on the bal
//10 is the radius of the ball
var PotensialPointOfCollision : Number = (surfaceAngle + 90);
var d : Number = (10 * Math.cos (Math.PI / 180 * PotensialPointOfCollision)) + _mcBall2._x
var e : Number = (10 * Math.sin (Math.PI / 180 * PotensialPointOfCollision)) + _mcBall2._y
if (_root._mcSurface.hitTest (d, e, true))
{
See ball1.jpg
This is allvery well untill the ball is approaching the surface at a low angle, this method fails to work.
See ball2.jpg
I have made a flash file with the code in to demonstrate the problem
If anybody can help that would be great
Help With Collision Detection
Hi guys,
I'm currently coding a game for my uni course and i need some help getting the collision detection right. What I'm after is for my user controlled character to collide with objects in various arrays but the code I'm using right now isn't detecting the collisions. This is my code for the function to check for colliusions between the character and objects in one of the arrays:
ActionScript Code:
//function that checks for collisions between char and mice
public function check4Col(){
trace("checking for collisions");
//loop through planes
for (var i=0; i<mcPlaneArray.length;i++){
//check if planes collide with char
if(mcPlaneArray[i].hitTest(Char)){
trace(mcPlaneArray[i]+"has collided with "+Char);
}//end if hitTest is true
}//end for i
}//end check4col
My game is made up of different classes such as the Char.as which holds the movement functions for the controllable character and the Game.as which controls the game functions. The above code is held in the Game.as and imported into the .fla which works just fine and reports no errors.
I can't help but think that the problem is just to do with how i reference the Char movieclip. Do I need to add a variable for it into the Game.as or use a certain reference path to relate the code to the Char.as????
I've included the relevant files as an attachment for you guys to have a look at.
Any help at all will be appreciated,
Cheers!
Collision Detection
Ive been having some troblue with the collision detection from this tutorial
Help?
|