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

Author Topic: My collision detection stopped working after using polymorphism  (Read 1312 times)

0 Members and 3 Guests are viewing this topic.

Missqu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Hello everyone,
So I had class A, and class B, and external function that would check collision for me. It looked something like that
bool collision(*A a, *B )
{
  if (a->sprite.getGlobalBounds().intersects(b->sprite.getGlobalBounds()) &&
                b->sprite.getGlobalBounds().intersects(a->sprite.getGlobalBounds()))
       {
return true;
        }
}
then I made parent class and put checking collision there
bool collision(parentclass *mleczny)
{
if (this->sprite.getGlobalBounds().intersects(mleczny->sprite.getGlobalBounds()) &&
                mleczny->sprite.getGlobalBounds().intersects(this->sprite.getGlobalBounds()))
{
 return true;
}
}
but after using it on my subclass A
 a.collision(&b);
it keeps returning false. Also I had few other functions that I used in both classes and put them into my parent class and they work just fine!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: My collision detection stopped working after using polymorphism
« Reply #1 on: May 28, 2017, 10:10:53 pm »
Use your debugger and/or add some std::cout statements, to check all the passed values.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Missqu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: My collision detection stopped working after using polymorphism
« Reply #2 on: May 30, 2017, 03:45:28 am »
So I noticed something really weird. Passed sprites object has both width and height of 0.
so for example when I call my collision function and cout: mleczny->sprite.getGlobalbounds.height;
it will display zero, but for "this" pointer it gives correct parameters.
I think that passing object of my class somehow that object starts reffering to empty sprite ?
« Last Edit: May 30, 2017, 04:05:24 am by Missqu »