abraker95 / tanks

2D arcade top-view shooting game
1 stars 0 forks source link

Rect-Circle Collision response #34

Closed ghost closed 9 years ago

ghost commented 9 years ago

Do you have an idea how to react once a rect-circle collision is detected? When the circle and the rectangle overlap I was thinking about doing something with the intersecting points but im not sure what.

possible cases

ghost commented 9 years ago

I always did rectangular collision be checking if it is in bounds. For example, if point x is between the left and right side of the object and is between the top and bottom side of the object, then we have collision. Of course that point has to be defined as a circle. So a point on the circle should be defined as the equation of a circle: (x - Cx)^2 + (y - Cy)^2 = r^2, where (Cx,Cy) is the center, r is the radius, and (x,y) is a point on the perimeter

The hard part comes in when you actually need to check for points. So if a point is within a circular bound if (x+Cx)^2<r^2 and (y+Cy)^2<r^2, where (x,y) is a test point If you need me, I will be able to work on it later, but I'm sure you can figure it out from here.

ghost commented 9 years ago

Thank you.

But you're talking about the collision detection, right? I was wondering what to do after the collision is detected. We need to move one object so that the objects are not overlapping anymore, so they're just touching.

ghost commented 9 years ago

How did you do it in circular-circular collision? I though it's the same concept.

On Tue, Nov 18, 2014 at 10:33 AM, Sherushe notifications@github.com wrote:

Thank you.

But you're talking about the collision detection, right? I was wondering what to do after the collision is detected. We need to move one object so that the objects are not overlapping anymore each other, so they're just touching.

— Reply to this email directly or view it on GitHub https://github.com/Sherushe/tanks/issues/34#issuecomment-63488611.

ghost commented 9 years ago
bool isCollidingWith(GameObject* _obj) const;
void collisionFeedback(GameObject* _obj);

isCollidingWith returns if it's colliding or not. Just a bool. Once the collision is detected, the collisionFeedback is called and move one object so that they're just touching not colliding.

ghost commented 9 years ago

So what is the problem applying that to rect-circle collision?

On Tue, Nov 18, 2014 at 10:44 AM, Sherushe notifications@github.com wrote:

bool isCollidingWith(GameObject* _obj) const;void collisionFeedback(GameObject* _obj);

isCollidingWith returns if it's colliding or not. Just a bool. Once the collision is detected, the collisionFeedback is called and move one object so that they're just touching not colliding.

— Reply to this email directly or view it on GitHub https://github.com/Sherushe/tanks/issues/34#issuecomment-63490631.

ghost commented 9 years ago

I didn't put it in the name but these functions only apply to circle-circle collision. Please I am not that stupid.

ghost commented 9 years ago

haha

ghost commented 9 years ago

I don't think I'm getting the problem.

On Tue, Nov 18, 2014 at 11:06 AM, Sherushe notifications@github.com wrote:

haha

— Reply to this email directly or view it on GitHub https://github.com/Sherushe/tanks/issues/34#issuecomment-63494630.

ghost commented 9 years ago

Moving two circles away from each other so they don't collide anymore is not the same as moving a rectangle and a circle from each other. Looks like it's the first time you're doing collisions management.

ghost commented 9 years ago

I found something interesting: http://www.metanetsoftware.com/technique/tutorialA.html

They're talking about the response.

ghost commented 9 years ago

That is interesting, nice find!

On Tue, Nov 18, 2014 at 12:27 PM, Sherushe notifications@github.com wrote:

I found something interesting: http://www.metanetsoftware.com/technique/tutorialA.html

They're talking about the response.

— Reply to this email directly or view it on GitHub https://github.com/Sherushe/tanks/issues/34#issuecomment-63509888.

ghost commented 9 years ago

Yeah.

The SAT they're describing looks good. The response i was talking about is relativly simple with this method.

ghost commented 9 years ago

okay i found a simple solution. It's a simplification of the article's method. If c is the center of the circle, and r1, r2, r3, r4 are the corners of the rectangle.

then do

d1.x = r1.x - c.x; d1.y = 0; d2.x = r1.x - c.x; d1.y = 0; d3.x = r1.x - c.x; d1.y = 0; d4.x = r1.x - c.x; d1.y = 0;

d5.x = 0; d1.y = r1.y - c.y; d6.x = 0; d1.y = r1.y - c.y; d7.x = 0; d1.y = r1.y - c.y; d8.x = 0; d1.y = r1.y - c.y;

take the smallest out of (d1,d2,...) and displace in that direction with the correct norm.

In short, do the smallest displacement on one of the axis.

ghost commented 9 years ago

Great! I'll take a look at the code to see how you did and ask you questions if I don't understand. On the other note, far we have been using bounding collision, which uses estimated boundaries of objects to calculate collision. Vector based collision, as I found, works best with small projectiles or formula based objects (like in 3D graphics, you know the location of every polygon). There is also mask collision, which prerenders a memory mask and checks it per-pixel within an object's bounds if there is a foreign pixel. Then there are techniques used to accelerate the process of searching for object collision such as quadtrees, object sorting based on location, and others. I think I'm good with what we got so far. If it starts to slow down under too many objects, or the collision is inaccurate, we may need to use something more advanced.

ghost commented 9 years ago

Yeah you got it. We will implement fancy features when we need them. For the moment, the number of objects on screen is not going to go higher than 20 i guess. I will implement rect-circle collision tomorrow and walls probably so that we can test if it works.

ghost commented 9 years ago

Sounds good.

On Tue, Nov 18, 2014 at 7:08 PM, Sherushe notifications@github.com wrote:

Yeah you got it. We will implement fancy features when we need them. For the moment, the number of objects on screen is not going to go higher than 20 i guess. I will implement rect-circle collision tomorrow and walls probably so that we can test if it works.

— Reply to this email directly or view it on GitHub https://github.com/Sherushe/tanks/issues/34#issuecomment-63570012.