Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collision Problem 1

Status
Not open for further replies.

gpalmer711

IS-IT--Management
May 11, 2001
2,445
0
0
GB
Hi Guys and Gals,

I have a little problem overcoming a collison problem. I have an animation that basically makes some molecules float around the screen. When they hit into each other they morph into another ion.

This all works fine.

However if one molecule hits another molecule while it is morphing then they seem to get trapped over each other, if they eventually break free then one of then seems to not be detectable by other molecules.

You can see a .fla 60kb (MX2004) file at which contains just circles and other shapes but exactly the same code as the main animation.

If you want to see the animation that I am implementing this code into you can see a low res version (1mb) at

Greg Palmer
Freeware Utilities for Windows Administrators.
 
You are doing [tt]hitTest[/tt] from both the main timeline and individual MCs – they can cancel each other and result in infinite loop, and that is probably what’s happening. If you put all your scripts in one place, not only it may solve your issue but also it will make far easier to debug things like this, so it’s generally a good practice to put all the code in one place – the main timeline.

Kenneth Kawamoto
 
Hi Kenneth,

Your advice has certanly helped an awful lot. The problem of molecules getting stuck on top of each other is not occuring now. However I now have another problem kind of related.

I am getting the problem of the two movieclips either not detecting each other or not executing the code that they should when they hit each other.

The code that I am currently using is as follows:

In the 1st Frame on the main timeline

