Getting exact sprite another sprite collided with?

I want to be able to have upwards of 50 sprites for an Atari Breakout game. the problem is, having a for loop with the 50+ sprites is very taxing. Is there a function that returns the exact sprite another collided with, that isn’t resource-intensive? Thanks!

P.S. what is the OPENGL constant in game lab for? Is there a plan to implement OpenGL? Is it already implemented?

If you mean collision of a sprite with a group, and you want it to return the sprite(s) that it collided with, there’s no hope there. I believe there are some ways you could do this without iterating with every sprite. One method perhaps is to check the region the center point of the sprite is on, and collide with the box from there. I used this method (kind of) for an achievements system that had 15 achievements on the same page, you take the floor function of the quotient of the X value and the size of blocks, and you get the x-index. You do the same with the Y value, and you get the y-index. Then index the sprite with that position.
For example if there were 5 blocks per row:

Math.floor(World.mouseX/(400/5)) //returns a value between 0~4

Here’s a game with 100 tiles, twice more than 50. It uses essentially the same system I described earlier. It may seem slow but the frame rate is 30 like default.
While you may have to sacrifice a circular collider (I don’t know how to get that working), this is pretty efficient and each tile has to move up when you hover over it, do an elastic animation when clicked. For a Atari Breakout game the “blocks” don’t necessarily have to play an animation which makes the system here pretty reliable.

As for ‘OPENGL’, I don’t think that exists in game lab.
Perhaps you meant WEBGL, in that case WEBGL is used in createCanvas. doc

Hi @kookooloopdaking,

Is this for a student’s end-of-chapter project? GameLab is designed as a coding environment for CS Discoveries, so it’s not as robust as other game engines.

–Michael K.

So essentially, I could replace World.mouseX with ball.x to use the position of the ball? That’s very interesting. Thank you!

Okay, i kind of used your idea, i had multiple groups and checking collision for said groups.

i have something like this:

while (ball.isTouching(blocks)) { //detecting collision between ball and blocks
for (var i = 0; i < 8; i++) { //panning through coloumns searching for first one ball is touching
    if (ball.isTouching(blocksX[i])) {
      x = i;
  for (var i = 0; i < 6; i++) { //panning through the rows
    if (ball.isTouching(blocksX[x].get(i))) {
      y = i;
  //I have some more code dealing with the output of the collision here.

if you want to see the game I used it with, please note, I’ve only spent around 2 days on it.

1 Like

Awesome game! @kookooloopdaking!