Entonces, sé que ha habido muchas preguntas sobre esto en el pasado, pero aún no he encontrado una solución que me ayude. En cuanto al tema, lo da todo. No puedo hacer que detecte una colisión, y tengo una teoría de por qué, pero ninguna de las soluciones que he intentado parece funcionar.
Soy un súper novato en JS.
checkPlayer2(Player2) { //checker kollision if (this.y - this.r < Player2.y + Player2.h/2 && this.y + this.r > Player2.y - Player2.h/2 && this.x - this.r < Player2.x + Player2.w/2) { print("hit") //checker vinkel if (this.x > Player2.x) { let forskel = this.y - (Player2.y - Player2.h/2); let rad = radians(45); let angle = map(forskel, 0, Player2.h, -rad, rad); this.xspeed = 5 * cos(angle); this.yspeed = 5 * sin(angle); this.x = Player2.x + Player2.w/2 + this.r; } } }Se supone que aquí es donde detecta la colisión y el ángulo. Es parte de un proyecto grupal, y mi compañero de grupo tampoco tiene idea. Así está diseñado el pádel:
class player{ constructor(U,D,nr) { rectMode(CENTER); this.x=width*(nr-1); if(nr>1){ this.x=this.x-35 }else{ this.x=this.x+35 } this.y=height/2 this.ctrlU=U this.ctrlD=D this.h = 100; this.w = 20; } show(){ rect(this.x, this.y, this.w, this.h); } update(){ if (keyIsDown(this.ctrlU)){ if(this.y>=51){this.y=this.y-10;} } if (keyIsDown(this.ctrlD)) { if(this.y<=height-51){this.y=this.y+10;} } } }Como puede ver, en esta "versión" estoy tratando de verificar si hay una colisión directa con una segunda instancia del "jugador", ya que habrá 2. Simplemente no hay colisión. Todo lo demás rebota como debería.