Code:
onLoad = function () {
	obj._x = 5000000;
	obj._y = 5000000;
	for (i=0; i<=3; i++) {
		duplicateMovieClip("obj", "obj" add i, i+1);
		// so ball0 goes after x=0, ball1 after 200 always and ball2 after 400
		eval("obj" add i)._x = random(140)+30+100*i;
		eval("obj" add i)._y = random(340)+30;
	}
	status = new Array(0, 0, 0, 0);
	// 0 is for circle, 1 is for square, 2 is for oval
};
onEnterFrame = function () {
	for (i=0; i<=3; i++) {
		if (i == 3) {
			newObj = 0;
		} else {
			newObj = i+1;
		}
		if (_root["obj"+i].hitTest(_root["obj"+newObj])) {
			if (status[i] == 2 && status[newObj] == 1) {
				_root["obj"+i].incrementX *= -1;
				_root["obj"+newObj].incrementX *= -1;
				_root["obj"+i].incrementY *= -1;
				_root["obj"+newObj].incrementY *= -1;
				_root["obj"+i].gotoAndPlay("oval2circle");
				_root["obj"+newObj].gotoAndPlay("square2circle");
			}
			if (status[i] == 1 && status[newObj] == 2) {
				_root["obj"+i].incrementX *= -1;
				_root["obj"+newObj].incrementX *= -1;
				_root["obj"+i].incrementY *= -1;
				_root["obj"+newObj].incrementY *= -1;
				_root["obj"+i].gotoAndPlay("square2circle");
				_root["obj"+newObj].gotoAndPlay("oval2circle");
			}
			if (status[i] == 0 && status[newObj] == 0) {
				_root["obj"+i].incrementX *= -1;
				_root["obj"+newObj].incrementX *= -1;
				_root["obj"+i].incrementY *= -1;
				_root["obj"+newObj].incrementY *= -1;
				_root["obj"+i].gotoAndPlay("circle2square");
				_root["obj"+newObj].gotoAndPlay("circle2oval");
			}
			if (status[i] == 0 && status[newObj] == 1) {
				_root["obj"+i].incrementX *= -1;
				_root["obj"+newObj].incrementX *= -1;
				_root["obj"+i].incrementY *= -1;
				_root["obj"+newObj].incrementY *= -1;
			}
			if (status[i] == 1 && status[newObj] == 0) {
				_root["obj"+i].incrementX *= -1;
				_root["obj"+newObj].incrementX *= -1;
				_root["obj"+i].incrementY *= -1;
				_root["obj"+newObj].incrementY *= -1;
			}
			if (status[i] == 2 && status[newObj] == 2) {
				_root["obj"+i].incrementX *= -1;
				_root["obj"+newObj].incrementX *= -1;
				_root["obj"+i].incrementY *= -1;
				_root["obj"+newObj].incrementY *= -1;
			}
			if (status[i] == 1 && status[newObj] == 1) {
				_root["obj"+i].incrementX *= -1;
				_root["obj"+newObj].incrementX *= -1;
				_root["obj"+i].incrementY *= -1;
				_root["obj"+newObj].incrementY *= -1;
			}
			if (status[i] == 0 && status[newObj] == 2) {
				_root["obj"+i].incrementX *= -1;
				_root["obj"+newObj].incrementX *= -1;
				_root["obj"+i].incrementY *= -1;
				_root["obj"+newObj].incrementY *= -1;
			}
			if (status[i] == 2 && status[newObj] == 0) {
				_root["obj"+i].incrementX *= -1;
				_root["obj"+newObj].incrementX *= -1;
				_root["obj"+i].incrementY *= -1;
				_root["obj"+newObj].incrementY *= -1;
			}
	}
};

Code in the MovieClip

Code:
onClipEvent (load) {
	if (random(10)%2) {
		incrementX = 1;
	} else {
		incrementX = -1;
	}
	if (random(10)%2) {
		incrementY = 1;
	} else {
		incrementY = -1;
	}
	gotoAndStop(100);
}
onClipEvent (enterFrame) {
	this._x += incrementX;
	this._y += incrementY;
	// ball goes too much in the left. bounce the wall
	if (this._x<30) {
		incrementX = 1;
	}
	// ball goes too much in the right. bounce the wall
	if (this._x>770) {
		incrementX = -1;
	}
	// ball goes too lower. bounce the wall
	if (this._y<30) {
		incrementY = 1;
	}
	// ball goes too higher. bounce the wall
	if (this._y>570) {
		incrementY = -1;
	}
}

Finally there is the code in the individual sections (e.g Oval2Circle) that changes the status of the Obj.

If anyone can offer any advice or pointers. Even if it means me rewriting the code I would be more than happy.


Greg Palmer
Freeware Utilities for Windows Administrators.
 
Hi Again,

I think I have found the root of the problem that I am experiencing now, but would appreciate some advice on the best way to go forward.

Code:
onEnterFrame = function () {
    for (i=0; i<=3; i++) {
        if (i == 3) {
            newObj = 0;
        } else {
            newObj = i+1;
        }

This is the first part of the code that is used to detect collisions. From what I can work out this will not test all possible combinations. (i.e. if i = 0 then it will test if obj0 hits any of the other objects. However if i = 2 then it will only test objects 3 and 0, missing out obj1.)

So does anyone have any ideas on how I can make sure we test when any object hits another?

Greg Palmer
Freeware Utilities for Windows Administrators.
 
The possible combinations for your collision test are as follows:
obj0/obj1
obj0/obj2
obj0/obj3
obj1/obj2
obj1/obj3
obj2/obj3

You can place a loop within a loop to get all the combinations:
[tt]//
var obj:String, newObj:String;
for (var i = 0; i<=3; i++) {
obj = "obj"+i;
for (var j = i+1; j<=3; j++) {
newObj = "obj"+j;
trace(obj+"/"+newObj);
}
}
//[/tt]

The script above will outout:
obj0/obj1
obj0/obj2
obj0/obj3
obj1/obj2
obj1/obj3
obj2/obj3

Kenneth Kawamoto
 
Hi Kenneth,

Many thanks again. I had tried something similar but was obviously thinking the wrong way.

I had assumed that because some of the molecules were not detecting each other that I needed to make sure that all possible combinations were taken care of.

0/0
0/1
0/2
0/3
1/0
1/1
1/2
1/3
etc etc...

However it seems that this way 0/1 and 1/0 would cancel each other out, hence them missing each other.

Thank you so much for all your help the animation is working perfectly now.

Greg Palmer
Freeware Utilities for Windows Administrators.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top