Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

67
Vistas
Having issues making a collision in Pong - JS

So, I know there's been a lot of questions on this in the past, but yet to find a fix that'd help on mine. As for the topic, it gives it all. I can't make it detect a collission, and I have a theory as to why, but no fix I've attempted has seemed to work.

I'm a super rookie in 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;
        }
        
    }
}

This is supposed to be where it detects the collission and angle. It's a part of a group project, and my groupmate has no clue either. This is how the paddle is designed:

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;}
  }
 }
}

As you see, in this "version" I'm trying to check for a direct collission with a second instance of the "player" - since there'll be 2. There's just no collission. Everything else bounces as it should.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda