Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

55
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

7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.