Moteur physique

Soyez le premier à donner votre avis sur cette source.

Vue 6 391 fois - Téléchargée 627 fois

Description

Moteur Physique

import mx.transitions.Tween;
import mx.transitions.easing.*;
import KeyDetection;
// on importe les classe easing pour les effets visuels.
_global.encart_count = 10;
_global.Encarts = new Array();
var keyDet = new KeyDetection();
keyDet.addCombination("automatique", Key.CONTROL, 90);
keyDet.addCombination("manual", Key.CONTROL, 89);
_global.automatique = true;
myObj = new Object();
myObj.onKeyCombination = function(name:String) {
switch (name) {
case "automatique" :
_global.automatique = true;
break;
case "manual" :
_global.automatique = false;
break;
}
};
modebutton.onPress = function() {
_global.automatique = !_global.automatique;
//trace(_global.automatique);
};
keyDet.addListener(myObj);
for (var i = 1; i<=_global.encart_count; i++) {
var current_encart:String = "encart"+i;
duplicateMovieClip(encart0, current_encart, i);
eval(current_encart)._x = eval("encart"+(i-1))._x+eval("encart"+(i-1))._width+20;
eval(current_encart)._y += random(40);
eval(current_encart).back._width = 60+random(40);
eval(current_encart).back._height = 30+random(40);
with (eval(current_encart)) {
createTextField("tbox", 1, 1, 1, 1, 1);
tbox.border = false;
tbox.type = "dynamic";
tbox.autoSize = true;
tbox.embedfonts = true;
tbox.text = i;
Selection.setFocus(tbox);
myf = new TextFormat();
myf.color = 0x000000;
myf.font = "Arial";
myf.size = 18;
myf.align = "center";
tbox.setTextFormat(myf);
}
_global.Encarts.push({clip:eval("encart"+(i-1)), px:eval("encart"+(i-1))._x, py:eval("encart"+(i-1))._y});
}
_global.Targets = new Array();
var Page = new Object();
Page.Target = _root.page1;
Page.ClipsIn = new Array();
var BestPlace = new Object();
BestPlace.px = 0;
BestPlace.py = 0;
Page.BestPlace = BestPlace;
_global.Targets.push(Page);
Page = new Object();
Page.Target = _root.page2;
Page.ClipsIn = new Array();
_global.Targets.push(Page);
Page = new Object();
Page.Target = _root.page3;
Page.ClipsIn = new Array();
_global.Targets.push(Page);
//, _root.page2
function getsurface(MC:MovieClip) {
return MC._width*MC._height;
}
function getavailablesurface(MC:MovieClip, clips:Array) {
var cumul = 0;
for (var i = 0; i<clips.length; i++) {
//trace2(clips[i]);
var obj:Object = clips[i].clip;
cumul += obj._width*obj._height;
}
//trace2(getsurface(MC)-cumul);
return getsurface(MC)-cumul;
}
function getbestplace(Current_Clip:MovieClip, Target_Clip:MovieClip, clips:Array, BestPlace:Object) {
Current_Clip.stopDrag();
oldX = Current_Clip._x;
oldY = Current_Clip._y;
var startX:Number = Number(Math.round(Target_Clip._x));
var endX:Number = Number(Math.round(Target_Clip._x+Target_Clip._width));
var startY:Number = Number(Math.round(Target_Clip._y));
var endY:Number = Number(Math.round(Target_Clip._y+Target_Clip._height));
pasy = 1;
yy = startY;
xx = startX;
/*
var Touch = false;
Current_Clip._x = BestPlace.px;
Current_Clip._y = BestPlace.py;
for (l=0; l<clips.length && !Touch; l++) {
//x++;
var obj:Object = clips[l].clip;
if (Current_Clip.hitTest(obj) && Current_Clip != obj) {
Touch = true;
}
if (Current_Clip._x+Current_Clip._width>=endX) {
Touch = true;
BestPlace.px = 0;
BestPlace.py = clips[l+1].clip._y;
}
if (Current_Clip._y+Current_Clip._height>=endY) {
Touch = true;
}
}*/
if (clips.length == 0) {
var obj:Object = new Object();
obj.px = xx;
obj.py = yy;
return obj;
}
if (_global.automatique) {
k = 0;
avant = true;
for (i=startY; i<endY; i++) {
trace2(i);
for (j=startX; j<endX; ) {
k++;
var Touch = false;
for (l=0; l<clips.length && !Touch; l++) {
//x++;
var obj:Object = clips[l].clip;
Current_Clip._x = j;
Current_Clip._y = i;
if (Current_Clip.hitTest(obj) && Current_Clip != obj) {
Touch = true;
}
if (Current_Clip._x+Current_Clip._width>=endX) {
avant = false;
Touch = true;
}
if (Current_Clip._y+Current_Clip._height>=endY) {
Touch = true;
}
}
if (!Touch) {
//il faut retourner
Current_Clip._x = oldX;
Current_Clip._y = oldY;
var obj:Object = new Object();
obj.px = j;
obj.py = i;
trace2("trouvée iteration : "+k+" x : "+j+" y :"+i);
return obj;
} else {
trace2("conflit avec le clip : "+obj);
j += obj._width+1;
}
}
}
trace2("non trouvée iteration : "+k);
return undefined;
} else {
var Touch = false;
for (l=0; l<clips.length && !Touch; l++) {
//x++;
var obj:Object = clips[l].clip;
if (Current_Clip.hitTest(obj) && Current_Clip != obj) {
Touch = true;
}
if (Current_Clip._x+Current_Clip._width>=endX) {
avant = false;
Touch = true;
}
if (Current_Clip._y+Current_Clip._height>=endY) {
Touch = true;
}
}
if (!Touch) {
trace2("trouvée ");
var obj:Object = new Object();
obj.px = Current_Clip._x;
obj.py = Current_Clip._y;
Current_Clip.stopDrag();
return obj;
} else {
trace2("conflit avec le clip : "+obj);
}
return undefined;
}
}
function findInArray(MC:MovieClip, clips:Array) {
for (var i = 0; i<clips.length; i++) {
if (MC == clips[i].clip) {
return i;
}
}
return -1;
}
function removeFromArray(obj:Object, clips:Array) {
tmp = new Array();
for (var i = 0; i<clips.length; i++) {
if (clips[i].clip != obj) {
tmp.push(clips[i]);
}
}
return tmp;
}
function goAway(MC:MovieClip, Targets:Array) {
//trace2("goAway");
//var obj:Object = clipsMap.findKey("encart"+substring(MC, 15, 2),"name");
var obj:Object = _global.Encarts[findInArray(MC, _global.Encarts)];
//trace2(obj.px+" "+obj.py);
var maTween:Tween = new Tween(MC, "_x", Regular.easeOut, MC._x, obj.px, 20, false);
var maTween:Tween = new Tween(MC, "_y", Regular.easeOut, MC._y, obj.py, 20, false);
//trace2("Targets[i].clip -------------------------: "+Targets.join());
return removeFromTargets(MC, Targets);
}
function goTarget(MC:MovieClip, Targets:Array) {
//trace2("goTarget");
//var obj:Object = clipsMap.findKey("encart"+substring(MC, 15, 2),"name");
var obj:Object = Targets[findInArray(MC, Targets)];
trace2(Targets[findInArray(MC, Targets)].px);
//trace2(obj.px+" "+obj.py);
var maTween:Tween = new Tween(MC, "_x", Regular.easeOut, MC._x, obj.px, 20, false);
var maTween:Tween = new Tween(MC, "_y", Regular.easeOut, MC._y, obj.py, 20, false);
//return removeTarget(MC, Targets);
}
function removeFromTargets(MC:MovieClip, Targets:Array) {
NotFound = true;
for (var i = 0; i<Targets.length && NotFound; i++) {
// trace2("---------------"+findInArray(MC, Targets[i].ClipsIn));
if (findInArray(MC, Targets[i].ClipsIn) != -1) {
// trace2("---------------found");
NotFound = false;
Targets[i].ClipsIn = removeFromArray(MC, Targets[i].ClipsIn);
}
}
return Targets;
}
function getTargetIndex(MC:MovieClip, Targets:Array) {
NotFound = true;
Index = -1;
for (var i = 0; i<Targets.length && NotFound; i++) {
if (findInArray(MC, Targets[i].ClipsIn) != -1) {
Index = i;
NotFound = false;
}
}
return Index;
}
function targetNotExiste(MC:MovieClip, Targets:Array) {
for (var i = 0; i<Targets.length; i++) {
if (MC == Targets[i].clip) {
//trace2("found");
return false;
}
}
return true;
}
function dropTarget(MC:MovieClip, Targets:Array) {
for (var i = 0; i<Targets.length; i++) {
if (eval(MC._droptarget) == Targets[i]) {
return Targets[i];
}
}
return undefined;
}
ReleaseTarget = function () {
};
function optimizePlace(newX:Number, newY:Number, Index:Number, Targets:Array) {
trace2("OPTIMIZE : ------------------------- Targets[Index].Target "+Targets[Index].ClipsIn);
/*for (var i = 0; i<Targets.length; i++) {
for (var j = 0; j<Targets.length; j++) {
var objGetBestPlace:Object = getbestplace(_global.Targets[i].ClipsIn[j].clip, _global.Targets[i].Target, _global.Targets[i].ClipsIn);
trace2("objGetBestPlace x : "+objGetBestPlace.px+" y : "+objGetBestPlace.py);
var maTween:Tween = new Tween(_global.Targets[i].ClipsIn[j].clip, "_x", Regular.easeOut, _global.Targets[i].ClipsIn[j].clip._x, objGetBestPlace.px, 20, false);
var maTween:Tween = new Tween(_global.Targets[i].ClipsIn[j].clip, "_y", Regular.easeOut, _global.Targets[i].ClipsIn[j].clip._y, objGetBestPlace.py, 20, false);
}
}*/
//newX -= startX;
//newY -= startY;
Target_Clip = Targets[Index].Target;
var startX:Number = Number(Math.round(Target_Clip._x));
var endX:Number = Number(Math.round(Target_Clip._x+Target_Clip._width));
var startY:Number = Number(Math.round(Target_Clip._y));
var endY:Number = Number(Math.round(Target_Clip._y+Target_Clip._height));
//newX=startX
//newY=startY
for (var i = 0; i<1; i++) {
var Current_Clip:MovieClip = Targets[Index].ClipsIn[i].clip;
trace2("OPTIMIZE : -------------------------"+Current_Clip);
oldX = Current_Clip._x;
oldY = Current_Clip._y;
trace2("new --- "+newX+" "+newY);
Current_Clip._x = newX;
Current_Clip._y = newY;
//Targets[Index].ClipsIn.length
for (var j = 0; j<Targets[Index].ClipsIn.length; j++) {
Touch = false;
if (i != j) {
var obj:Object = Targets[Index].ClipsIn[j].clip;
if (Current_Clip.hitTest(obj)) {
Touch = true;
}
if (Current_Clip._x+Current_Clip._width>endX) {
Touch = true;
}
if (Current_Clip._y+Current_Clip._height>endY) {
Touch = true;
}
}
if (!Touch) {
//Current_Clip._x = oldX;
//Current_Clip._y = oldY;
var maTween:Tween = new Tween(Current_Clip, "_x", Regular.easeOut, oldX, newX, 20, false);
var maTween:Tween = new Tween(Current_Clip, "_y", Regular.easeOut, oldY, newY, 20, false);
Targets[Index].ClipsIn[i].px = newX;
Targets[Index].ClipsIn[i].py = newY;
newX = oldX;
newY = oldY;
//return optimizePlace(newX, newY, Index, Targets);
}
}
}
/*for (var i = 0; i<_global.Targets[Index].ClipsIn.length; i++) {
Current_Clip = Targets[Index].ClipsIn[i].clip;
var objGetBestPlace:Object = getbestplace(Current_Clip, _global.Targets[Index].Target, _global.Targets[Index].ClipsIn);
if (objGetBestPlace != undefined) {
// this.stopDrag();
//trace2(objGetBestPlace+" x : "+objGetBestPlace.px+" y : "+objGetBestPlace.py);
_global.Targets[Index].ClipsIn.push({clip:Current_Clip, px:objGetBestPlace.px, py:objGetBestPlace.py});
var maTween:Tween = new Tween(Current_Clip, "_x", Regular.easeOut, Current_Clip._x, objGetBestPlace.px, 20, false);
var maTween:Tween = new Tween(Current_Clip, "_y", Regular.easeOut, Current_Clip._y, objGetBestPlace.py, 20, false);
} else {
trace2("OPTIMIZE : there is no fit surface return to first place");
//_global.Targets = goAway(this, _global.Targets);
}
}*/
return Targets;
}
for (var i = 1; i<=_global.encart_count; i++) {
var current_encart:String = "encart"+i;
//clipsMap.addItem({name:current_encart, px:eval(current_encart)._x,py:eval(current_encart)._y});
clipsMap.push(eval(current_encart));
eval(current_encart).onPress = function() {
this.startDrag();
};
eval(current_encart).onRelease = function() {
this.stopDrag();
var Target:MovieClip;
NoTarget = true;
for (var j = 0; j<_global.Targets.length && NoTarget; j++) {
//trace2("eval(this._droptarget) == _global.Targets[j].Target : "+eval(this._droptarget) +" "+_global.Targets[j].Target);
if (eval(this._droptarget) == _global.Targets[j].Target) {
NoTarget = false;
//trace2("targetNotExiste(this, _global.Targets[j].ClipsIn : "+targetNotExiste(this, _global.Targets[j].ClipsIn));
if (targetNotExiste(this, _global.Targets[j].ClipsIn)) {
if (getsurface(this)<=getavailablesurface(_global.Targets[j].Target, _global.Targets[j].ClipsIn)) {
var objGetBestPlace:Object = getbestplace(this, _global.Targets[j].Target, _global.Targets[j].ClipsIn, _global.Targets[j].BestPlace);
if (objGetBestPlace != undefined) {
this.stopDrag();
trace2(objGetBestPlace+" x : "+objGetBestPlace.px+" y : "+objGetBestPlace.py);
_global.Targets[j].ClipsIn.push({clip:this, px:objGetBestPlace.px, py:objGetBestPlace.py});
if (_global.automatique) {
var maTween:Tween = new Tween(this, "_x", Regular.easeOut, this._x, objGetBestPlace.px, 20, false);
var maTween:Tween = new Tween(this, "_y", Regular.easeOut, this._y, objGetBestPlace.py, 20, false);
}
} else {
trace2("there is no fit surface return to first place");
_global.Targets = goAway(this, _global.Targets);
}
} else {
trace2("there is no available surface return to first place");
_global.Targets = goAway(this, _global.Targets);
}
//break;
} else {
trace2("encart already existe return to target"+this);
//goTarget(this, _global.Targets[j].ClipsIn);
var objGetBestPlace:Object = getbestplace(this, _global.Targets[j].Target, _global.Targets[j].ClipsIn, _global.Targets[j].BestPlace);
if (objGetBestPlace != undefined) {
this.stopDrag();
//trace2(objGetBestPlace+" x : "+objGetBestPlace.px+" y : "+objGetBestPlace.py);
var index = findInArray(this, _global.Targets[j].ClipsIn);
_global.Targets[j].ClipsIn[index].px = objGetBestPlace.px;
_global.Targets[j].ClipsIn[index].py = objGetBestPlace.py;
if (_global.automatique) {
var maTween:Tween = new Tween(this, "_x", Regular.easeOut, this._x, objGetBestPlace.px, 20, false);
var maTween:Tween = new Tween(this, "_y", Regular.easeOut, this._y, objGetBestPlace.py, 20, false);
}
} else {
trace2("there is no fit surface return to old place in target");
//_global.Targets = goAway(this, _global.Targets);
goTarget(this, _global.Targets[j].ClipsIn);
}
}
}
//break;
}
if (NoTarget) {
Index = getTargetIndex(this, _global.Targets);
var obj:Object = _global.Targets[Index].ClipsIn[findInArray(this, _global.Targets[Index].ClipsIn)];
var oldX:Number = obj.px;
var oldY:Number = obj.py;
trace2("No Target return to first place ");
_global.Targets = goAway(this, _global.Targets);
/*if (Index>=0) {
trace2(" old --- "+oldX+" "+oldY);
_global.Targets = optimizePlace(oldX, oldY, Index, _global.Targets);
}*/
}
};
}
function trace2(msg) {
//trace(msg);
_root.traceclip.text = msg+'\n'+_root.traceclip.text;
//_root.traceclip.scroll+=1000000;
}

