- Home /
How to make CircleCast work properly with multi vertex EdgeCollider2D
Hello everyone. I'm not really used to asking for help in here, so I'll try to keep it as tidy as I can.
[Context]
I am trying to make a ball bounce within a group of Colliders (using BoxCollider2D and EdgeCollider2D) that compose the level's limits. I wanted to use my own custom physics, without Unity's physics influence, for gameplay purposes. I have removed all the RigidBodies from the colliding Objects, and did the collision solving code myself resorting to Unity's Raycast functions. The code in question is the following:
private List<HitInfo> check_collisions(Vector2 pos, Vector2 spd, out Vector2 new_speed)
{
float ray_length = spd.magnitude;
Vector2 dir = spd.normalized;
RaycastHit2D hit = Physics2D.CircleCast(pos, radius, dir, ray_length, collision_mask);
List<HitInfo> list = new List<HitInfo>();
HitInfo info = new HitInfo();
list.Add(info);
if(hit.collider != null)
{
bool on_going;
float bounced;
float perm;
on_going = true;
bounced = 1;
info.from_position = pos;
info.from_fraction = 0;
while(on_going)
{
perm = 2.0f * Vector2.Dot(dir, hit.normal);
dir = dir - (hit.normal * perm);
pos = (hit.point + (hit.normal * radius));
ray_length *= (1 - hit.fraction) * bounce;
bounced *= bounce;
info.speed = pos - info.from_position;
info.to_position = pos;
info.to_fraction = hit.fraction;
info = new HitInfo();
list.Add(info);
info.from_position = pos;
info.from_fraction = hit.fraction;
if(ray_length > 0)
{
hit = Physics2D.CircleCast(pos, radius, dir, ray_length, collision_mask);
if(hit.collider == null)
{
on_going = false;
info.speed = (dir * ray_length);
info.to_position = pos + info.speed;
info.to_fraction = 1;
}
}
else
{
on_going = false;
info.speed = (dir * ray_length);
info.to_position = pos + info.speed;
info.to_fraction = 1;
}
}
new_speed = dir * (speed.magnitude * bounced);
}
else
{
info.from_position = pos;
info.to_position = pos + spd;
info.speed = spd;
info.from_fraction = 0;
info.to_fraction = 1;
new_speed = spd;
}
return list;
}
[Problem]
The ball is able to collide quite perfectly with both BoxCollider2D and EdgeCollider2D with only 2 vertices. Sometimes however, it simply doesn't detect an EdgeCollider2D with various vertices. I even added RigidBodies to the Ball and the EdgeCollider2D producing the exact same results. I have created a few screenshots with different Ball speeds, and all of them with the a Kinematic RigidBody in each colliding object.
100 (pixels) speed units: Pic4 The ball was already outside the collision because the condition for the player to pause was for the ball to be outside the screen.
10000 (pixels) speed units: Pic5
As you can see, while it collides perfectly with both the EdgeCollider2D (with only 2 vertices) at the top of the screen, and the 2 BoxCollider2D at the bottom, every once in a while it fails to detect the EdgeCollider2D with multiple vertices on both sides (I only have pictures of the ball leaving the left side, but it does the same with the right). All this error happen only moments after I start the game at the higher speeds.
One detail though. This collisions are not "hand made" in the scene window. I have an inspector editor script that allow me to create this walls more easily.
More importantly, if I remove all the vertices of the EdgeCollider2D leaving only 2 (still using my inspector editor), the collisions work perfectly. As is can be seen on the following picture, even after over an hour play by itself, the ball keeps doing spaghetti: link text
Did anyone else found this kind of problem? Is there any way around it without having to use multiple EdgeCollider2D or multiple BoxColliders?
Your answer
Follow this Question
Related Questions
Is there a way of casting Rays at the end of the collider of an object ? 0 Answers
How can i shoot 2D ball/bounce of wall using Raycast? 1 Answer
Is there a way to only acces one collider when there are many colliders with the same tag? 1 Answer
HOW TO ONLY DETECT ANGLES BY COLLIDERS2D FACES 0 Answers
How can I do a 2D raycast against one specific collider? 3 Answers