Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to check if Mario collides with a block? [PARTIALLY SOLVED]  (Read 1513 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Guest
How to check if Mario collides with a block? [PARTIALLY SOLVED]
« 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:
  • Draw the level (both sprites (blocks and Mario) are 40x40)
  • Handle events and interact with the user.
  • Check if any visible pixel of Mario hasn't collided with a with any of the block
  • If it has collided, push Mario back to the side of the block and change the values of needed velocities to zero.
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...
« Last Edit: December 20, 2013, 03:48:46 pm by Mark »

Mark

  • Guest
Re: How to check if Mario collides with a block?
« Reply #1 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.

Sir Lulzalot

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to check if Mario collides with a block? [PARTIALLY SOLVED]
« Reply #2 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.

 

anything