- Home /
2D Bouncing formula doesn't work properly
I am new to unity, and i am trying to create a bouncing ball, so i've did many researches about bouncing realted physics and i found a formula :
Formula:
-2*(V dot N)*N + V
Where V is the velocity vector and N is the normal of the surface on which the ball will bounce
Here is my script :
using UnityEngine;
using System.Collections;
public class BallPhysics : MonoBehaviour {
void Start () {
rigidbody2D.velocity =new Vector2 (-1,-3);
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D col) {
if (col.gameObject.name == "Pong") {
tBounce ();
}
}
void tBounce(){
RaycastHit2D hit = Physics2D.Raycast (new Vector2 (transform.position.x,transform.position.y), new Vector2(-1f,-1f));
Vector2 n = hit.normal;
Vector2 v = rigidbody2D.velocity;
Vector2 R = -2 * (Vector2.Dot (v, n)) * n + v;
rigidbody2D.velocity = R;
}
}
I am giving the ball a velocity vector in the start function, i am using OnTriggerEnter2D for collision handling and raycast2D to get the normal of a surface.
The problem is that the script doesn't reflect the velocity vector called R, i think the probleme is in the normal vector.
For example let's say V is a Vector2(-1,-1) so basically R should be (-1,1), but it's not. R is (3,1) !
i've successfuly been able to make a ball bouncing on Horizontal/vertical surface by reversing the ball velocity but this won't work properly with arbitary angles,that's why i am using this formula. So what's the problem ?
Hi, why is the direction of your raycast (-1, -1)? Shouldn't it be rigidbody2D.velocity?
Even if i use rigidbody2d.velocity the same problem occurs
Answer by skylem · Jan 15, 2015 at 10:40 PM
Hello, Quick question is there any particular reason as to why u wish to do this with code? You could set a Physics material property to your 2D Collider and the properties within this Physics Material Include Bounce and Friction.
The ball will bounce in the same place and i dont want it to be so realistic
Answer by moichezmoi · Jan 15, 2015 at 11:05 PM
If you use the OnCollisionEnter2D method, you'll get a collision2D as parameter, that gives you more informations about the collision than the simple Collider2D you get right now with th OnTriggerEnter2D method.
You don't have to perform a raycast anymore :
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "Pong")
{
Vector2 n = col.contacts[0].normal;
Vector2 v = col.relativeVelocity;
rigidbody2D.velocity = -2 * (Vector2.Dot (v, n)) * n + v;
}
}
I think the raycast was the problem : it interact with your ball's collider instead of your "Pong" object.
To use the OnCollisionEnter2D method, you have to untick "is Trigger" on your collider components.
I've already did this the problem with this is that the pong move when there is a collision with the ball,and the only way to dfx that is to tick isTrigger.
I don't need it ,so without a rigidbody the pong won't move, right?
Your answer
![](https://koobas.hobune.stream/wayback/20220613180553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2d physics problem 1 Answer
New Pointer Effector 2D Help 0 Answers
2D Ragdoll 1 Answer
Trying to get a ball to bounce realistically in 2D. 2 Answers
2D Collision not working! (child sprites colliders) 1 Answer