bothering, what happens is that I have a molecule and I already have the collision with the canvas so that these molecules do not come out and bounce, the problem I have is that the collision is generated in my red atom (red circle) and not on target, I would appreciate your help on this occasion
JS
class AtomoClass{
constructor(posx,posy,color,radio){
this.posx=posx;
this.posy=posy
this.color=color;
this.radio=radio;
this.inicia=0;
this.termina=2*Math.PI;
this.angle = Math.random()*(2*Math.PI);
this.movimientox = Math.cos(this.angle);
this.movimientoy = Math.sin(this.angle);
this.enlace = null;
}
}
var atomos=[];
var ballRadius = 30;
var otroballRadius=20;
var canvasRecipiente = document.getElementById("recipiente");
var ctx = canvasRecipiente.getContext("2d");
for (var i= 0; i < 20; i++) {
var atomoR = new AtomoClass(ballRadius+(Math.random()*(recipiente.width-(2*ballRadius))),(ballRadius+(Math.random()*(recipiente.height-(2*ballRadius)))),'red',ballRadius);
atomos.push(atomoR);
var atomoB = new AtomoClass(otroballRadius+(Math.random()*(recipiente.width-(2*otroballRadius))),(otroballRadius+(Math.random()*(recipiente.height-(2*otroballRadius)))),'white',otroballRadius);
atomoB.enlace = atomoR;
atomos.push(atomoB);
}
function atomoPinta(){
for (var j = 0; j < atomos.length; j++){
var defaultX = atomos[j].posx;
var defaultY = atomos[j].posy;
if(atomos[j].enlace != null){
defaultX = atomos[j].enlace.posx+15;
defaultY = atomos[j].enlace.posy+25;
}
ctx.beginPath();
ctx.arc(defaultX,defaultY,atomos[j].radio,atomos[j].inicia,atomos[j].termina);
ctx.fillStyle = atomos[j].color;
ctx.stroke();
ctx.fill();
ctx.closePath();
}
}
function draw(){
ctx.clearRect(0, 0, recipiente.width, recipiente.height);
atomoPinta();
for (var b = 0; b < atomos.length; b++) {
if(atomos[b].posx + atomos[b].movimientox > recipiente.width-atomos[b].radio || atomos[b].posx + atomos[b].movimientox < atomos[b].radio) {
atomos[b].movimientox = -atomos[b].movimientox;
}
if(atomos[b].posy + atomos[b].movimientoy > recipiente.height-atomos[b].radio || atomos[b].posy + atomos[b].movimientoy < atomos[b].radio) {
atomos[b].movimientoy = -atomos[b].movimientoy;
}
atomos[b].posx += atomos[b].movimientox;
atomos[b].posy += atomos[b].movimientoy;
}
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);