- Home /
Simple ball bounce (like pang/bubble trouble)
Hello,
I am trying to build a sequel to my game in Unity (http://www.rebubbled.com). If you feel like looking at the first level of the game you will understand what I am after.
In writing - I am trying to achieve the effect of ball (in a perfect bounce world) bouncing always same velocity on X after collision, and adjusting Y velocity depending on its Y position if needed so it always jumps to predetermined height (if it is jumping upwards after collision - if not, let it just use gravity and keep falling 'naturally').
2D is perfect, 3D works too.
After trying and failing many times over I figured I need some help.
Here is a snippet of calculating the new velocity.y (in case ball is moving upwards, after collision), if that helps..
float relativePositionY = JUMP_HEIGHT - currentPosition.y;
float newYVelocity = Mathf.Sqrt(2 * relativePositionY * Physics2D.gravity.y);
Thank you in advance!
Kreso
Answer by kreso · Jul 28, 2014 at 07:53 AM
To answer my own question:
This is very easy to do. First mistake I did was use physics materials. No need for those here. Other than that - just using hit normal did the trick.
private float velX = 1.9f;//horizontal speed of ball
private Vector2 Vel_in;//save incoming velocity here
private float startY;//max jump height (every time ball hits floor it will calculate force needed to reach this height).
void FixedUpdate()
{
Vel_in = rigidbody2D.velocity;
}
void OnCollisionEnter2D(Collision2D coll)
{
ContactPoint2D hit = coll.contacts[0]; //(for debug only) the first contact is enough
Vector3 Vel_out = Vector3.Reflect(Vel_in, hit.normal);
if(hit.normal.x < 0)
rigidbody2D.velocity = new Vector2( - velX, Vel_in.y);
else if(hit.normal.x > 0)
rigidbody2D.velocity = new Vector2( velX, Vel_in.y);
else
rigidbody2D.velocity = new Vector2( velX*(Vel_in.x/Mathf.Abs(Vel_in.x)), rigidbody2D.velocity.y);
if(hit.normal.y < 0)
{
if (Mathf.Abs(Vel_in.y) < 1)
rigidbody2D.velocity = new Vector2( rigidbody2D.velocity.x, -1);
else
rigidbody2D.velocity = new Vector2( rigidbody2D.velocity.x, -Mathf.Abs(Vel_in.y));
} else if(hit.normal.y > 0)
{ //jumping up, calculate how much force is needed to jump to certain height (startY)
float relPos = transform.position.y - startY;
if(relPos > 0f)
relPos = 0f;
float newYVel = Mathf.Sqrt(2 * relPos * Physics2D.gravity.y * rigidbody2D.gravityScale);
if (newYVel == 0) newYVel = 1f;
rigidbody2D.velocity = new Vector2( rigidbody2D.velocity.x, newYVel);
}
//save now, because sometimes collision happens before next FixedUpdate tick
Vel_in = rigidbody2D.velocity;
}
Answer by Gordinho · Apr 24, 2016 at 04:24 PM
Hello, I played your pang clone and it looks cool. Do you think you could share the code to make the spheres bounce and move?. I tried the code above but my sphere stays jumping from one side to the other and doesn't move anywhere else, just stays jumping in two spots. Thanks.
Answer by Gnomemann · Jun 15, 2018 at 09:46 PM
Seems like you get an error at
rigidbody2D.velocity = new Vector2( velX*(Vel_in.x/Mathf.Abs(Vel_in.x)), rigidbody2D.velocity.y);
At least I did and I can't seem to figure out how to fix it. It returns a NaN so I assume it's a division by 0.
Edit: I made the calculation, it multiplies 1.9f with 0 / 0 which returns a NaN.
Your answer
Follow this Question
Related Questions
Bouncing ball stops when hitting edge of platform 2 Answers
Best way to achieve Sonic style physics? 0 Answers
Soccer Ball Kick Help 2 Answers
Constant force, with gravity, and correct collisions. 2 Answers