- Home /
How to calculate a RigidBody2D's bounce from a collider?
Ok, so after trying to figure it out by myself for the last three days I'm finally ready for some help :P.
I'm building circle pong like game and i've been trying to implement a mechanic where time slows down to almost a halt (0.0001) for everything ingame except the player. During this slowmotion mode i want to show the trajectory that the ball will take after time is back to normal, like this:
But when i'm using this code to calculate the trajectory:
LayerMask playerMask = LayerMask.GetMask("Player");
LayerMask enemyMask = LayerMask.GetMask("Enemy");
while (active){
Vector2 dir = this.transform.position - ball.transform.position;
RaycastHit2D hit = Physics2D.CircleCast(ball.transform.position, ball.GetComponent<CircleCollider2D>().radius, dir, Mathf.Infinity, playerMask | enemyMask);
if (hit.point != null) {
lr.SetPosition(0, ball.transform.position);
lr.SetPosition(1, hit.point);
Vector2 ballpos2d = new Vector2(ball.transform.position.x, ball.transform.position.y);
Vector2 ballTrajectory = Vector2.Reflect( hit.point - ballpos2d, hit.normal);
lr.SetPosition(2, ballTrajectory * 50);
}
yield return null;
}
}
The ball is set to dynamic rigidBody2D with no mass or drag and physics material2D with 0.3 bounce and no friction.The problem is that the ball off by some degree, like this:
So is there any way to calculate how physics based object would bounce off a collider?
Answer by UnityVeris · Jan 22, 2018 at 02:32 AM
For a simple style like that I would encourage you to write your own physics from scratch, you would have way more control and then this kind of operation would become trivial.
Barring that, I don't believe it is possible to force a physics Update, because physics are based on time, so if no time has passed then nothing will happen, but you want an answer each frame.
Another option is to (with some trial and error) is to use the velocity of the ball (rigidbody.velocity) to figure out where the ball is going and how it would bounce when it hits the next object along it's path.
I tried your suggestion with using trial and error, but since physics calculations are not open source and I dont see the correlation between my projections and the actual bounce, had to drop this angle.
I also tried switching to custom physics but there seems to be a problem with collision detection when i do. I'm pretty sure it happens since my project is not scaled correctly..
Right now, I'm trying setting the ball to kinematic after the slow$$anonymous$$otion ends and then restoring it to dynamic and setting it's velocity manually on it's first collision.
Thank for the help btw!!
Your answer
Follow this Question
Related Questions
Problem with Ball Bouncing after Collision 2 Answers
How to make physics relative to moving parent 1 Answer
Rigidbody2D: Follow other Rigidbody2D strictly 1 Answer
Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer
Rigidbody bounce to a place without physics material. 0 Answers