This is my project file:
SDL collision is the problem this time. When this is downloaded, look at collision.cpp, player.cpp, and kyden.cpp.
I'm trying to make my player stop when he/she collides with the crate. If you play Kyden.exe, you can clearly see the box can only collide with Y coordinates(Or is it X?). Also, its all buggy when I collide with the top of the screen, the box is unable to leave from its grip.
There is no errors whatsoever, its just buggy. Thanks in advance for the help I recieve.
Might have better luck putting this on pastebin. I'm sure not opening a random zip file.
As for some of the errors you're describing, what I've done is if collision gets "stuck" (ie the object enters another object so collision detection stops it from moving altogether) is to basically just move the object back a tiny amount after collision. It's not noticeable to the eye, and it keeps it from getting stuck.
But there's probably better ways. My experience with graphics and games is pretty limited.
What I do is, every time an object tries to move, I have it check for collisions with other nearby objects and walls and such (collision detection is done by the objects themselves and only when they try to move move). I detect collisions by simply reducing the size of each object's bounding box by 10% (to allow some overlap) and then checking if the moving object would overlap the other object if it were allowed to move. Then all I do is figure out how much it would overlap by, and take that much off of its motion vector so that it can't overlap (except by the 10% allowance). Since all the detection is done by a single thread (the main thread), there's no way two objects can move simultaneously, which would circumvent the detection. Of course, it's done so fast by the computer that the player can't tell that it's not simultaneous.
I'd like to do pixel-by-pixel detection rather than just using boxes but I think that would be too slow and complicated to do for every object.
Rectangular collision fits my purposes for now, I'm planning on changing that.
I check for a collision every CPU cycle, as collision detection is in the player's update function. The problem is that the rectangular box won't move back once it collides. Also, the left & right side of the rectangle won't collide with the player's bounding box for some reason.
well, -1 may not be enough. if your position is changing at a rate of, say, 6px per frame then 1 is not enough. That is why I suggested simply having a 'previous' variable so before you update, you copy the position to previous, that way on collision, just revert position to previous. That way when it renders, the character is in the same position.