More collision management

2014 02 04

Today I wanted to try my circle vs circle overlap function.

In our Game state, I made two test colliders and two circles using sf::CircleShape. The colliders were allocated and initiated via the overloaded constructor that take in a position vector and a radius float (I added a position to it). then I added a function to the CollisionManager. CheckCollisionCircleVsCircle, I called it. It is a bool function. I then pushed the colliders into my std::vector for colliders.

CheckCollisionCircleVsCircle():
CheckCVsC

This function checks the elements of the vector to see if there’s a collision and returns true if this is the case and returns false if it is no collision. In the Game states update function I started with Writing “Collision!” in the consol in case the manager’s checkcollisioncirclevscircle was true. Besides from that I had the colliders take the circles positions every update.

I also made a couple of keypresses to Control the circles left, right, up and down.

It turned out my circle vs circle overlap function really only was a Square vs Square overlap funktion because I checked both the x and the y axes with the radius, thus creating Square collisions. I noticed this when I tried to move one circle around the outline of the other. The consol started Writing “Collision!” when the angle between the circles were about 45 degrees(anti clockwise degrees) and the y axis were inline and the x axis also inline. the visible shapes did not touch but it behaved as if there was an invisible corner.. which in fact it was.

To solve this I used the distance formula derived from the phytagorean theorem (again) and added the two Catheti squared and compared it to the two radii added and squared. If the sum of the Squares of two catheti is smaller than the Square of the sum of the radii, the circles are overlapping. or in other Words when the distance between the centers of the circles is smaller than the sum of their radii.

Here is the working circle vs circle overlap function:
CircleVsCircle2

No invisible corners!
CircleCollision

There are some things I have to polish. for example I don’t know if I need an offset. the circle overlaps are not supposed to collide, but change an AI state for example, or enable you to eat Another fish.

Tomorrow I’ll see if I can make a rectangle vs circle overlap.

Leave a comment