Heyo hopyos, hope you are having a great day!
Have you ever wanted to make a plataformer but struggled with coding the collisions? Or, let’s say, wanted to code a pool game but never knew how to make the balls collide and react?
Well! You’ve come to the right place! Here I will teach you the basics about smooth collisions, the uses, and the different types!
Square collisions
Ok, so you wanna do square collisions
Let’s say your character moves because of the vertical and horizontal velocities, but the character is fixed at the middle of the screen, making background objects being the actual stuff that moves
We would make a rule that sets where is the player X or Y according to our map, and we would subtract that values to the map’s objects, like this
In this example, the variable Scroll X determines where my character is, and the variable (self) X defines the blocks position in relation to the map
So, now that we know those values, let’s use them to create a collision with the character!
We can make either one only rule for collisions, or 2 rules, or 4 rules
In this case we will do 2
So what we will do first, is to check if the player is colliding near any side of the block, the code would look like this:
When Absolute value (charafter) X position - (self) X position < (self) Width/2 + (character) Width/2
NOTE: This rule goes on the blocks’ sprite, not on the character one
This rule measures how far a block is from the character, and if the character is inside a certain area surrounding the square, the function will trigger
To make the character stop there, just put this code inside the rule:
Check once if (character) Y position - (self) Y position < (self) Height/2 + (character) Height/2
Check once if (character) X position > (self) X position
Set (character) horizontal velocity to 0
Set Scroll X to (self) X*(self) width - (self) width/-2
Else
Set (character) horizontal velocity to 0
Set Scroll X to (self) X*(self) width - (self) width/2
now we have done the side collisions! This will make the character stop when it is touching the side of the object, this same rule can be adapted to when the character is on a block, we will see the adaptations right now
The following code goes inside this rule:
When Absolute value (charafter) Y position - (self) Y position < (self) Height/2 + (character) Height/2
Check once if (character) X position - (self) X position < (self) Width/2 + (character) Width/2
Check once if (character) Y position > (self) Y position
Set (character) Vertical velocity to 0
Set (Character) Y to (self) Y + (self)Height/2
Else
Set (character) vertical velocity to 0
Set (Character) Y to (self) Y + (self)Height/-2
It’s basically the same; we just change the x values for y values and vice versa
So now that we have done our square collisions
We can advance to circular ones, even though they are hard to use on a platformer context, I’ll teach you how to get them and how they work!
Circular collisions
This collisions need a bit less of code, since they can be resumed in just one rule, but it’s hard to make it so a character bumps into them, so why not use them instead, for making a score?
The rule is pretty simple, I’ll leave it here:
when absolute value √([{character} X position - {self} X position]^2 + [{character} Y position - {self} Y position]^2<(self) Width/2
This rule works the following way, it calculates the distance of an object to another and checks if that object is within a certain radius
(This collisions can be observed in my summer team’s game, Beach Mayhem, at the treasure hunt place)
Anyways, this is all you need to know to code collisions! This concept was inspired by @Nobody
Hope it’s helpful! @ omtl
