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

Author Topic: Using getGlobalBounds of a member object.  (Read 2434 times)

0 Members and 1 Guest are viewing this topic.

legacycf

  • Newbie
  • *
  • Posts: 3
    • View Profile
Using getGlobalBounds of a member object.
« on: July 15, 2015, 03:36:42 am »
Hey guys,

I'm trying to program a very basic avoider type game just to kind of remind myself of some coding topics, and my skills are proving rustier than I imagined. One of the issues i'm having is with collision detection and getGlobalBounds(). I'm trying to use the bounds of a member object (in this case text) as the bounds of the classes i have made. I can't get that to work at all, and can't seem to figure it out.


I have a class that looks like this(please be gentle, I haven't coded something like this in YEARS):

class Enemy : public sf::Sprite
{
public:
     Enemy();
     ~Enemy();
     sf::FloatRect getMemberBounds();
private:
     virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
     sf::Font enemyFont; //the enemies are letters for now
     sf::Text enemyText;
};

Enemy::Enemy()
{
     //text setup that i *think* has no effect on the collision box goes here
     setPosition(500, 500);
     enemyText.setPosition(getPosition());
}

Enemy::getMemberBounds()
{
     return enemyText.getGlobalBounds();//use text bounds as enemy bounds
}

Enemy::draw(sf::RenderTarget&, sf::RenderStates states) const
{
     states.transform *= getTransform();
     target.draw(enemyText);
}
 

and then I have a Playable class for the player controlled character which is setup in very much the same way. I realize that my method getMemberBounds() is probably a horrible way to go about this, and might be the source of my issue. but what I want to be able to do is this:

if(character.getMemberBounds().intersects(enemy.getMemberBounds()))
{
     //near phase collision detection calls
}
 


I'm kind of at a loss for tweaks/changes to try. so any suggestions/critiques/tips are more than welcome. and if i haven't given enough information, I will edit and add w/e i need to.

thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using getGlobalBounds of a member object.
« Reply #1 on: July 15, 2015, 07:39:21 am »
Quote
if i haven't given enough information
Well, you didn't describe the most important: your problem. All you said is that you can't get it to work, which is not very precise... ;)
Laurent Gomila - SFML developer

legacycf

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Using getGlobalBounds of a member object.
« Reply #2 on: July 16, 2015, 01:38:46 am »
Quote
if i haven't given enough information
Well, you didn't describe the most important: your problem. All you said is that you can't get it to work, which is not very precise... ;)

haha oops. Yeah I guess I should explain that a bit more. I have that second snippet in my code as it is, but the expression is never found to be true and whatever i put inside there is never run. I actually have two collision detections in my code right now:

if(character.getMemberBounds().intersects(enemy.getMemberBounds()))
{
     //near phase collision detection calls
}
 
which doesn't work, and the following one that does work:

if(character.getMemberBounds().intersects(myCircleShape.getGlobalBounds()))
{
     std::cout<<"Collision detected! (character and circle)"<<std::endl;
}
 

the second one I used as a test and works as I expect it would, which only confuses me more why the first example is never evaluated as true.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using getGlobalBounds of a member object.
« Reply #3 on: July 16, 2015, 08:08:13 am »
The text is drawn using the transform of its container class (Enemy). But this transform is only applied when you draw it, which means that the bounding rectangle that you get in getMemberBounds() doesn't take this transform in account, and is thus possibly offsetted/rotated/scaled compared to what is drawn.

By the way, why do you inherit from sf::Sprite if your class implements its own drawing/bounds/etc...?
Laurent Gomila - SFML developer

legacycf

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Using getGlobalBounds of a member object.
« Reply #4 on: July 19, 2015, 10:19:19 pm »
The text is drawn using the transform of its container class (Enemy). But this transform is only applied when you draw it, which means that the bounding rectangle that you get in getMemberBounds() doesn't take this transform in account, and is thus possibly offsetted/rotated/scaled compared to what is drawn.

By the way, why do you inherit from sf::Sprite if your class implements its own drawing/bounds/etc...?

So if i'm understanding correctly, i'm actually transforming the entity twice?

and I was using sprite because originally I had planned to actually import sprites for the enemy...but got lazy  :-[

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using getGlobalBounds of a member object.
« Reply #5 on: July 20, 2015, 07:33:10 am »
Quote
So if i'm understanding correctly, i'm actually transforming the entity twice?
No. You're only transforming the entity temporarily when you draw it. Everywhere else, the entity is untransformed.
Laurent Gomila - SFML developer