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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - bglaze

Pages: 1 [2] 3 4
16
Graphics / Help with this bouncing code??
« on: October 30, 2011, 09:07:08 pm »
My "rocks" are not bouncing off of each other in the most natural way, and I was wondering if anyone would be willing to look at my code and give me some advice on a better way to accomplish better rect-wall-detection of my sprite and the sprite it is bouncing off of.

Here is the code I am using to cause my rocks to bounce off of each other:

Code: [Select]
for(unsigned sprite1 = 0; sprite1 < rockbox.size(); sprite1++){
    rockbox[sprite1].Update();
    Window.Draw(rockbox[sprite1]);

    for(unsigned sprite2 = 0; sprite2 < rockbox.size(); sprite2++){
        if(sprite1 != sprite2){
            bounce_sprites(rockbox[sprite1], rockbox[sprite2]);
        }
    }
}


Here is the function bounce_sprites:

Code: [Select]
void bounce_sprites(SubSprite& sprite1, SubSprite& sprite2)
{
    if(sprite1.Rect.Intersects(sprite2.Rect)){

        if(sprite1.Rect.Top <= sprite2.Rect.Top + sprite2.Rect.Height
           and sprite1.Rect.Top + sprite1.Rect.Height >= sprite2.Rect.Top + sprite2.Rect.Height){

            switch(sprite1.Direction){
                case UPRIGHT:
                    sprite1.Direction = DOWNRIGHT;
                    break;
                case UPLEFT:
                    sprite1.Direction = DOWNLEFT;
                    break;
                default:
                    break;
            }
        }

        if(sprite1.Rect.Top + sprite1.Rect.Height >= sprite2.Rect.Top
           and sprite1.Rect.Top <= sprite2.Rect.Top){

            switch(sprite1.Direction){
                case DOWNRIGHT:
                    sprite1.Direction = UPRIGHT;
                    break;
                case DOWNLEFT:
                    sprite1.Direction = UPLEFT;
                    break;
                default:
                    break;
            }
        }

        if(sprite1.Rect.Left + sprite1.Rect.Width >= sprite2.Rect.Left
           and sprite1.Rect.Left <= sprite2.Rect.Left){

            switch(sprite1.Direction){
                case DOWNRIGHT:
                    sprite1.Direction = DOWNLEFT;
                    break;
                case UPRIGHT:
                    sprite1.Direction = UPLEFT;
                    break;
                default:
                    break;
            }
        }

        if(sprite1.Rect.Left <= sprite2.Rect.Left + sprite2.Rect.Width
           and sprite1.Rect.Left + sprite1.Rect.Width >= sprite2.Rect.Left + sprite2.Rect.Width){

            switch(sprite1.Direction){
                case DOWNLEFT:
                    sprite1.Direction = DOWNRIGHT;
                    break;
                case UPLEFT:
                    sprite1.Direction = UPRIGHT;
                    break;
                default:
                    break;
            }
        }
    }
}


If anyone is willing and able to give me some advice in the general direction of what I could be doing better I would be very appreciative!

I prefer to not import a huge library like Box2D, since this is about the extent of the physics that I will be doing in this game.

Thanks so much!

17
Graphics / sf::Rect::Intersects() for two moving rectangles?
« on: October 30, 2011, 02:10:20 pm »
Well, I realized that I was creating my Rect's wrong, and that it doesn't go by Right and Bottom any more but by Width and Height, so I should have been using this code:

Code: [Select]

    Rect = sf::Rect<int>(this->GetPosition().x,
                         this->GetPosition().y,
                         this->GetSize().x,
                         this->GetSize().y);

18
Graphics / sf::Rect::Intersects() for two moving rectangles?
« on: October 30, 2011, 01:54:00 pm »
Any recommendation for how to create a Rect for each of my two sprites that will change appropriately as they move? I currently have it in an Update() function that gets called with every frame. But for some reason Intersects() is returning true for a rectangle like 10 times the size of my actual Rect.

The code I am using to set each Sprite's Rect member every time it updates is this:
Code: [Select]

Rect = sf::Rect<int>(this->GetPosition().x,
                     this->GetPosition().y,
                     this->GetPosition().x + this->GetSize().x,
                     this->GetPosition().y + this->GetSize().y);


And then I am calling:
Code: [Select]

if(sprite1.Rect.Intersects(sprite2.Rect))
        std::cout << "HIT" << std::endl;

19
Graphics / sf::Rect::Intersects() for two moving rectangles?
« on: October 30, 2011, 05:32:23 am »
Is it possible to use sf::Rect::Intersects() for two moving rectangles?

I notice that Intersects() takes a const Rect, so I am assuming that one of the Rects has to be permanently stationary like a platform or something.

I am trying to make collision detection for a laser and a moving asteroid, and I was hoping to use this function, but if this isn't possible I may have to make my own. In that case I would love some guidance (doesn't have to be direct code, just basic advice, though code is fine too... =) ) on creating my own Bounding Box Collision.

Anyway, thank you!!!!
Brock G.

20
General / Simple Collision Detection - BoundingBoxTest
« on: October 30, 2011, 03:54:45 am »
Quote from: "Haikarainen"
EDIT: Actually, I'm very tired and the code i provided is wrong, I didn't take into account A's size in any check. Go figure ;)


Well, I am close. Here is what I have so far. However, it does no detect if the bottom left corner or the top right corner have collided with the other sprite.

Code: [Select]