Source / Exemple :


Moteur Physique

Conclusion :


Moteur Physique

Codes Sources

A voir également

Ajouter un commentaire Commentaires
choinet
Messages postés
89
Date d'inscription
mardi 8 février 2005
Statut
Membre
Dernière intervention
3 novembre 2008

4 déc. 2007 à 23:31
Tlecoz a raison, ça calcule trop. Je suis une quiche en AS donc je ne critiquerais pas ton code mais si au moins il y avait un minimum de commentaire, je serais moins perdu.

Ceci dit, 415 lignes de code c'est effectivement un bel effort

Choinet
tlecoz
Messages postés
40
Date d'inscription
mardi 13 janvier 2004
Statut
Membre
Dernière intervention
9 décembre 2007

1 déc. 2007 à 16:23
Hello,

J'avous ne pas avoir pris le temps de lire le code, je me suis contenté de regarder le swf. D'une part, je ne vois pas trop dans quel cas appliquer ton anim, d'autre part le temps de calcul est enorme ! C'est casiment instantané sur les 4 premiere elements, mais ca prend plus de 5 seconde a partir du 7eme blocs....Pas trés exploitable quoi !

je te suggère d'aller voir du coté de APE (Actionscript Physic Engine) :
http://www.cove.org/ape/demo1.htm

ou Motors :
http://lab.polygonal.de/category/physics/

ou encore Box2DFlash qui vient de sortir et qui m'a l'air assez genial :) :
http://box2dflash.sourceforge.net/

Tu verras que c'est quand meme un autre niveau (sans vouloir t'offenser)

2/10 pour l'effort

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.