- Home /
ContactPoint2D.contacts[0].normal returns sometimes returns different value that breaks my game.
Hi,
I have a 2d game where the ball (Player) bounces from obstacles like a laser using Vector2.Reflect.
I tried everything, and through the debugger I noticed that sometimes ContactPoint2D.contacts[0].normal returns a different value.
So, with this function, I reflect the ball:
void ReflectBall(ContactPoint2D contact, Vector2 ballDirection)
{
Vector2 inDirection = Vector2.Reflect(ballDirection, contact.normal);
float rot = Mathf.Atan2(inDirection.y, inDirection.x) * Mathf.Rad2Deg;
_transform.eulerAngles = new Vector3(0, 0, rot);
}
And now, using the debugger, I see that the normal of the contact point is x(0.5) y(-0.9)

Now I set the debugger to break only when the normal does not equal that value (0.5, -0.9). And I tested on the same game object, hitting it on the same spot:

Here is the object with it's collider:
as well its physics components:
Now here is the OnCollisionEnter function:
void OnCollisionEnter2D(Collision2D col)
{
Vector2 forward = _transform.right;
// ball hit bound of gamefield, level is going to reset
if (col.gameObject.layer == LayerMask.NameToLayer(Constants.LayerBounds))
{
shouldResetTransform = true;
Reset(false);
}
//if ball hit on figure
else
{
string tag = col.gameObject.tag;
if (tag.Equals("Normal") || tag.Equals("Hard") || tag.Equals("NotDisappear"))
{
ContactPoint2D contact = col.contacts[0];
ReflectBall(contact, forward);
OnHitResponse(col.gameObject);
}
}
}
OnHitResponse() is a heavy function, but it's called after RefflectBall().
I'd really appreciate any ideas. Thanks :)
Answer by MelvMay · Feb 13, 2017 at 09:40 AM
For starters, something is very odd with that normal as it's supposed to be normalized and (0.5, -0.9) clearly isn't.
Ensure that you're using continuous collision detection otherwise you can be penetrating the other collider.
Also, changing the Transform of a GameObject that uses a Dynamic body-type Rigidbody2D is bad behaviour. If you want to modify the rotation of a Rigidbody2D then set its rotation property i.e. let the Rigidbody2D type drive the Transform, not the other way around.
As far as I understand, this is a surface normal, not a normalized vector.
A surface normal vector is the vector which angle with the surface is perpendicular.
See this
Your answer
Follow this Question
Related Questions
Physics2D.IgnoreCollision not working! 0 Answers
Collider2D.GetContacts() Checking One Physics Step Behind 0 Answers
How to use OverlapCollider? 1 Answer
Physics2D.IgnoreCollision Not Working 0 Answers