Ball freezes when being bounced

I’m trying to make atari breakout and all is going well, however, my ball keeps freezing (for about 0.5-2 seconds) whenever it collides with a brick. Below is a screenshot with the code related to the ball bouncing. I was unable to find the direct cause of the long pauses by removing bits of code. I use custom drawn shapes instead of objects in order to scale up/down and more control over the bricks.

I’m using an iPhone 8 for testing my code using the companion mode.

Left function is to draw the bricks by getting the data from the blocksData dictionary
Right function runs every 10ms to detect if the ball has collided with a block. It then removes that from the dictionary and draws the bricks again.
Other code is insignificant and couldn’t be triggered when this problem occurs.

What kind of component is Brickssfx?

A Sound, or a Player?

The Sound component has been known to block the cpu while playing.

Also, might the Ball be hitting the side of another brick after bouncing off the bottom of a brick?

That changes the bounce angle calculation.

See

for bounce angle experiments and references.

1 Like

I was actually looking for a better bounce equation, so thanks in advance.

The sfx are currently Sound components so I’m afraid they do block the cpu. I am unable to test thoroughly today, so I’ll get deeper into that tomorrow but from my initial testing with Players this does seem to speed things up a lot!

Unfortunately it still freezes for about 400ms or so. Could there be any other reason for this?

You might be able to rule out side collisions by adding a cool down period after collisions, but that would need tuning to account for bouncing in a cavity.

You might also get a speed up from undrawing individual bricks instead of redrawing all of those left in the dictionary.

You might get another speedup by translating ball coordinates into block key for the current ball location and just testing the dictionary for that key in that clock timer cycle. The key calculation would be some modulus calculation, plus or minus x and y offsets.

1 Like

After optimising my canvas and sounds I’ve concluded that the collision checking is indeed the culprit. I’ve tried to optimise it a bit by only checking the bricks in the ball’s column but I wasn’t able to get a better performance out of that. What would you recommend I do? I see you’ve already said this previous post but I don’t understand what you mean by that exactly.

You might get another speedup by translating ball coordinates into block key for the current ball location and just testing the dictionary for that key in that clock timer cycle. The key calculation would be some modulus calculation, plus or minus x and y offsets.

Also, I wasn’t able to implement the improved bounce angle because I was unable to find the ‘heading’/side the ball was on of the brick. Do you have any suggestions for this? Below is a screenshot of my current collision checking (first if statement is my lousy attempt to optimise the code, second statement is for the actual collision detection)

UPDATE

After doing some optimisation and increasing the timer interval the game now runs relatively smooth. So performance is fine now. I’m just curious to see if someone has a solution for finding which side of the brick the ball is. Big thanks to ABG..

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.