Circles, check

2014 02 10 03:28.

I wanted to fix the circles as well. I wanted to make solid circles, just like I did the rectangles. This was acually mainly for my own sake. Two of the circles push each other around and the third stays where it is.

I thought I would just have to do it exactly like I did with the rectangles, but it didn’t quite work out that way.

I first checked if the distance between the circles centres where lesser than or equal to their radii. I covered the details in an earlier blog.

If this was the case, I checked for their x-positions and y-positions. if one of them were greater than the other I would set a float with the value: delta position x – (R1+R2), to its negative value and assign the vector2 offset’s x value to that.
I did the same if one of the y-positions were greater than the other’s, only the value was: delta position y – (R1+R2). (Rn beeing their radii).

This made some sort of hybrid squre-circle collision that I did not like at all. There were no invisible corners, but the other circle could only move up, down, left or right when I pushed it with a circle. When I moved one circle to collide with the other at an angle of approximatly 45 degrees, it would leap a Little to the right or straight up. I wanted it to bumb away in the approximate angle I hit it with.

I tried a few different things that didn’t work out at all. So I grabbed a pen and paper and tried to draw what I acctually wanted to do.

I decided I wanted to use vectors because if I made the amount of overlap into a vector instead of a simple value and assigned that vector to the offset I would get the desired action in case of a collision.

The Unit Vector was my choice of doing this. If I had the direction from one circle to the other and if there was an overlap, offset would be a vector pointing in the same direction as the Unit Vector with the lenght of the overlap.

This worked! I tried a number of times Before I got it right. A made the circles stick to each other if they tuched in the wrong Place, and sometimes they would disappere. After a while I stopped to Think. Then I checked the code our programming master gave me to see how the Unit Vector was implemented there. Instead of using the delta positions of the colliders in a vector and tried to normalize that, I should have used their acctual positions, like in the ClosestPoint function(also shown in an earier post). Of course.

When I did that, it worked like a sharm.

And since the normalized Unit vector is 1 lenght unit long I multiplied it with the amount of the overlap which I got by subtracting the sum of the radii from the distance.

Working circle collision with solid circles:



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s