Rebond dans un cercle

firfelin Messages postés 16 Date d'inscription lundi 26 janvier 2004 Statut Membre Dernière intervention 23 août 2004 - 6 juil. 2004 à 15:36
blackwizzard Messages postés 1258 Date d'inscription mercredi 21 mars 2001 Statut Membre Dernière intervention 21 juin 2009 - 19 juil. 2004 à 16:57
Rebonjour,
Encore un truc qui devrait être tout simple et que j'arrive pas à faire... O_o
Comment faire pour faire rebondir des trucs à l'intérieur d'un cercle? Un rectangle, ça j'arrive sans problème, mais un cercle, je vois pas...
Des idées, des exemples?
Merci

Mangez un castor, sauvez un arbre

3 réponses

blackwizzard Messages postés 1258 Date d'inscription mercredi 21 mars 2001 Statut Membre Dernière intervention 21 juin 2009 2
16 juil. 2004 à 02:43
j'avais codé un proto a partir d'une source d'alcys...
etant donné que c'est un proto, ça fonctionnera que sous MX6, sauf si tu compile en AS1 sous 2004.

donc, sur le _root, tu met ça:

_root.aTest = new Array();
function init_rebond(object, bool) {
object.vx = 2;
object.vy = -2;
object.k = -1;
object.r = 10;
object.r2 = object.r - 2;
object.inca = 0.08;
object.drag = false;
object.hit = bool;
}
function rebond(object, map) {
if (!object.drag) {
anx = object._x;
any = object._y;
long = Math.sqrt(object.vx * object.vx + object.vy * object.vy);
nn = Math.floor(long / object.r2);
px = object.vx * object.r2 / long;
py = object.vy * object.r2 / long;
for (i = 1; i <= nn + 1; i++) {
if (i <= nn) {
x = anx + i * px;
y = any + i * py;
} else {
x = anx + object.vx;
y = any + object.vy;
}
if (map.hitTest(x, y, true) == object.hit) {
anx += (i - 1) * px;
any += (i - 1) * py;
rebondir(object, map);
break;
} else {
if (i == nn + 1) {
afficher(object, map);
}
}
}
} else {
anx = x;
any = y;
x = object._x;
y = object._y;
object.vx = x - anx;
object.vy = y - any;
}
}
angle = function (aa, inc, object, map) {
do {
t1 = map.hitTest(anx + Math.cos(aa) * object.r, any + Math.sin(aa) * object.r, true);
t2 = map.hitTest(anx + Math.cos(aa + inc) * object.r, any + Math.sin(aa + inc) * object.r, true);
aa += inc;
} while ((aa < 6.28) && ((t1 && t2) || (!t1 && !t2)));
return (t1 != object.hit) ? aa - inc : aa;
};
rebondir = function (object, map) {
for (i = 1; i < 3; i++) {
x0 = (x + anx) / 2;
y0 = (y + any) / 2;
if (map.hitTest(x0, y0, true) == object.hit) {
x = x0;
y = y0;
} else {
anx = x0;
any = y0;
}
}
a = angle(0, object.inca, object, map);
if ((aa >= 6.28) || (t1 && t2) || (!t1 && !t2)) {
object.vx *= -1;
object.vy *= -1;
x = anx;
y = any;
} else {
ca = Math.cos(a);
sa = Math.sin(a);
tpx = -sa * object.k * (-object.vx * sa + object.vy * ca) + ca * (object.vx * ca + object.vy * sa);
object.vy = ca * object.k * (-object.vx * sa + object.vy * ca) + sa * (object.vx * ca + object.vy * sa);
object.vx = tpx;
long = Math.sqrt(object.vx * object.vx + object.vy * object.vy);
}
afficher(object, map);
};
afficher = function (object, map) {
object._x = anx + object.vx;
object._y = any + object.vy;
};
/*##########################################################################
FIN REBOND
*/
function addtoActives(object) {
_root.aTest.push(object);
}
function checkActives(object) {
for (i = 0; i <= (_root.aTest.length); i++) {
obj = eval(_root.aTest[i]);
_root.rebond(object, obj);
//_root.konsole.text = _root.aTest.toString();
}
}


puis, dans le code du clip censé rebondir:

onClipEvent(enterFrame) {
_root.rebond(this, _root.map);
}
onClipEvent(load) {
_root.init_rebond(this,true);
}


Pour que ça fonctionne, donc compile en AS1, et appele le clip qui sert de limite aux rebond "_root.map"

amuse toi...
[BlackWizzard] http://www.blackwizzard.net
0
firfelin Messages postés 16 Date d'inscription lundi 26 janvier 2004 Statut Membre Dernière intervention 23 août 2004
19 juil. 2004 à 09:34
Une belle solution comme ça, servie sur un plateau:-)
Que dire..., à part merci infiniment. Tu viens de gagner ta place dans mon panthéon personnel:-) Il m'aurait fallu des années pour commencer à entrapercevoir un début d'esquisse de possibilité aussi bien foutue...

Mangez un castor, sauvez un arbre
0
blackwizzard Messages postés 1258 Date d'inscription mercredi 21 mars 2001 Statut Membre Dernière intervention 21 juin 2009 2
19 juil. 2004 à 16:57
lol
c alsys qu'il faut remercier! moi je n'ai fait que transformer son code en proto ;)
[BlackWizzard] http://www.blackwizzard.net
0
Rejoignez-nous