Designing placement of brick/ball object collisions

Hey all,

I'm a developer but haven't done much with game development and GameMaker Studio 2 for much longer than a few weeks. I'm recreating an old game that is a spin on the class ball-breaks-bricks game. It's more complicated then that but that's the idea.

I have about 14 different bricks that all have different sprites and different behaviour when the ball collides with them. I've designed a ball and 14 different objects, and need to create collision events between the ball and each of the bricks. I'm trying to avoid copying-and-pasting the logic (it works just fine) that will calculate the ball's rebounding angle/speed into each of the collision events. Any ideas or better way to design it?

My very first attempt was to put all the collision events between the ball and the different bricks, on the ball itself, but then (as far as I know) I couldn't put that rebound logic in a parent brick object which was another idea. I feel like I'm missing something.
 

Yal

🐧 *penguin noises*
GMC Elder
Make a "parent_brick" object and make it the parent of all the brick objects, then give the ball code for colliding with the parent_brick only; its child objects will be treated as special cases of the parent.
 

Tyg

Member
there is point_in_circle and point_in_rectangle funcs that work quite well
I made something similar a pool game that used gamemakers physics, worked quite well
the balls were all parented but certain things were overridden. like the cue ball or 8 ball
the cushions were parented off a solid object which was semi-transparent so i could see and shape them in the editor
and then visibility off in the game..the physics system has gravity and friction cant remember but the rebound was pretty easy too
was quite amazed ..and it was quite fun..
pool.jpg
 
Make a "parent_brick" object and make it the parent of all the brick objects, then give the ball code for colliding with the parent_brick only; its child objects will be treated as special cases of the parent.
Thanks for the quick response.

That was my first thought and attempt. I had one collision event on the ball when it collided with the parent brick (that had no sprite assigned to it). For some reason though it wasn't getting triggered. Are there any particular settings that need to be set to collide with an object without a sprite? I feel like it should be associated to the children objects.

The other part of that was hoping, if I did that one event on the parent object, that I could then run code specific to a given child brick without putting in a big 'switch' statement that runs code depending on the sprite of the object. It's a bit backwards for inheritance since the event is on the ball, but is there a way to run the child brick collision event that is housed on the ball, and within that call the parent brick collision event that is housed on the ball as well? It's the same idea as calling event_inherited if I understand that correctly, and is cleaner than a giant 'switch' or 'if/elseif' statement on the parent brick collision event.
 
there is point_in_circle and point_in_rectangle funcs that work quite well
I made something similar a pool game that used gamemakers physics, worked quite well
the balls were all parented but certain things were overridden. like the cue ball or 8 ball
the cushions were parented off a solid object which was semi-transparent so i could see and shape them in the editor
and then visibility off in the game..the physics system has gravity and friction cant remember but the rebound was pretty easy too
was quite amazed ..and it was quite fun..
View attachment 37955
Thanks for the quick response.

Did you run those point_in_* functions in a collision event or make your own collision event by putting them in a step event?
 

Yal

🐧 *penguin noises*
GMC Elder
I had one collision event on the ball when it collided with the parent brick (that had no sprite assigned to it). For some reason though it wasn't getting triggered. Are there any particular settings that need to be set to collide with an object without a sprite? I feel like it should be associated to the children objects.
Yes, that's what "make it the parent of all the brick objects" bit meant :p
There's a special "parent" window you can access by clicking 1, once you open it up, drag all the child objects into the 2 area.
1613150006452.png

By default the collision mask is the same as the sprite; having no sprite means there's no hitbox. You can set the variable mask_index to a sprite if you want the object to only have a collision mask but no sprite. (In my experience, the opposite is more common: wanting no hitbox while having a sprite, or at least using a different sprite for collision purposes)
 
Thanks for the re
Yes, that's what "make it the parent of all the brick objects" bit meant :p
There's a special "parent" window you can access by clicking 1, once you open it up, drag all the child objects into the 2 area.
View attachment 37957

By default the collision mask is the same as the sprite; having no sprite means there's no hitbox. You can set the variable mask_index to a sprite if you want the object to only have a collision mask but no sprite. (In my experience, the opposite is more common: wanting no hitbox while having a sprite, or at least using a different sprite for collision purposes)
Thanks for the response. I think I wasn't clear.

I have already set up the 14 brick objects to be children of the parent brick (obj_BounceableBrick in the image below).

Screenshot 2021-02-12 at 7.23.40 PM.png


Then I have a collision event on the obj_Ball with the obj_BouncableBrick. This collision event houses all the logic to rebound the brick based on it's current angle. This collision event is successfully triggered every time the ball collides with any of the 14 child bricks and the ball rebounds correctly. What I'm now trying to accomplish is adding additional logic to the collision event for each of the child bricks. (eg: when the ball hits a brown brick it rebounds off with the logic of the parent brick, and then plays a sound unique to the brown brick.

One solution would be to add code in this collision event between the ball and the obj_BouncableBrick parent class and just do a "switch" statement or a bunch of "if" statements that check for the sprite of the object the ball is colliding with and play the correct sound, but that seems like a less than elegant way to do this considering inheritance is often used to solve problems like this.

Screenshot 2021-02-12 at 7.25.28 PM.png

Does my question make more sense? I feel like there should be a way to use inheritance to extend the obj_BounceableBrick collision event and add more logic on each brick inheriting from obj_BounceableBrick.
 

Tyg

Member
If you check the box that says solid and uses physics and have physics enabled in the room
i have a parent solid object a ball, all the other balls inheirit the solid and physic features of the parent
ill show you some pics...some of the gamemaker physics functions dont work if physics is not enabled
thats where so many go wrong they try to reinvent the wheel when gamemaker has a lot of built in features
why should i trust their code over gamemakers proven code?
I have to laugh at people (won't mention names) that still are writing code for WASD movement
and have the audacity to call people Backwards thinkers and wonder why are they even using gamemaker?
sorry need to vent ..lol
gamemaker has robust physics, collision, event, sprite, shader and other systems
and it makes me wonder why they dont't use it or don't know how to
ill use my pool game as an example

Pool1.jpgPool2.png
I would be happy to explain any of the objects in more detail if anyone needs
Here is an entire pool game with very little coding :)
 
Last edited:

Yal

🐧 *penguin noises*
GMC Elder
What I'm now trying to accomplish is adding additional logic to the collision event for each of the child bricks. (eg: when the ball hits a brown brick it rebounds off with the logic of the parent brick, and then plays a sound unique to the brown brick.
Using the "variables" interface, give the parent brick a "my_bouncesound" variable which has type Asset (and then use the gear icon to set it to only accept Sound assets), set it to a good default bounce sound. Now go into all the child objects and you should see they have this variable grayed out; click the "override" button and change the sound for each brick to its unique sound.

In the bounce event, run audio_sound_play with the brick's my_bouncesound as the sound argument; it should now play the assigned sound of each brick (and if a brick doesn't have an override value, it uses the parent's value automatically)
 
Top