# 2d Collision detection response equation doesn't work

I am currently doing some game engine programming as a hobby. I have been working on getting the collision detection working for a while now, and I'm at my wits end with it.

I started with this guide. If you scroll down the the picture of the character sprite, you will see that she uses points on the character instead of a bounding box to do the collision. That is what I currently have implemented, but I'm having a ridiculously hard time making it work perfectly.

I started with axis aligned, but moved to separating axis theorem so that I can implement collision boxes that are at an angle. The collision response I'm trying to use when a collision occurs on the bottom of the character is this horrible thing:

nextY = (bottomColl.getY1() + bottomColl.getCenterY() + (currHeight() / 2) +
(nextX + collisionPoints[currState][currFrame][4][0] - bottomColl.getCenterX() + (currHeight() / 2) * tan(angle / 2)) * tan(angle));


Y1 being top left corner and collisionPoints 4 being the bottom right point. (This is for an incline as opposed to a decline. The decline would use point 5, the bottom left point.)

This, however, doesn't work, and updates the character position to slightly lower than he is supposed to be. This has little effect, unless the frame rate is really high, in which case, the jump animation does not move the character out of the collision box and the character gets pushed way far away.
I realize the code is probably hard to decipher out of context, but I'm pretty damn sure the algorithm should be right, yet it doesn't work properly.

Would it be better to switch to using a bounding box for the character and figure out collision response for that, or does anyone know what the proper equation is for setting the character height on an incline given an X position?

Edit: Here is a picture of me showing the equation works in CAD. The calculator shows me using my equation from above to get the same number as is indicated on the sketch.

• Blue is the ground. Green is entity's location during collision. Black is desired position after collision response. Red is inconsequential, just how data is stored. – Xerict Apr 5 '15 at 1:47

Everything is green, so I can't tell what is is a player, what is a block, and what "2" means or is supposed to mean.

I did make you this, however:

The diagram can be mirrored and/or rotated and, with a tweak to the math, match any orientation of collider and collidee. Let me know if this isn't what you need and I'll try again.

These triangles are all in the same proportions; I think the left-most hollow point is a "collision point" so you are given the red, blue, and magenta for that small triangle. If you make blue twice as long, pink will be twice as long, etc.

• I don't really understand what you are projecting onto... In my second image, the red box is how the collision box is stored, along with an angle, which when tested in the collision detection, means that character is being compared to the blue box. Basically the red is just for calculations I needed to make. The numbers mean nothing, its just an example. The black box is the position that I want to set the player to, assuming there was a collision detected. I just wanted to show that the code I posted would get the right answer. – Xerict Apr 5 '15 at 1:03
• Oh man that second image makes a difference! – Jon Apr 5 '15 at 1:06
• I feel like I should just state that my goal here is this: Player(black) intersects collision box(blue). NO movement in X is desired, I just want the player's Y position to move up until it is at the edge of the blue box. Just in case it simplifies things for you :) – Xerict Apr 5 '15 at 1:10
• I just added the green box, which would be where the player was when the collision was detected. Black is where I want the player to be – Xerict Apr 5 '15 at 1:36
• Well, damn. I don't know what is wrong with my trig, but like triangles is totally doing the trick. I don't know why I didn't think about it :) I really over complicated this. – Xerict Apr 5 '15 at 2:06