Problems in detecting object collisions

2

Recently I started to develop a game that consists of throwing a ball and getting it to take place (in this case in a rectangle), which has been a parabolic shot in full force.

  

In the game there are different objects:

     

· The basket (a simple rectangle)

     

· Objects that act as obstacles (more rectangles).

     

· The ball

     

· And the walls (the game area)

Well, the mess I have is that sometimes the shock with the object is not detected and this happens only when the ball goes very fast and this happens because between each frame the object advances more pixels (at a time) than when the speed is lower, I mean:

I have an object that contains the levels:

var selLevel = 1;

    var level = {

        1: {
            basketPos: [600,500], //Basket pos
            basketSize: [200], //The width of the basket
            ballRad: 20, //ballRad
            ay: 25, //Aceleration y
            ax: 0, //Aceleration x
            bR: 0.2, //Bounce reduction
            objects: { //External objects (obstacles)
                1: [200,280,50,1000],
                2: [450,200,400,25]
            }
        }
    };

This is the loop:

function animation() {

    //Se que hay variables y otras cosas que no aparecen en la función, pero para esto no son necesarias

    //Current speed
    vx = v0x+(ax*t);
    vy = -v0y+(ay*t);

    //Current position
    x=x0+(v0x*t)+(0.5*ax*Math.pow(t,2));
    y=y0+(-v0y*t)+(0.5*ay*Math.pow(t,2));

  //Este es el sistema que estoy usando para detectar los objetos
  for (var i = 1; i <= otherObjects.length; i++) {

      oX = level[selLevel]["objects"][i][0]; //Posición del obstáculo en el eje x
      oY = level[selLevel]["objects"][i][1]; //Posición del obstáculo en el eje y
      oW = level[selLevel]["objects"][i][2]; //Ancho del objeto
      oH = level[selLevel]["objects"][i][3]; //Alto del objeto

      //Adaptar posiciones y tamaños a la pantalla actual
      oX = (oX * windowWidth) / scaleSize[0];
      oY = (oY * windowHeight) / scaleSize[1];

      oW = (oW * windowWidth) / scaleSize[0];
      oH = (oH * windowHeight) / scaleSize[1];

      if (oW <= ballRad * 2) {
          oW = ballRad * 2;
      }

      if (oH <= ballRad * 2) {
          oH = ballRad * 2;
      }

      //Left object bounce
      if (x + ballRad >= oX &&
          x + ballRad <= oX + oW &&
          y >= oY &&
          y <= oY + oH &&
          vx > 0) {
          //Aquí está llamando a una función que se encarga de cambiar los valores para simular el rebote
          bounceLeft(fix);
          break;
      }

      //Right object bounce
      if (x - ballRad <= oX + oW &&
          x - ballRad >= (oX + oW) - oW &&
          y >= oY &&
          y <= oY + oH &&
          vx < 0) {
          bounceRight(fix);
          break;
      }

      //Top object bounce
      if (y + ballRad >= oY &&
          y + ballRad <= oY + oH &&
          x >= oX &&
          x <= oX + oW &&
          vy > 0) {
          bounceUp(fix);
          break;
      }

      //Bottom object bounce
      if (y - ballRad <= oY + oH &&
          y - ballRad >= oY &&
          x >= oX &&
          x <= oX + oW &&
          vy < 0) {
          bounceDown(fix);
          break;
      }
  } 

  //Aquí hay una adición del tiempo
  t=t+0.16; //Aquí lo he ocultado pero en el programa de verdad calculo el tiempo que tarda en hacer el loop y luego sumo el tiempo respecto a ese valor
  window.requestAnimationFrame(animation);
  }
  

I'm simplifying the scripts a lot to be as thin as possible

Well, that said, let's imagine that there is an object in the position (200,300) and its size is (100,50) respectively, when the speed of the ball is very high and the checks are applied there is a difference of pixels between a frame and another one higher than the height of the object , with which it is impossible for the conditions to be met, then it occurred to me to calculate the operations in a separate interval, and the actions to be painted they would execute using requestAnimationFrame() in another function, and in this way I managed to make work but the game started to have a lot of lag (so to speak) and the animation was jumping and stumbling. Then of course, I do not know what to do in the end, if someone could help me I would be very grateful, and I would also like to know if I am using a more or less correct method for the detection of these objects.

Thank you.

    
asked by lromeraj 06.04.2017 в 20:33
source

1 answer

0

The problem is that you do your process as many times as your team could do it, so you spend all the resources on it.

If you perform the calculation in a different thread, you can pause it for a certain time (milliseconds) to save resources.

It's a matter of you calibrate the pause time to achieve the balance between accuracy and performance.

    
answered by 06.04.2017 в 21:19