Zurück   Flashforum > Flash > ActionScript > Softwarearchitektur und Entwurfsmuster

Antwort
 
LinkBack Themen-Optionen Ansicht
Alt 08-05-2003, 19:45   #1 (permalink)
God made me funky..
 
Registriert seit: Apr 2003
Ort: Bremen
Beiträge: 1.067
Steering Behaviours - Part II

Edit:
Nochmal ein paar kleine Änderungen eingefügt


Sodele, die Langeweile hat wieder zugeschlagen und somit Teil 2, jetzt auch mit Kollisionsvermeidung.

anschauen
haben

bei Copy & Paste wird noch ein leerer Mc benötigt, welcher mit dem Namen "critter" gelinkt ist.

ActionScript:
  1. Math.DEGREE_IN_RAD = 0.0174532925199433;
  2.  
  3. Movieclip.prototype.drawCircle = function(r, x, y) {
  4.     var rx = r+x, ry = r+y, xr = x-r, yr = y-r;
  5.     var r07 = r*0.7071, r04 = r*0.4142;
  6.     this.moveTo(rx, y);
  7.     this.curveTo(rx, r04+y, r07+x, r07+y);
  8.     this.curveTo(r04+x, ry, x, ry);
  9.     this.curveTo(x-r04, ry, x-r07, r07+y);
  10.     this.curveTo(xr, r04+y, xr, y);
  11.     this.curveTo(xr, y-r04, x-r07, y-r07);
  12.     this.curveTo(x-r04, yr, x, yr);
  13.     this.curveTo(r04+x, yr, r07+x, y-r07);
  14.     this.curveTo(rx, y-r04, rx, y);
  15. };
  16.  
  17. function Critter() {
  18.     this.init();
  19. }
  20.  
  21. Critter.prototype = new MovieClip();
  22.  
  23. o = Critter.prototype;
  24.  
  25.  
  26. o.init = function() {
  27.     this.radius = 4;
  28.     this.clr = 0x000000;
  29.     this.rate = 10; // bestimmt wie schnell sich die Bewegungsrichtung ändert
  30.     this.steeringCircleRadius = 10 // bestimmt wie groß die Änderung maximal ist
  31.     this.steeringCircleDistance = 80; // bestimmt die Auswirkung der Änderung
  32.     this.force = 10; // bestimmt wie schnell das Teil hier ist
  33.     this.distanceForCollisionCheck = this.force * 4; //wie weit vorraus soll gecheckt werden
  34.    
  35.     //für das verhalten unwichtig, dient nur zur Anzeige
  36.     // der letzten Kollisionspunkte
  37.     this.collisionCount = 0;
  38.     this.maxPointsOfCollisionstoDraw = 10;
  39.    
  40.    
  41.     this.steeringVector = {};
  42.     this.targetVector = {};
  43.     //Umgebung-->sollte via set gesetzt werden
  44.     this.environment = _root.environment;
  45.    
  46.     //neuer Richtungsvektor
  47.     var x = Math.random() * Stage.width;
  48.     var y = Math.random() * Stage.height;
  49.     x *= (Math.random() > 0.5) ? -1 : 1;
  50.     y *= (Math.random() > 0.5) ? -1 : 1;
  51.    
  52.     //normalisieren
  53.     var l = Math.sqrt((x * x) + (y * y));
  54.     x *= 1 / l;
  55.     y *= 1 / l;
  56.     //auf Kreis projezieren
  57.     x *= this.steeringCircleRadius;
  58.     y *= this.steeringCircleRadius;
  59.     x += this.steeringCircleDistance
  60.    
  61.     //Steuerungsvektor festlegen
  62.     this.steeringVector.x = x;
  63.     this.steeringVector.y = y;
  64.    
  65.     //und action...
  66.     this.onEnterFrame = this.think;
  67. }
  68.  
  69. o.think = function() {
  70.     //var t = getTimer();
  71.     if(this.collisionCount > this.maxPointsOfCollisionsToDraw) {
  72.         _root.clear();
  73.         updateAfterEvent();
  74.         this.collisionCount = 0;
  75.     }
  76.     this.newTarget();
  77.     if(this.detectPossibleCollision()) {
  78.         this.collisionCount++;
  79.         this.rotateToCleanTarget(this.getCurrentRotationInRad());
  80.     }
  81.     this.seek();
  82.     this.paint();
  83.     //trace(getTimer() - t);
  84. }
  85.  
  86. o.paint = function() {
  87.     this.clear();
  88.    
  89.     //Kreis auf den der Steuerrungsvektor projeziert wird zeichen
  90.     this.lineStyle(1,0x000000);
  91.     this.drawCircle(this.steeringCircleRadius,this.steeringCircleDistance, 0);
  92.    
  93.     //kleinen Punkt zeichnen
  94.     this.beginFill(this.clr);
  95.     this.drawCircle(this.radius, 0, 0);
  96.     this.endFill();
  97.    
  98.     //den Steuerungsvektor zeichnen
  99.     this.moveTo(0,0)
  100.     this.lineStyle(1,0xFF0000);
  101.     this.lineTo(this.steeringVector.x, this.steeringVector.y)
  102. }
  103.  
  104. o.newTarget = function() {
  105.     //neuer Richtunsvektor
  106.     var x = Math.random() * Stage.width;
  107.     var y = Math.random() * Stage.height;
  108.     x *= (Math.random() > 0.5) ? -1 : 1;
  109.     y *= (Math.random() > 0.5) ? -1 : 1;
  110.    
  111.     //wenn Länge größer als maximal erlaubte Änderung, skalieren
  112.     var l = Math.sqrt((x * x) + (y * y));
  113.     if(l > this.rate) {
  114.         x *= 1 / l;
  115.         y *= 1 / l;
  116.         x *= this.rate;
  117.         y *= this.rate;
  118.     }
  119.    
  120.     //den alten hinzu addieren
  121.     x += this.steeringVector.x;
  122.     y += this.steeringVector.y;
  123.    
  124.     //wieder auf den Kreis projezieren
  125.     x -= this.steeringCircleDistance;
  126.     l = Math.sqrt((x * x) + (y * y));
  127.     x *= 1 / l;
  128.     y *= 1 / l;
  129.     x *= this.steeringCircleRadius;
  130.     y *= this.steeringCircleRadius;
  131.     x += this.steeringCircleDistance;
  132.    
  133.     //neuer Steuerungsvektor
  134.     this.steeringVector.x = x;
  135.     this.steeringVector.y = y;
  136.    
  137.     //in Weltkoordinaten umrechnen
  138.     var p  = {x: x, y: y};
  139.     this.localToGlobal(p);
  140.     this.targetVector.x = Math.round(p.x);
  141.     this.targetVector.y = Math.round(p.y);
  142. }
  143.  
  144. o.seek = function() {
  145.     //Winkel zum Ziel in rad
  146.     var rad = this.getCurrentRotationInRad();
  147.    
  148.     //rotation anpassen
  149.     this._rotation = (rad * 180) / Math.PI;
  150.    
  151.     //position updaten
  152.     this._x += this.getAppliedForce() * Math.cos(rad);
  153.     this._y += this.getAppliedForce() * Math.sin(rad);
  154.    
  155.     //wrap around
  156.     this._x %= Stage.width;
  157.     this._y %= Stage.height;
  158.    
  159.     if(this._x < 0) {
  160.         this._x = Stage.width;
  161.     }
  162.     if(this._y < 0) {
  163.         this._y = Stage.height;
  164.     }
  165. }
  166.  
  167. o.detectPossibleCollision = function() {
  168.     //neue Position bestimmen
  169.     var pt = this.getPossiblePointOfCollision(this.getCurrentRotationInRad());
  170.     //schauen ob diese innerhalb eines Hindernisses liegt
  171.     if(this.environment.hitTest(pt.x,pt.y, true)){
  172.         this.drawPointOfCollision(pt.x, pt.y);
  173.         return true;
  174.     }
  175.     return false;
  176. }
  177.  
  178. o.rotateToCleanTarget = function(rad) {
  179.     //um jeweils 2 Grad pro Aufruf drehen
  180.     rad += Math.DEGREE_IN_RAD * 2
  181.     var pt = this.getPossiblePointOfCollision(rad);
  182.     if(this.environment.hitTest(pt.x, pt.y, true)) {
  183.         this.drawWrongDecision(pt.x, pt.y)
  184.         this.rotateToCleanTarget(rad)
  185.     } else {
  186.         this.targetVector.x = pt.x;
  187.         this.targetVector.y = pt.y;
  188.     }
  189. }
  190.  
  191. o.drawPointOfCollision = function(x, y) {
  192.     _root.lineStyle(1,0xFF0000);
  193.     _root.drawCircle(this.radius,x, y);
  194. }
  195.  
  196. o.drawWrongDecision = function(x, y) {
  197.     _root.moveTo(this._x, this._y);
  198.     _root.lineStyle(1,0x0000FF);
  199.     _root.lineTo(x, y);
  200. }
  201.  
  202. o.getCurrentRotationInRad = function() {
  203.     var dx = this.targetVector.x - this._x;
  204.     var dy = this.targetVector.y - this._y;
  205.     return Math.atan2(dy, dx);
  206. }
  207.  
  208. o.getPossibleTarget = function(rad) {
  209.     var nx = this._x + (this.getAppliedForce() * Math.cos(rad));
  210.     var ny = this._y + (this.getAppliedForce() * Math.sin(rad));
  211.     return {x: nx, y: ny};
  212. }
  213.  
  214. o.getPossiblePointOfCollision = function(rad) {
  215.     var nx = this._x + (this.distanceForCollisionCheck * Math.cos(rad));
  216.     var ny = this._y + (this.distanceForCollisionCheck * Math.sin(rad));
  217.     return {x: nx, y: ny};
  218. }
  219.  
  220. o.getAppliedForce = function() {
  221.     //distanz zum Ziel
  222.     var d = this.getDistanceToTarget();
  223.     var possibleForce = Math.round(d / 2);
  224.     return (possibleForce > this.force) ? this.force : possibleForce;
  225. }
  226.  
  227. o.getDistanceToTarget = function() {
  228.     var dx = this.targetVector.x - this._x;
  229.     var dy = this.targetVector.y - this._y;
  230.     return Math.sqrt((dx * dx) + (dy * dy));
  231. }
  232. delete o;
  233.  
  234. Object.registerClass("critter",Critter);
  235.  
  236. c = _root.attachMovie("critter", "c", 1);
  237. c._x = 200;
  238. c._y = 200;
  239. stop();
