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

Author Topic: Getting boundaries of non-rectangular sprite?  (Read 9046 times)

0 Members and 3 Guests are viewing this topic.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Getting boundaries of non-rectangular sprite?
« Reply #15 on: September 24, 2014, 12:07:19 am »
With the copy constructor...?

sf::RectangleShape shape1;
...
sf::RectangleShape shape2 = shape1; //copy!
 

This is basic C++.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Getting boundaries of non-rectangular sprite?
« Reply #16 on: September 24, 2014, 01:02:57 am »
Ok, that would make sense. So do sf::FloatRects not do well inside vectors or something? Because by all means that I can tell, this code should work:
if(event.type == (sf::Event::KeyPressed)){
                        switch(event.key.code){
                        case sf::Keyboard::Left:{
                                GamePiece copySprite = level.GetGamePieces().at(temp - 1);
                                copySprite.MovePiece(-1, 0);
                                for(int i = 0; i < 4; i++){
                                        sf::FloatRect fr = copySprite.GetPieceRectangles()[i].getGlobalBounds();
                                        for(int i = 0; i < level.GetCollisionObjects().size(); i++){
                                                if(fr.intersects(level.GetCollisionObjects()[i]))
                                                        std::cout << "hit\n";
                                        }
                                }
                                level.GetGamePieces().at(temp - 1).MovePiece(-1, 0);
                                std::cout << "left ";
                        }
                        break;

I'm debugging, so all I'm looking for is for the console to display "hit" when the piece rides on the left wall, but the piece can go all the way across it with no sign of this code actually working.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Getting boundaries of non-rectangular sprite?
« Reply #17 on: September 24, 2014, 08:13:08 am »
A floatrect is a tiny class with a few floats in it.  Nothing that would cause problems in standard containers.

There's so much of your code mixed in with the SFML stuff there that I don't think we can figure out what the problem is.  If you could post a complete and minimal example with this problem using (almost) nothing but SFML, then we can help.  Otherwise it's anyone's guess, although odds are it's a bug in one of your classes.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Getting boundaries of non-rectangular sprite?
« Reply #18 on: September 24, 2014, 02:44:31 pm »
Ok so I'll try to keep this simple. I think the problem is with how I'm using sf::RectangleShape, because they're not drawing to the screen. Here is where they make an appearence in my gamepiece constructor:
pieceShape.setTexture(imgr.GetImage("line.png"));
                for(int i = 0; i < 4; i++){
                        sf::RectangleShape rect;
                        rect.setPosition(pieceShape.getPosition().x + (i * 10), pieceShape.getPosition().y);
                        rect.setFillColor(sf::Color::Blue);
                        pieceRectangles_.push_back(rect);
                }

I'm setting their color to blue to see if they're even on the screen where I intend for them to be, but they're simply not appearing.
//for(int i = 0; i < level.GetGamePieces().size(); i++){
                //window.draw(level.GetGamePieces()[i].GetPieceSprite());
        //}

        for(int i = 0; i < level.GetGamePieces().size(); i++){
                window.draw(level.GetGamePieces()[i].GetPieceRectangles()[i]);
        }
The commented section is where I draw the sprite, that works fine. The bottom for loop does nothing visible, as if the sf::RectangleShapes I created weren't even there...
Related functions:
std::vector<sf::RectangleShape> pieceRectangles_;
std::vector<sf::RectangleShape> &GamePiece::GetPieceRectangles(){
        return pieceRectangles_;
}
std::vector<GamePiece> gamePieces_;
std::vector<GamePiece> &Level::GetGamePieces(){
        return gamePieces_;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Getting boundaries of non-rectangular sprite?
« Reply #19 on: September 24, 2014, 02:47:45 pm »
There should be two nested loops: one for the game pieces, and another for the current piece's rectangles. You're currently using the same index (i) for both.

You should also give a valid size to your rectangle shapes.
Laurent Gomila - SFML developer

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Getting boundaries of non-rectangular sprite?
« Reply #20 on: September 24, 2014, 05:54:31 pm »
You should also give a valid size to your rectangle shapes.
size. Wow, that solution puts me to shame. Well, still after setting their size to something actually visible, only one square is showing up. I think all four squares are stacking on top of each other, like their position isn't being set.

Edit:: Wait, wait, that was related to the for loop issue you described. Ok so "collision detection" (still a work in progress) seems to be working now, so I'll post the code for future reference:
pieceShape.setTexture(imgr.GetImage("line.png"));
                for(int i = 0; i < 4; i++){
                        sf::RectangleShape rect;
                        rect.setPosition(pieceShape.getPosition().x + (i * 10), pieceShape.getPosition().y);
                        rect.setSize(sf::Vector2f(10, 10));
                        rect.setFillColor(sf::Color::Blue);
                        pieceRectangles_.push_back(rect);
                }
                break;


if(event.type == (sf::Event::KeyPressed)){
                        switch(event.key.code){
                        case sf::Keyboard::Left:{
                                GamePiece copySprite = level.GetGamePieces().at(temp - 1);
                                copySprite.MovePiece(-1, 0);
                                for(int i = 0; i < 4; i++){
                                        sf::FloatRect fr = copySprite.GetPieceRectangles()[i].getGlobalBounds();
                                        for(int j = 0; j < level.GetCollisionObjects().size(); j++){
                                                if(fr.intersects(level.GetCollisionObjects()[j]))
                                                        std::cout << "hit\n";
                                        }
                                }
                                level.GetGamePieces().at(temp - 1).MovePiece(-1, 0);
                                std::cout << "left ";
                        }
                        break;

                        case sf::Keyboard::Right:{
                                GamePiece copySprite = level.GetGamePieces().at(temp - 1);
                                copySprite.MovePiece(1, 0);
                                for(int i = 0; i < 4; i++){
                                        sf::FloatRect fr = copySprite.GetPieceRectangles()[i].getGlobalBounds();
                                        for(int j = 0; j < level.GetCollisionObjects().size(); j++){
                                                if(fr.intersects(level.GetCollisionObjects()[j]))
                                                        std::cout << "hit\n";
                                        }
                                }
                                level.GetGamePieces().at(temp - 1).MovePiece(1, 0);
                                std::cout << "right ";
                        }
                        break;
                        }
                }
        }
        if(timeElapsed.asMilliseconds() >= 10000){
                timeElapsed = clock.restart();
                GamePiece copySprite = level.GetGamePieces().at(temp - 1);
                copySprite.MovePiece(0, 1);
                for(int i = 0; i < 4; i++){
                        sf::FloatRect fr = copySprite.GetPieceRectangles()[i].getGlobalBounds();
                        for(int j = 0; j < level.GetCollisionObjects().size(); j++){
                                if(fr.intersects(level.GetCollisionObjects()[j]))
                                        std::cout << "hit\n";
                        }
                }
                level.GetGamePieces().at(temp - 1).MovePiece(0, 1);
                std::cout << "down ";
        }

for(int i = 0; i < level.GetGamePieces().size(); i++){
                window.draw(level.GetGamePieces()[i].GetPieceSprite());
                for(int j = 0; j < level.GetGamePieces()[i].GetPieceRectangles().size(); j++){
                        window.draw(level.GetGamePieces()[i].GetPieceRectangles()[j]);
                }      
        }
 
« Last Edit: September 24, 2014, 06:09:46 pm by wh1t3crayon »