Can someone explain this code to me?

so i have this onCollision code and it includes object:applyForce code… 

CODE WAS REMOVED

So when the circle collides with the box ; it pushes the box …

But im not 100% sure on how it works and what it means…

CODE WAS REMOVED

This code above is the one i need help understanding…

ill tell you what i do know…

CODE WAS REMOVED

in the code above the event.other.x and the event.other.y prevents the box from spinning freely…

What i dont know is why in this code below

CODE WAS REMOVED

why is the event.other subtracted from self.x and self.y…

Thanks for anyone who helps!

Hi @SonicX278,

Let’s just consider the x aspect as an example. Imagine if, when the objects collide, the circle (“self”) is at x=200, and the box (“event.other”) is at x=220. Doing the math in that example, it works out to 220-200=20. So, the force of 20 is applied to the center of the box, which should correctly send the box moving to the right.

Consider other examples as well, using similar values:

  1. Circle is to the right of the box on collision: 200-220=-20, sending the box moving to the left.

Or for the y aspects:

  1. Circle is above the box on collision: 220-200=20, sending the box moving downward.

  2. Circle is below the box on collision: 200-220=-20, sending the box moving upward.

Hope this helps,

Brent

Ahhhh i see Thanks a ton @Brent

Why did you remove the code?  now this post is not of much use to anyone.

The code wasn’t going to be of much use to anyone anyways… So I spared y’all the trouble and removed it. I mean if you want it I’ll post it when I have time… I’m on vacation… And usually replying to posts on my phone…

In the future, please leave the previous posts in tact. People searching for threads will find this and be confused.

Rob

I guess that’s true, I’ll repost the code when I have time!!

Hi @SonicX278,

Let’s just consider the x aspect as an example. Imagine if, when the objects collide, the circle (“self”) is at x=200, and the box (“event.other”) is at x=220. Doing the math in that example, it works out to 220-200=20. So, the force of 20 is applied to the center of the box, which should correctly send the box moving to the right.

Consider other examples as well, using similar values:

  1. Circle is to the right of the box on collision: 200-220=-20, sending the box moving to the left.

Or for the y aspects:

  1. Circle is above the box on collision: 220-200=20, sending the box moving downward.

  2. Circle is below the box on collision: 200-220=-20, sending the box moving upward.

Hope this helps,

Brent

Ahhhh i see Thanks a ton @Brent

Why did you remove the code?  now this post is not of much use to anyone.

The code wasn’t going to be of much use to anyone anyways… So I spared y’all the trouble and removed it. I mean if you want it I’ll post it when I have time… I’m on vacation… And usually replying to posts on my phone…

In the future, please leave the previous posts in tact. People searching for threads will find this and be confused.

Rob

I guess that’s true, I’ll repost the code when I have time!!