__________________
Kunst ist in erster Linie eine Frage der Form und nicht des Inhalts
Paul Rand

Geändert von e2e4 (08-05-2003 um 22:31 Uhr)
e2e4 ist offline   Mit Zitat antworten
Alt 09-05-2003, 00:15   #2 (permalink)
◘ ◘
 
Benutzerbild von beachmeat
 
Registriert seit: Dec 2001
Ort: Amsterdam
Beiträge: 6.126
Schöner Code!

Werd ich mir mal zu gemüte führen
__________________
jeden Tag frisch
beachmeat ist offline   Mit Zitat antworten
Alt 11-05-2003, 11:48   #3 (permalink)
God made me funky..
 
Registriert seit: Apr 2003
Ort: Bremen
Beiträge: 1.067
Weiter gehts, keine neuen Funktionen aber aufgeräumt, damit das alles mal Sinn macht.

Source zum anschauen:
extensions
Vector
DataHolder
AbstractBehaviour
Wanderer
ObstacleAvoidance
Vehicle

Das ganze in Aktion und als Paketdownload:
anschauen
haben

Beispiel:

-- leerer MC exportiert als "critter"
-- MC in Bühnengröße welcher die Hindernisse beinhaltet: Instanzname "environment"

ActionScript:
  1. #include "net.e2e4.extensions.as"
  2. #include "net.e2e4.tools.Vector.as"
  3. #include "net.e2e4.steering.VBDHolder.as"
  4. #include "net.e2e4.steering.Behaviour.as"
  5. #include "net.e2e4.steering.Wanderer.as"
  6. #include "net.e2e4.steering.Avoidance.as"
  7. #include "net.e2e4.steering.Vehicle.as"
  8.  
  9. c = _root.attachMovie("critter", "c", 1);
  10.  
  11. //behaviourName, weight
  12. c.attachBehaviour("Wanderer", 0.1);
  13. c.attachBehaviour("ObstacleAvoidance", 0.9);
  14. c._x = 200;
  15. c._y = 200;
  16.  
  17. stop();
__________________
Kunst ist in erster Linie eine Frage der Form und nicht des Inhalts
Paul Rand
e2e4 ist offline   Mit Zitat antworten
Alt 11-05-2003, 11:53   #4 (permalink)
helpQLODhelp
 
Benutzerbild von bokel
 
Registriert seit: Feb 2002
Ort: Köln
Beiträge: 8.505
Guten Morgen e2e4,

Das sieht ja alles sehr vielversprechend aus. Ich habe mir erlaubt den Thread mal in die Liste "wichtiger Threads" zu verlinken.

mfg r.
bokel ist offline   Mit Zitat antworten
Alt 02-10-2003, 16:00   #5 (permalink)
Neuer User
 
Registriert seit: Jan 2003
Beiträge: 172
Ehrlich gesagt ist das Ding mir noch zuuuu ungenau. 5x Durch die Wand in einer halben minute
R3PL4Y 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 15:45 Uhr.

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


Copyright ©1999 – 2012 Marc Thiele