collisions with complex shapes - Gary Fixler's original

  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

This is my attempt to write a simplified version of Gary Fixler's original 'collisions using multiple empty movieClips' on the perimeter of complex shapes.

The two clips "puck" and "cClip" are just a circle and a ring (torus) with empty clips around their perimeter. The emptyClips together are in a movieClip named perim_mc and pucPerim_mc. Can someone troubleShoot this code for me? The clips do drag, but there is no collision detection. - A thank you in advance, to all teachers.

Code: [ Select ]

Movieclip.prototype.col=function(o:MovieClip){
    var c:Number=0;
    var xs:Number=0;
    var ys:Number=0;
    
    for(i in this.perim){
        this.perim.localToGlobal(p={x:this.perim[i]._x,y:this.perim[i]._y})
        
        if(o.hitTest(p.x,p.y,true)){
            o._alpha=50;
        }
        
        ys+=p.y;
        xs+=p.x;
        c++
    }
}

ASSetPropFlags(MovieClip.prototype,"col",1);


aClips=["puck","cClip"];

eval(aClips[0]).otherClip=eval(aClips[1]);
eval(aClips[1]).otherClip=eval(aClips[0]);

for(i in aClips){
    thisClip=this[aClips[i]];

    thisClip.onPress=function(){
        this.startDrag();
    }

    thisClip.onRelease=thisClip.onReleaseOutside=function(){
        this.stopDrag();
    }
    thisClip.onEnterFrame=function(){
        this.col(this.otherClip);
    
    }

}
  1. Movieclip.prototype.col=function(o:MovieClip){
  2.     var c:Number=0;
  3.     var xs:Number=0;
  4.     var ys:Number=0;
  5.     
  6.     for(i in this.perim){
  7.         this.perim.localToGlobal(p={x:this.perim[i]._x,y:this.perim[i]._y})
  8.         
  9.         if(o.hitTest(p.x,p.y,true)){
  10.             o._alpha=50;
  11.         }
  12.         
  13.         ys+=p.y;
  14.         xs+=p.x;
  15.         c++
  16.     }
  17. }
  18. ASSetPropFlags(MovieClip.prototype,"col",1);
  19. aClips=["puck","cClip"];
  20. eval(aClips[0]).otherClip=eval(aClips[1]);
  21. eval(aClips[1]).otherClip=eval(aClips[0]);
  22. for(i in aClips){
  23.     thisClip=this[aClips[i]];
  24.     thisClip.onPress=function(){
  25.         this.startDrag();
  26.     }
  27.     thisClip.onRelease=thisClip.onReleaseOutside=function(){
  28.         this.stopDrag();
  29.     }
  30.     thisClip.onEnterFrame=function(){
  31.         this.col(this.otherClip);
  32.     
  33.     }
  34. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

may i ask what's with this code:
eval(aClips[0]).otherClip=eval(aClips[1]);
eval(aClips[1]).otherClip=eval(aClips[0]);

imho it should be:
this[aClips[0]].otherClip = this[aClips[1]];
this[aClips[1]].otherClip = this[aClips[0]];
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

Thanks iceCold for responding !

I think the Gary Fixler code was originally posted at ActionScript Toolbox a number of years ago. For those of us who never learned actionscript 1, and came late to actionscript 2.0, I just assume that 'eval' was used, and 'prototype', and 'ASSetPropFlag', were all used in earlier code.

By way of a lesson in updating earlier code, would you be willing to show me how to bring this up to date and make it run? The many emptyClip perimeter technique?

I am ultimately trying to write code for many small ball-mc 's to bounce around inside a container of complex shape (like letters, or other curved shapes)

The many balls must collide with one another and any complex boundary.

Your thoughts? Are there easier solutions for a beginner like me to try?
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

yes, true, i never learned actionscript 1.
About your request ... don't know how simple or how complex solution is for someone else. What do you mean anyway by collide? when they hit each other or hit the boundaries to change their direction according to the physic laws? Or just random change of direction.
If second case is just a matter of hittest. For first case, man you got to learn some physics :D
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

Thanks,

Just a hitTest and change of direction - I managed to get an interesting result; I guess it works by passing the emptyClips as points "p" into the clip.hitTest(p={x:this.perim[i]._x,y:this.perim[i]._y})

with the movieClip full of emptyClips given an instance name of "perim"

have you seen this before?

http://flash-creations.com/notes/astb_bounceCollide.php

Can you help me write an actionscript 2.0 version, using this "sentry" technique?
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

Subsequently,

I just realized I had to set the publish settings to actionscript 1.0 and Flash Player 6

doesnt work set to Flash Player 7 or 8

actually it also works when set to actionscript 2.0 and Flash Player 6

How do we change the code to make it work for Flash Player 8 and 9?
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

get rid of eval and prototype.
btw, nice implementation of physical laws with complex shapes .... i'll take a look over it. I must admit, right now i have no idea how to imitate the physical laws of motion for such objects.
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

thanks iceCold

Do we replace prototype with a custom class that extends MovieClip? and create a "col" method?

I'm working on it.
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

This is my new AS 2.0 version of the earlier code, removing prototype and eval. In place of prototype, the clip is passed to the function 'superCollider' and the eval line is rewritten as

Code: [ Select ]
this[aClips[0]].otherClip = this[aClips[1]];
this[aClips[1]].otherClip = this[aClips[0]];
  1. this[aClips[0]].otherClip = this[aClips[1]];
  2. this[aClips[1]].otherClip = this[aClips[0]];


Doesnt work yet ; the code below does not produce any collison results

Code: [ Select ]
aClips=["puck","cClip"];

this[aClips[0]].otherClip = this[aClips[1]];
this[aClips[1]].otherClip = this[aClips[0]];

for (i in aClips) {
    
    thisClip = this[aClips[i]];
    
    thisClip.onPress = function() {
        
        this.startDrag();
    };

    thisClip.onRelease = thisClip.onReleaseOutside = function() {
        this.stopDrag();
        
    };
    
    thisClip.onEnterFrame = function() {
        superCollider(this.otherClip);
    };

}

function superCollider(target){
    target._alpha=100;
    var c=0;
    var xs=0;
    var ys=0;
    
    for (i in this.perim) {
        this.perim.localToGlobal(p={x:this.perim[i]._x,y:this.perim[i]._y})
        
        if(target.hitTest(p.x, p.y, true)){
            target._alpha=50;
        }
        
        ys += p.y;
        xs += p.x;
        c++;
    }
}
  1. aClips=["puck","cClip"];
  2. this[aClips[0]].otherClip = this[aClips[1]];
  3. this[aClips[1]].otherClip = this[aClips[0]];
  4. for (i in aClips) {
  5.     
  6.     thisClip = this[aClips[i]];
  7.     
  8.     thisClip.onPress = function() {
  9.         
  10.         this.startDrag();
  11.     };
  12.     thisClip.onRelease = thisClip.onReleaseOutside = function() {
  13.         this.stopDrag();
  14.         
  15.     };
  16.     
  17.     thisClip.onEnterFrame = function() {
  18.         superCollider(this.otherClip);
  19.     };
  20. }
  21. function superCollider(target){
  22.     target._alpha=100;
  23.     var c=0;
  24.     var xs=0;
  25.     var ys=0;
  26.     
  27.     for (i in this.perim) {
  28.         this.perim.localToGlobal(p={x:this.perim[i]._x,y:this.perim[i]._y})
  29.         
  30.         if(target.hitTest(p.x, p.y, true)){
  31.             target._alpha=50;
  32.         }
  33.         
  34.         ys += p.y;
  35.         xs += p.x;
  36.         c++;
  37.     }
  38. }


Where does the error lie?

[/quote]
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

If I trace (i) in the 'for i in perim' loop inside the collision function, it traces all the instances of the empty movieclips: instance1- instance16, which is nice. If I trace(point); after the loop, I get [object Object] over and over..... which I think must be the problem.

here is the code

Code: [ Select ]
var aClips:Array=["circle_mc","ring_mc"];

this[aClips[0]].otherClip = this[aClips[1]];
this[aClips[1]].otherClip = this[aClips[0]];

for (i in aClips) {
    
    thisClip = this[aClips[i]];
    
    thisClip.onPress = function() {
        
        this.startDrag();
    };

    thisClip.onRelease = thisClip.onReleaseOutside = function() {
        this.stopDrag();
        
    };
    
    thisClip.onEnterFrame = function() {
        superCollider(thisClip.otherClip);
        
    };

}

function superCollider(target):Void{
    

    var c:Number=0;
    var xs:Number=0;
    var ys:Number=0;
    
// circle_mc is instance on stage of an mc containing the perim instance, which contains 16 empty clips around its edge.

    for (i in this.circle_mc.perim) {       
        
        var point:Object = {x:this.perim[i]._x,y:this.perim[i]._y};
        
        this.perim.localToGlobal(point);
        
        if(target.hitTest(point.x, point.y, true)){
            target._alpha=40;
            
        }

        trace(point);       // gives [object Object] [object Object] [object Object] 
        
        ys += point.y;
        xs += point.x;
        c++;
    }
}
  1. var aClips:Array=["circle_mc","ring_mc"];
  2. this[aClips[0]].otherClip = this[aClips[1]];
  3. this[aClips[1]].otherClip = this[aClips[0]];
  4. for (i in aClips) {
  5.     
  6.     thisClip = this[aClips[i]];
  7.     
  8.     thisClip.onPress = function() {
  9.         
  10.         this.startDrag();
  11.     };
  12.     thisClip.onRelease = thisClip.onReleaseOutside = function() {
  13.         this.stopDrag();
  14.         
  15.     };
  16.     
  17.     thisClip.onEnterFrame = function() {
  18.         superCollider(thisClip.otherClip);
  19.         
  20.     };
  21. }
  22. function superCollider(target):Void{
  23.     
  24.     var c:Number=0;
  25.     var xs:Number=0;
  26.     var ys:Number=0;
  27.     
  28. // circle_mc is instance on stage of an mc containing the perim instance, which contains 16 empty clips around its edge.
  29.     for (i in this.circle_mc.perim) {       
  30.         
  31.         var point:Object = {x:this.perim[i]._x,y:this.perim[i]._y};
  32.         
  33.         this.perim.localToGlobal(point);
  34.         
  35.         if(target.hitTest(point.x, point.y, true)){
  36.             target._alpha=40;
  37.             
  38.         }
  39.         trace(point);       // gives [object Object] [object Object] [object Object] 
  40.         
  41.         ys += point.y;
  42.         xs += point.x;
  43.         c++;
  44.     }
  45. }



what is [object Object] ? and where do I go from here?
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

point is a object. You need to trace one of it's attributes, or go through the debug stuff to see it

Post Information

  • Total Posts in this topic: 11 posts
  • Users browsing this forum: No registered users and 45 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.