bool Freighter::Collides(sf::Sprite* sprite)
{
    if(this->GetPosition().x > sprite->GetPosition().x
       and this->GetPosition().x < sprite->GetPosition().x + sprite->GetSize().x
       and this->GetPosition().y > sprite->GetPosition().y
       and this->GetPosition().y < sprite->GetPosition().y + sprite->GetSize().y){
           return true;

    }else if(this->GetPosition().x + this->GetSize().x < sprite->GetPosition().x + sprite->GetSize().x
             and this->GetPosition().x + this->GetSize().x > sprite->GetPosition().x
             and this->GetPosition().y + this->GetSize().y < sprite->GetPosition().y + sprite->GetSize().y
             and this->GetPosition().y + this->GetSize().y > sprite->GetPosition().y){
                 return true;
    }else{
        return false;
    }
}


sf::Rect::Intersects() looks promising, however, I can't figure out how to get my sprites' Rects, and also how to use this function.

Does anyone have experience with this that might be able to enlighten me on a good way to give each of my sprites a Rect, and then use those rects to check for intersecting using
sf::Rect::Intersects()?

21
General / Simple Collision Detection - BoundingBoxTest
« on: October 30, 2011, 02:59:10 am »
Haikarainen,

Thank you for your help, man. You are right, I do want to learn how to do this on my own, and from your advice, it should be very easy. Thank you so much!

And I didn't know SFML had a way to check this. I thought I read somewhere that collision wasn't something they wanted to implement. However, I must've been wrong, and I will now check out the Rect function.

Thanks again, so much!

22
SFML projects / sfMod 1.0 - Play module files in SFML
« on: October 29, 2011, 01:09:45 pm »
Nice man, looking forward to trying this out!

23
SFML projects / Yellow Snake - w/Source (Updated 10/21)
« on: October 29, 2011, 12:59:30 pm »
Quote from: "Nexus"
I remember one of my first games (or even the first?) was a snake clone, too... At the time I used SDL. My code was horrible, I used a single .cpp file, goto statements and a 500 line main() :D


Thanks for the encouragement, Nexus! My approach started out far more condensed and sloppy (not to claim that my current version isn't sloppy). However, with the help if this thread, I've been able to learn some better techniques, so I must give the credit to those who have helped me here.

I am going to be working today to port this game over to SFML 2.0, because I've begun using it since writing this, and I like it a lot!

24
SFML projects / Yellow Snake - w/Source (Updated 10/21)
« on: October 24, 2011, 10:11:20 pm »
Early,

Thank you for that comment! I am sure there is a lot about my code that could be done in more of "the C++ way," but I'm sure I'll continually improve it over time. However, to hear that it was at least coherent for someone else to read is very encouraging! Thanks you so much!

25
SFML projects / Yellow Snake - w/Source (Updated 10/21)
« on: October 22, 2011, 03:05:04 am »
Wow thanks. Those suggestions were definitely needed.

26
SFML projects / Yellow Snake - w/Source (Updated 10/21)
« on: October 21, 2011, 09:56:40 pm »
Pyrius:

I implemented both of the changes you suggested. Restarting and Changing modes no longer restarts the entire program. It just calls a Reset() member function which resets all the settings based on the current mode (or switches modes and resets accordingly).

Thanks for the suggestion!

27
SFML projects / Airport
« on: October 21, 2011, 06:41:06 pm »
Wow, this game is epic. Nice work!

28
SFML projects / Yellow Snake - w/Source (Updated 10/21)
« on: October 21, 2011, 06:21:35 pm »
Pyrius:

Thank you! The game no longer blinks by default. I made it so you can toggle it on by hitting F9, however, this isn't so much a feature that I'll make known any longer as it is a way for me to experiment with tiled_images and animations. I also slowed down the blinking a tad, just in case someone did want to turn it on.

Also, thanks for the advice on how the game restarts, etc... That will take a bit more to implement, so I will look into that today and see what I can come up with.

I appreciate your advice!

29
SFML projects / Yellow Snake - w/Source (Updated 10/21)
« on: October 21, 2011, 05:08:04 pm »
I have really appreciated all the input on my game! I updated the original post with the most current version and the most current source-code.

Any and all criticism and suggestions are welcome!! I have no pride concerning my programming abilities; all I am seeking to do is improve. So criticize away!! =)

Thanks to everyone who has helped me thus far!

Oh, and my snake now animates (or blinks rather), so maybe i should rename it Yellow Disco Snake.

30
SFML projects / Yellow Snake - w/Source (Updated 10/21)
« on: October 21, 2011, 04:36:38 pm »
meissner61: Thank you for the compliment! But I did have some experience in Python before I started learning C++, so I was familiar with classes, functions, containers, variables, etc... C++ is definitely a different animal, but I am finding it to be a blast. And I have actually found some things in C++ come easier to me than they did or still have in Python. I think I am sold on this language for a while, and I'm sold on SFML for sure.

Pyrius: Excellent suggestions, and both of them should be very easy to implement! Thank you so much for playing it and commenting, and I will implement both of those ideas today!

[Edit: Pyrius, I implemented both of your suggestions, which were dead on. Others have agreed wholeheartedly that the game needed to start up in a Paused state. Also, solving the food issue (where you can see it spawning and jumping around if you have a really long snake) was as easy as adjusting the draw order so that the food draws before the body. This way it will always draw underneath the body if it spawns in the same piece of the grid. Again, thanks for those suggestions.]

Pages: 1 [2] 3 4
anything