Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!