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

Author Topic: Snake Game, detecting collision help!  (Read 1534 times)

0 Members and 1 Guest are viewing this topic.

mchtsyn

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Snake Game, detecting collision help!
« on: April 22, 2022, 07:52:55 pm »
Hi, I am trying to detect collision between a snake(Rectangleshape) and an apple(Sprite).



The function that needs to detect collision is marked red as shown in the screenshot. It's not working at all. The problem is that when I write the same if statement that detects collision in the main function, it works fine. But when it's implemented in a class it doesn't work. I think my problem is referencing a class member which is from another class. Can you help..?

"snake" is the object for mySnake class.
"yilan" is the RectangleShape referance.
"sprite" is the sprite for Elma class.


screenshots:
https://www.hizliresim.com/du53hc8
https://www.hizliresim.com/hqk5ses
https://www.hizliresim.com/2szihm5
« Last Edit: April 22, 2022, 11:34:29 pm by mchtsyn »

antoine2509

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Snake Game, detecting collision help!
« Reply #1 on: April 22, 2022, 10:48:30 pm »
Hello!

Could you please give some details about what is not working properly?

One thing I could guess is that the collision can be detected a bit too early because the global bounds of the sprite is a bounding rectangle around your sprite (and you can't see it if your apple texture has a transparent background).

So it means :
1) the rectangle is a rough approximation of an apple shape so you'll touch the bounding rectangle slightly before you'll touch the apple inside, especially in the corners.
2) if your sprite is not tightly fitted to the apple (there is free space between the apple and the side of the sprite), it will be even worse since the bounding rectangle is defined around the sprite.

You can either resize the rectangle of your sprite when you set the texture to better fit the apple, or define your own function for colliding the apple by approximating it with a circle for example, it will be better than a rectangle.

mchtsyn

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Snake Game, detecting collision help!
« Reply #2 on: April 22, 2022, 11:35:55 pm »
No sir, collision is not detected at all. I've modified the post, could you reply if you have any idea?

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Snake Game, detecting collision help!
« Reply #3 on: April 23, 2022, 02:44:02 am »
You have two snakes.
There's the first snake in main.cpp which is the one being updated and drawn.
Then there's a second snake in fruit.h inside of the Elma class. This is the snake that Elma's collision() is testing against, but it isn't updated so it never moves.

What I'd do is remove the snake member from Elma. Then change the collision function to take a snake reference like this:
void collision(mySnake &snake)

Now in the main.cpp you can do this:
elma.collision(snake);
 

mchtsyn

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Snake Game, detecting collision help!
« Reply #4 on: April 23, 2022, 03:15:04 am »
Thank you so much, solved!