# Always Get Same Physics Collision Rebound Force

Discussion in 'Physics' started by siddharth3322, Jan 20, 2019.

1. ### siddharth3322

Joined:
Nov 29, 2013
Posts:
689
I was working on a 3d car war game, and I was moving my cars through velocity using the following code:
Code (CSharp):
1. appliedSpeed += Time.fixedDeltaTime * 8f;
2.         appliedSpeed = Mathf.Min (appliedSpeed, speed);
3.         myRigidbody.velocity = transform.forward * appliedSpeed;
At collision time, I was giving backward force using the following code:
Code (CSharp):
1. private void ApplyReboundForce (Collision other)
2.     {
3.         Vector3 reboundDirection = other.contacts [0].point - transform.position;
4.         reboundDirection = -reboundDirection.normalized;
5. //        myRigidbody.AddForce (reboundDirection * 7f, ForceMode.Impulse);
6.         myRigidbody.AddForce (reboundDirection * 180f, ForceMode.Force);
7.     }
But at present, I was getting a collision response force dynamically. Sometimes it was a higher force and sometimes it was lower force based on physics calculation.
I want collision response force always same.

Here is the reference game for collision response force:

I have already made all physics property of all cars same like physics material, rigidbody properties values etc... then also getting different collision response on each collision.
Please give me some suggestion to achieve something similar.

2. ### Magnas_94

Joined:
Jan 20, 2019
Posts:
5
Hi @siddharth3322 ,

I just saw the video and your script , the force can be different in case of each collision, because you are using collision by points, thus the value of variable can "reboundDirection" can be different in case of objects with different sizes and positions.

I made the following changes to your code , see if it best suits you :

Code (CSharp):
1. using UnityEngine;
2.
3. public class speed : MonoBehaviour {
4.
5.     public Rigidbody thisRB;
6.     float appliedSpeed = 0.0f;
7.     public float theSpeed = 1.0f;
8.
9.     public float impactForce = 15.0f;
10.
11.
12.     // Update is called once per frame
13.     void FixedUpdate () {
14.         appliedSpeed += Time.deltaTime * 8.0f;
15.         appliedSpeed = Mathf.Min(appliedSpeed, theSpeed);
16.         thisRB.velocity = (transform.forward * appliedSpeed);
17.     }
18.
19.     private void OnCollisionEnter(Collision collision)
20.     {
21.         Vector3 reboundDirection = Vector3.Normalize(transform.position - collision.transform.position);
22.         reboundDirection.y = 0.0f;
23.
24.         Debug.Log("Rebound Force is " + reboundDirection);
26.         appliedSpeed = 0.0f;
27.     }
28. }
I would suggest multiplying the impact force by the mass of the colliding rigid body, if you are increasing mass of the object , as per its size , as seen in the video.

Also freeze unnecessary rotations and position in Y - axis.

Good Luck !

siddharth3322 likes this.
3. ### siddharth3322

Joined:
Nov 29, 2013
Posts:
689
@Magnas_94 thank you for your reply and I carefully applied whichever points you have suggested.
Let me share overall experience with you.

Code (CSharp):
1.  // Update is called once per frame
2.     void FixedUpdate () {
3.         appliedSpeed += Time.deltaTime * 8.0f;
4.         appliedSpeed = Mathf.Min(appliedSpeed, theSpeed);
5.         thisRB.velocity = (transform.forward * appliedSpeed);
6.     }
This code, I can't able to put directly in FixedUpdate method otherwise it directly started controlling car movement.
So car background force gets stopped directly. I have modified the code like this way:
Code (CSharp):
1.  // Update is called once per frame
2.     void FixedUpdate()
3.     {
4.         //        if (brakePlayerCar && myRigidbody.velocity.magnitude > 3f) {
5.         //            return;
6.         //        } else
7.         //            brakePlayerCar = false;
8.
9.         elapsedEngineStartWaiting += Time.deltaTime;
10.         if (elapsedEngineStartWaiting < 0.7f)
11.             return;
12.
13.         // Move the player forward
14.         appliedSpeed += Time.deltaTime * 10f;
15.         appliedSpeed = Mathf.Min(appliedSpeed, speed);
16.         myRigidbody.velocity = transform.forward * appliedSpeed;
17. }
Overall added waiting time to start car engine again.

Here you have the code that applied on collision with other cars:
Code (CSharp):
1.     void OnCollisionEnter(Collision other)
2.     {
3.         if (other.gameObject.CompareTag(GameConstants.TAG_ENEMY))
4.         {
5.             //appliedSpeed = speed * 0.5f;
6.             elapsedEngineStartWaiting = 0f;
7.             ApplyReboundForce(other);
8.         }
9.     }
10.
11.     private void ApplyReboundForce(Collision other)
12.     {
13.         Vector3 reboundDirection = Vector3.Normalize(transform.position - other.transform.position);
14.         reboundDirection.y = 0f;
15.
16.         //Vector3 reboundDirection = other.contacts [0].point - transform.position;
17.         //reboundDirection = -reboundDirection.normalized;
20.     }
21.
Here is the overall car configuration:

Still, I can't able to get the same collision response always, it continuously dynamic.
Whether I require to control rebound force manually?
Please give me some more suggestions into this so I can get above video kind of output.

Magnas_94 likes this.
4. ### Magnas_94

Joined:
Jan 20, 2019
Posts:
5
Hi @siddharth3322 ,

I got the exact setup you needed for your game , our code had 1 major issue :

We were adding to the velocity to move the car forward, but we were also adding a force to push our cars back , that was causing that jittery throwback of the car , I made some changes to the way our car catches speed, also I changed the way forces are added , so that it always remains constant.

Code (CSharp):
1. using UnityEngine;
2.
3. public class speed : MonoBehaviour {
4.
5.     public Rigidbody thisRB;
6.     float appliedSpeed = 0.0f;
7.     public float theSpeed = 1.0f;
8.
9.     public float impactForce = 15.0f;
10.
11.
12.     // Update is called once per frame
13.     void FixedUpdate () {
14.         appliedSpeed += Time.deltaTime * 8.0f;
15.         appliedSpeed = Mathf.Min(appliedSpeed, theSpeed);
16.         // thisRB.velocity = (transform.forward * appliedSpeed);
17.         if(thisRB.velocity.z < theSpeed)
19.     }
20.
21.     private void OnCollisionEnter(Collision collision)
22.     {
23.           Vector3 reboundDirection = -transform.forward;
24.           // I have applied force in direction of our car's back, since impacts were back pushed , mostly
25.           // CHANGE THIS BY collision.transform.forward , if you want our car to bounce back in the direction of opponent's forward!
26.
28.         appliedSpeed = 0.0f;
29.     }
30. }
Also , I got a similar result as your video , bu putting these values :

Rigidbody :
Mass : 1

Speed Script :
The Speed = 15
Impact Force = 40.

Hope it helps !!

siddharth3322 likes this.
5. ### siddharth3322

Joined:
Nov 29, 2013
Posts:
689
I have given you a personal message with my project's more details included in it. So please check this and reply back to me.