The ULTIMATE guide to smooth collisions

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

21 Likes

No I clicked on accident

5 Likes

yes

want me to delete then you can delete the topic?

Sigh I’m.p not done yet…

1 Like

Ok anyways I’m pretty much done with side collisions

1 Like

relatable

2 Likes

Very useful for me.
My platformer is wonky lo
Finish it

2 Likes

wow my thinking was close lol, I wasn’t doing absolute value and that messed it up

Can a leader please edit the op and change the last thing to this?

2 Likes

Or type the rest and tell a lead to edit it in
Sigh too late

1 Like

Yeah but I will do that separately

That post above was an edit, I’ll do the rest now

3 Likes

I’ve always had trouble with collisions. This has helped me a lot.

3 Likes

Thanks! @TheCMStudios

2 Likes

Hey @TheCMStudios, I’m done with the topic, could you please edit the op? Just delete everything and paste this

Thanks in advance m8!

2 Likes

Oof the omtl tag in the OP didn’t work so @omtl check the op!

This might be a helpful topic!

6 Likes

I completed the tag and did an edit to cancel it in case future edits are made

4 Likes

Oooh, smooth! Thanks for sharing this!

3 Likes

Love the tutorial!

1 Like

I thought I have this exact thing in Tip of the Week, but nice job regardless!

3 Likes

oo nice topic!