SFML community forums

Help => General => Topic started by: Mark on December 20, 2013, 11:23:34 am

Title: How to check if Mario collides with a block? [PARTIALLY SOLVED]
Post by: Mark on December 20, 2013, 11:23:34 am
Hello guys :) I've been busy this week but now I'm getting long holidays and I'll be able to code all night! Right now I'm almost finished creating a very tiny Mario clone. It's nothing but a framework of his motion (no map scrolling, no enemies or dangers (except that you die if you go off the screen)) but I'm still hitting a problem. I managed to get gravity and inertia working fine, but how do I handle Mario's collision with block? I'd know how to do it if his sprite would be a collection of solid visible pixels, but there are many full alpha pixels too! I can explain the whole game cycle more specifically so you could understand me better:
How do I make the third step? Any function to do it for me?
I've heard something about function that checks if one rectangle contains the other, but does that function consider the alpha channel? Anyone?

Thanks in advance ;D
I'll be able to send the code and the game later (currently I'm away from home, coming back after ~5 hours).

UPDATE: Found some BIG code snippets here: https://github.com/SFML/SFML/wiki/Source%3A-Simple-Collision-Detection
Well, this simple colission doesn't look so simple. Maybe I should turn Mario to perfect square. That would make things much easier...
Title: Re: How to check if Mario collides with a block?
Post by: Mark on December 20, 2013, 03:48:28 pm
Nevermind, I'll solve it by replacing Mario with a square character. I guess I'll figure this out later, and I can note that I already know how to make buttons of any shape by checking the alpha value at the mouse pointer coordinates (collision detection has some similarities), but now I've got some more serious topics to be learned.
Title: Re: How to check if Mario collides with a block? [PARTIALLY SOLVED]
Post by: Sir Lulzalot on December 20, 2013, 04:23:11 pm
You can use the size attribute of an object (more easily rectangles/squares) to determine where a character can be placed when colliding with other objects.

For example if you collide from the left (therefore your character is moving to the right) remember your point of origin is always top left. So you'd want your character's position to be objectCollided.getPosition().x-character.getPosition().x (maybe add a -1 depending on how you treat collisions) and the y coordinate can be exactly as it was before.