Understanding this collision snippet (paddle/ball) c#

Hello guys. I am trying to break down the code below (this is similar to breakout etc, where a ball hits the paddle to the left or right of it and gets sort of deflected at a greater angle).

void OnCollisionEnter ( Collision col ) {
		foreach (ContactPoint contact in col.contacts ) {
			if (contact.thisCollider == collider ) {
				//this is the paddle's contact point
				float english = contact.point.x - transform.position.x;
				contact.otherCollider.rigidbody.AddForce ( 300f * english, 0, 0);
			}
	}
}
}

So OnCollisionEnter basically initiates when there is a collision between two rigidbodies (in this case the paddle and the ball - this script is applied to the paddle).

( Collision col ) According to the script reference page Unity - Scripting API: MonoBehaviour.OnCollisionEnter(Collision) it says we are passing the class “Collision” and then I guess we are giving it the name col? Can anyone expand on that part and whats going on? Not too familiar with classes in C# (I will read up after promise :P). Does the col part refer to this specific collision?

foreach (ContactPoint contact in col.contacts ) {

I suppose here we use a foreach loop as there may be multiple points of contact? Now I am starting to get confused by the number of “contacts” here… we have ContactPoint contact and contacts :open_mouth: But what do they all refer to?

if (contact.thisCollider == collider ) {

All I know here is that thisCollider refers to the paddle, and it needs to be equal to the “collider” (terms getting a bit vague…).

float english = contact.point.x - transform.position.x;

assigning a floating number variable called english that is result of the calculation “contact.point.x - transform.position.x;” I suppose contact.point.x is the x co-ordinate of the contact point of the collision. But what do we mean here by transform.position.x? I checked this Unity - Scripting API: Collision.transform, is it just “opening up transform” to check the x co-ordinate of the paddle rather than actually transforming something?

contact.otherCollider.rigidbody.AddForce ( 300f * english, 0, 0);

I guess from my previous programming experience we are looking through a bunch of data stuff and then adding more force to the ball (depending on english calculation) but how does more force affect the angle if the ball if its just falling straight down? Or is that stuff taken care of by the physics engine?

My guess is that this script assumes that the ball and paddle are set up in a way so that the actual bounce/reflection of the ball off of the paddle is getting automatically handled – that isn’t part of this script. It seems to just be applying a force to the ball (to the left or right) based on how offset from the paddle the ball was when it collided.

Most of your assessment seemed correct. From a quick read through:

foreach (ContactPoint contact in col.contacts ) {

The array of ContactPoint structs in col.contacts will contain 1 or more points where things collided. We are going through that array.

if (contact.thisCollider == collider ) {

We are checking to see if the first collider in the struct (of the two colliders that collided) is the collider that is attached to this GameObject (collider is just a shorthand for checking the collider component on this GO) – one thing that I would check is whether or not this is ever false. Intuitively, it seems like it would always be true, but I don’t know for sure.

 float english = contact.point.x - transform.position.x;

We end up with a float that if added back to transform.position.x (“transform” is a fast reference to the GO’s Transform component and we are looking at the x value of Vector3 position) would give us contact.point.x (the x value of the Vector3 of where the collision occurred).

contact.otherCollider.rigidbody.AddForce ( 300f * english, 0, 0);

We have the physics system add a force (a Vector3) to the other object in the collision (the ball?) – the force is that offset value (english) multiplied by 300f (probably just a reasonably big number arrived at given the scale of the game that this was written for and the physics properties of the ball.).

So – just applying a force to the ball (to the left or right) based on how offset from the paddle the ball was when it collided. Assuming the ball is already bouncing off the paddle (probably through default physics simulation, not a script), this is just to give it the illusion of having some spin.

Thanks, great descriptions! I’ll go through them carefully.