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

138
Vistas
how to bounce a rectangle off another rectangle and scale it with speed

I am making a game and in need collision on objects that will bounce it away so of I hit the wall I bounce off
This is my code

if (xpad < rect2.x + rect2.w &&
        xpad + rect1.w > rect2.x &&
        ypad < rect2.y + rect2.h &&
        rect1.h + ypad > rect2.y) {
      if(ypad+ 40 == rect2.y)
           {
            vert=-vert;  //top of the cube
           }
      if(ypad == rect2.y + rect2.h)
           {
              vert=-vert;  //bottom of cube 
           }
      if(xpad + 40 == rect2.x)    //40 is the width of my player and i didn't use rect1.h
           {
               horz=-horz;     //left of the cube
           }  
 if(rect2.x + rect2.w == xpad)
           {
              horz=-horz;     //right of the cube 
           }
           }

Also the loop I use to call my functions and other things is already running without a delay so I can't decrease the delay.
The two vars I'm using for the collision are

var rect1={
    x:xpad,
    y:ypad,
    w:40,
    h:40
};
var rect2={
    x: 150,
    y: 200,
    w:100,
    h:100
};

the xpad/ypad increases itself which pushes the cube or the player

typical xpad and ypad = 1, this works with my collision but this issue starts when I increase the speed or xpad / ypad to 2, then it starts going through walls and objects so how can I fix this or change my code to allow different speeds form 1 to 3

LINK TO SEE/TEST CODE:
https://javascriptss.glitch.me/testing.html for testing
https://javascriptss.glitch.me/scriptcol.js for seeing

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