var omegaMouse, nPlanRose, mouse, centreOmega;
var impactRayonMouse, laTrans, centre;
createCanvas(800, 800, WEBGL);
centre2O=0.5*height/tan(PI/6.0);
impactRayonMouse = createVector(0, 0, 0);
angles = createVector(0, 0, 0);
angleR = createVector(PI/6, PI/3, 0);
laTrans = createVector(150, -160, -1500);
centre=createVector(0, 0, 0);
mobiles[0]= new Mobile(-300, -300, 0);
mobiles[1]= new Mobile(300, 350, 0);
mobiles[2]= new Mobile(400, -300, 0);
centreOmega=createVector(0, 0, centre2O);
mouse=createVector(-width/2.0+mouseX, mouseY-height/2.0, 0);
omegaMouse=createVector(-width/2.0+mouseX, mouseY-height/2.0, -centre2O);
nPlanRose=local2Global(createVector(0, 0, 100));
translate(laTrans.x, laTrans.y, laTrans.z);
for (var i=0; i<3; i++) {
if (mouseButton == RIGHT && choix==false) {
impactRayonMouse=intersection( omegaMouse, nPlanRose);
for (var i0=0; i0<3; i0++) {
var vecdis=p5.Vector.sub(impactRayonMouse,mobiles[i0].global);
var dis=p5.Vector.mag(vecdis);
if (numero<3 && dist0<100) {
if (choix==true && mouseButton==RIGHT) {
impactRayonMouse=intersection( omegaMouse, nPlanRose);
dessinI(impactRayonMouse);
mobiles[numero].global=comb(0.95, mobiles[numero].global, 0.05, impactRayonMouse);
mobiles[numero].display();
if ( mouseButton==LEFT) {
angles.x=map(mouse.y, 0, height, -0.5, 0.5);
angles.y=map(-mouse.x, 0, width, -0.5, 0.5);
angleR=comb(0.9, angleR, 0.1, angles);
for (var i=0; i<3; i++) {
mobiles[i].calculG(angleR);
function mouseReleased() {
translate(-lo/2.0, lo/2.0, 0);
translate(0,-lo/2.0, lo/2.0);
function comb( a1, v1, a2, v2) {
var v3=p5.Vector.mult(v1, a1);
var v4=p5.Vector.mult(v2, a2);
return p5.Vector.add(v3, v4);
function local2Global( cote) {
var qx=new Quat(cos(angleR.x/2.0), sin(angleR.x/2.0), 0, 0);
var qy=new Quat(cos(angleR.y/2.0), 0, sin(angleR.y/2.0), 0);
var qz=new Quat(cos(angleR.z/2.0), 0, 0, sin(angleR.z/2.0));
var q=qx.mul(qy.mul(qz));
function intersection(omegMouse, normRose) {
var oeilOrigin=p5.Vector.sub(laTrans, centreOmega);
var lambda=p5.Vector.dot(oeilOrigin, normRose)/p5.Vector.dot(omegMouse, normRose);
impactRayonMouse=p5.Vector.add(p5.Vector.mult( omegMouse,lambda),centreOmega);
function Mobile(locx,locy,locz){
this.local=createVector(locx,locy,locz);
this.global=p5.Vector.add(this.globalize(this.local),laTrans);
Mobile.prototype.calculG=function(angl){
this.global=p5.Vector.add(this.globalize(this.local),laTrans);
Mobile.prototype.calculL=function(){
var globalQ=p5.Vector.sub(this.global,laTrans);
this.local=localize(globalQ);
Mobile.prototype.display=function(){
translate(this.global.x,this.global.y,this.global.z);
Mobile.prototype.globalize=function( cote) {
var qx=new Quat(cos(angles.x/2.0), sin(angles.x/2.0), 0, 0);
var qy=new Quat(cos(angles.y/2.0), 0, sin(angles.y/2.0), 0);
var qz=new Quat(cos(angles.z/2.0), 0, 0, sin(angles.z/2.0));
var qua=qx.mul(qy.mul(qz));
return qua.tourner(cote);
Mobile.prototype.localize=function( cote) {
var qz=new Quat(cos(-angles.z/2.0), 0, 0, sin(-angles.z/2.0));
var qy=new Quat(cos(-angles.y/2.0), 0, sin(-angles.y/2.0), 0);
var qx=new Quat(cos(-angles.x/2.0), sin(-angles.x/2.0), 0, 0);
var q= qz.mul(qy.mul(qx));
function Quat (w, x, y, z) {
this.w = (w !== undefined) ? w : 1.0;
this.x = (x !== undefined) ? x : 0.0;
this.y = (y !== undefined) ? y : 0.0;
this.z = (z !== undefined) ? z : 0.0;
Quat.prototype.reset = function () {
Quat.prototype.mag = function () {
var m = Math.sqrt(sq( this.x) + sq( this.y)+ sq(this.z) + sq(this.w));
Quat.prototype.setFromAxisAngle = function (axis, angle) {
var s = sin(angle * 0.5);
cos(angle * 0.5), axis.x * s, axis.y * s, axis.z * s);
Quat.prototype.toAxisAngle = function (targetAxis) {
var angle = 2 * Math.acos(this.w);
var s = Math.sqrt(1 - this.w * this.w);
targetAxis.x = this.x / s
targetAxis.y = this.y / s
targetAxis.z = this.z / s
Quat.prototype.mul = function ( q2) {
q2 = q2 || new Quat(1,0,0,0);
res.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
res.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
res.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;
res.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;
Quat.prototype.conjugate = function (targ) {
targ = targ || new Quat();
Quat.prototype.normalize = function () {
var l = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
Quat.prototype.vmult = function (v, target) {
target = target || createVector(0,0,0);
var ix = qw * x + qy * z - qz * y,
iy = qw * y + qz * x - qx * z,
iz = qw * z + qx * y - qy * x,
iw = -qx * x - qy * y - qz * z;
target.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
target.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
target.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
Quat.prototype.copy = function () {
var res=new Quat(this.w,this.x,this.y,this.z);
Quat.prototype.tourner = function(v) {
var qv = new Quat(0.0, v.x, v.y, v.z);
var qc = this.conjugate();
return createVector(q.x, q.y, q.z);