Zurück   Flashforum > Flash > ActionScript > ActionScript 1

Antwort
 
LinkBack Themen-Optionen Ansicht
Alt 12-09-2005, 13:58   #1 (permalink)
Benjamin Bojko
 
Benutzerbild von XLR8R
 
Registriert seit: Jan 2003
Ort: Berlin
Beiträge: 59
Elastischer Stoß wird manchmal zum schwarzen Loch :)

Servus!

ich möchte für ein Spiel einen elastischen Stoß haben und habe dafür folgendes Script gemacht:

ActionScript:
  1. k = 1 ;
  2. g = 90;
  3. D = 0;
  4.  
  5. dt=1/25
  6.  
  7. width = 550;
  8. height = 400;
  9.  
  10. startDepth = 10;
  11. mcAmount = 15;
  12.  
  13. mcArray = new Array();
  14.  
  15. // hitTest
  16.  
  17. bumper = function(){
  18.     for(i in mcArray){
  19.         for(n in mcArray){
  20.             if( i != n){
  21.                 ho1 = _root[mcArray[i]];
  22.                 ho2 = _root[mcArray[n]];
  23.                 distanceX = Math.abs(ho2._x - ho1._x);
  24.                 distanceY = Math.abs(ho2._y - ho1._y);
  25.                 distance = Math.sqrt(Math.pow(distanceX,2)+Math.pow(distanceY,2));
  26.                
  27.                 if(distance <= ho1.radius + ho2.radius){
  28.                    
  29.                     phi = Math.atan(distanceY/distanceX);
  30.                     gamma = Math.atan(distanceX/distanceY);
  31.  
  32.                     m1 = ho1.m;
  33.                     m2 = ho2.m;
  34.                    
  35.                     v1 = Math.sqrt(Math.pow(ho1.vx,2)+Math.pow(ho1.vy,2));           
  36.                     v2 = Math.sqrt(Math.pow(ho2.vx,2)+Math.pow(ho2.vy,2));
  37.                    
  38.                     f=m1/m2;
  39.                    
  40.                     p1 = m1*v1;
  41.                     p2 = m2*v2;
  42.                    
  43.                     vNeu=(p1+p2)/(m1+m2);
  44.         
  45.                     ho1.vx = vNeu/f*(Math.cos(gamma+Math.PI));
  46.                     ho1.vy = vNeu/f*(Math.sin(gamma+Math.PI));
  47.                     ho2.vx = vNeu*f*(Math.cos(gamma));
  48.                     ho2.vy = vNeu*f*(Math.sin(gamma));
  49.                 }
  50.             }
  51.         }
  52.         _root["schatten"+i]._x = _root["mc"+i]._x;
  53.         if((height-_root["mc"+i]._y)>0){
  54.             _root["schatten"+i]._alpha = _root["schatten"+i]._width = _root["mc"+i]._width*(_root["mc"+i]._y/height)*2;
  55.             _root["schatten"+i]._height = _root["schatten"+i]._width*2/3;
  56.         }
  57.         _root["schatten"+i]._y = height;
  58.     }
  59. }
  60.  
  61. _root.onEnterFrame = function(){
  62.    
  63.     //Mausgeschwindigkeit
  64.     vx_mouse_alt1 = vx_mouse_alt2;
  65.     vy_mouse_alt1 = vy_mouse_alt2;
  66.     vx_mouse_alt2 = _root._xmouse;
  67.     vy_mouse_alt2 = _root._ymouse;
  68.     vx_mouse = ((vx_mouse_alt2-vx_mouse_alt1)/dt);
  69.     vy_mouse = ((vy_mouse_alt2-vy_mouse_alt1)/dt);
  70.    
  71.     //Spacetaste
  72.     if(Key.isDown(Key.SPACE)){
  73.         for(h in mcArray){
  74.             _root[mcArray[h]].F_y = -50000;
  75.             _root[mcArray[h]].F_y = random(50000) - 25000;
  76.         }
  77.     }
  78.    
  79.     //Stoss
  80.     bumper();
  81.    
  82. }
  83.  
  84. MovieClip.prototype.move = function(){
  85.    
  86.     this.x = this._x;
  87.     this.y = this._y;
  88.     this.s_y = 0;
  89.     this.s_x = 0;
  90.     this.vy = 0*(random(100)-50);
  91.     this.vx = 0*(random(100)-50);
  92.    
  93.     this.drag = false;
  94.    
  95.     this.onPress = function(){
  96.         startDrag(this,true);
  97.         this.drag = true;
  98.     }
  99.    
  100.     this.onRelease = this.onReleaseOutside = function(){
  101.         stopDrag();
  102.  
  103.         this.x = _root._xmouse;
  104.         this.y = _root._ymouse;
  105.         this.s_y = 0;
  106.         this.s_x = 0;
  107.         this.vx = vx_mouse;
  108.         this.vy = vy_mouse;
  109.         this.drag = false;
  110.        
  111.     }
  112.    
  113.     this.onRollOver = this.onReleaseOutside = function(){
  114.  
  115.         this.vx += vx_mouse;
  116.         this.vy += vy_mouse;
  117.        
  118.     }
  119.    
  120.     this.onEnterFrame = function() {
  121.        
  122.         if(this.drag == false){
  123.  
  124.             this.dt = dt;
  125.             this.k = k;
  126.             this.g = g;
  127.             this.D = D;
  128.            
  129.             this.Fg = (this.g*this.m);
  130.             this.Fl_y = this.k*Math.ceil(this.vy);
  131.             this.Fl_x = this.k*Math.ceil(this.vx);
  132.            
  133.             //y
  134.             this.y_alt = this._y;
  135.             this.valt_y = this.vy;
  136.             this.salt_y = this.s_y;
  137.             if(Key.isDown(Key.SPACE) == false){
  138.                 this.F_y = -this.D*this.s_y-this.Fl_y+this.Fg;
  139.             }
  140.             this.a_y = this.F_y/this.m;
  141.             this.vy = this.a_y*this.dt+this.valt_y;
  142.             if(this.vy < 1 && this.vy > -1){
  143.                 this.vy = 0;
  144.             }
  145.             this.s_y = this.vy*this.dt+this.salt_y;
  146.             this._y = this.s_y+this.y;
  147.            
  148.             //x
  149.             this.x_alt = this._x;
  150.             this.valt_x = this.vx;
  151.             this.salt_x = this.s_x;
  152.             this.F_x = -this.D*this.s_x-this.Fl_x;
  153.             this.a_x = this.F_x/this.m;
  154.             this.vx = this.a_x*this.dt+this.valt_x;
  155.             if(this.vx < 1 && this.vx > -1){
  156.                 this.vx = 0;
  157.             }
  158.             this.s_x = this.vx*this.dt+this.salt_x;
  159.             this._x = this.s_x+this.x;
  160.            
  161.         }
  162.            
  163.         //Grenzen
  164.        
  165.         //y
  166.         if(this._y + this._height/2 > height && this.vy > 0){
  167.             this.vy *= -1;
  168.         }
  169.         if(this._y + this._height/2 >= height){
  170.             this._y = height - this._height/2;
  171.         }
  172.        
  173.         //x
  174.         if(this._x + this._width/2 > width && this.vx > 0){
  175.             this.vx *= -1;
  176.         }
  177.         if(this._x - this._width/2 < 0 && this.vx < 0){
  178.             this.vx *= -1;
  179.         }
  180.        
  181.     };
  182. };
  183.  
  184. //MCs laden
  185. for(i=0;i<mcAmount;i++){
  186.    
  187.     mcArray.push("mc"+i);
  188.    
  189.     _root.attachMovie("mc","mc"+i,startDepth+mcAmount+i+1);
  190.     _root["mc"+i]._x = random(width);
  191.     _root["mc"+i]._y = random(height);
  192.     _root["mc"+i].m = random(4)+3;
  193.     _root["mc"+i]._width = _root["mc"+i]._height *= _root["mc"+i].m;
  194.     _root["mc"+i].radius = _root["mc"+i]._height/2;
  195.     _root["mc"+i].move();
  196.    
  197.     _root.attachMovie("schatten","schatten"+i,startDepth+i);
  198.    
  199. }
Kompiliert sieht das ganze dann so aus:
http://ikf.proplay.de/upload/xlr8r/bumper.html

Dem ein oder anderen wird es vielleicht schon aufgefallen sein: Manche Kugeln scheinen einen "negativen" Stoß auszuführen und saugen ihre Stoßpartner an anstatt sie abzustoßen

Ich find den fehler leider nicht und hoffe, dass mir da vielleicht jemand helfen kann.

Danke im Voraus - xlr8r


//Edit:
Wirklich relevant dürfte eigentlich nur die function "bumper" sein. Der Rest sind die Bewegungssteuerungen etc, die aber nicht für die Stöße verantwortlich sind.

Geändert von XLR8R (12-09-2005 um 15:45 Uhr)
XLR8R ist offline   Mit Zitat antworten
Alt 12-09-2005, 16:36   #2 (permalink)
Techniker
 
Benutzerbild von hgseib
 
Registriert seit: Sep 2003
Ort: 64807
Beiträge: 16.322
ich geh jetzt natürlich nicht dein ganzes programm durch. aber das da ist mir sofort 'sauer' aufgestossen:
for(i in mcArray){
for(n in mcArray){
du testest i mit n UND n mit i also alles doppelt. das dauert zum einen unnötig lange und kann durchaus deinen fehler verursachen? weil nach dem 1. test die flugrichtung schon gedreht wurde.

mach das besser so:
for (i=0; i<mcArray.length-1; i++)
for (j=i+1; j<mcArray.length; j++)



und damit verfälschst du wohl den winkel:
distanceX = Math.abs



http://www.seibsprogrammladen.de/fra...allistik/flash
step 9 (fall's du es nicht schon kennst ;-)

http://www.seibsprogrammladen.de/fra...ispiele/flash7
-> ballaballa
Kollisionserkennung: Viele Bälle in Kollision plus Stoppbälle.
__________________
die ultimative antwort auf alle programmierfragen: der debugger
mfg h.g.seib www.SeibsProgrammLaden.de

Geändert von hgseib (12-09-2005 um 16:57 Uhr)
hgseib ist offline   Mit Zitat antworten
Alt 13-09-2005, 07:52   #3 (permalink)
Benjamin Bojko
 
Benutzerbild von XLR8R
 
Registriert seit: Jan 2003
Ort: Berlin
Beiträge: 59
Das mit den for Schleifen hat leider nichts gebracht

Die SeibsProgrammLaden Scripte hab ich mir auch schon angeschaut gehabt. Im Prinzip ist das Ballistikbeispiel ziemlich ähnlich mit meinem. Das ballaballa funktioniert zwar geil, hat aber keine Kräfte, Beschleunigung, Reibung usw. implementiert.

Trotzdem danke für deine Hilfe
XLR8R ist offline   Mit Zitat antworten
Alt 13-09-2005, 17:52   #4 (permalink)
Techniker
 
Benutzerbild von hgseib
 
Registriert seit: Sep 2003
Ort: 64807
Beiträge: 16.322
das problem liegt wohl darin, dass sich bei dir die kugeln zu schnell bewegen. somit kann eine kugel pro step über eine andere kugel drüber, oder zumindestens weit in sie hinein gelangen, anstatt das die kugeln an der oberfläche abprallt.
verstärkt wird das problem durch die vielen kugeln die du hast. so kann eine abbrallende kugel regelrecht in eine andere kugel reingeschossen werden.

vielleicht kannst du da noch was abgucken?
www.krazydad.com/bestiary/
__________________
die ultimative antwort auf alle programmierfragen: der debugger
mfg h.g.seib www.SeibsProgrammLaden.de
hgseib ist offline   Mit Zitat antworten
Alt 14-09-2005, 15:36   #5 (permalink)
Benjamin Bojko
 
Benutzerbild von XLR8R
 
Registriert seit: Jan 2003
Ort: Berlin
Beiträge: 59
danke! die links schau ich mir dann man an
XLR8R ist offline   Mit Zitat antworten
Antwort

Lesezeichen

Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind an
Pingbacks sind an
Refbacks sind an



Alle Zeitangaben in WEZ +1. Es ist jetzt 10:46 Uhr.

Domains, Webhosting & Vserver von Host Europe
Unterstützt das Flashforum!
Adobe User Group


Copyright ©1999 – 2012 Marc Thiele