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

Author Topic: Help with collision detection and sprite management  (Read 1863 times)

0 Members and 1 Guest are viewing this topic.

Dan581

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Help with collision detection and sprite management
« on: April 23, 2013, 07:20:02 pm »
Firstly, I'm a noob, now that that's out of the way, I'm confident I can make what I'm trying to make which is an overhead rpg type game similar to Final Fantasy 3, the problem is that the code will be ridiculously inefficient.

My first question is how do I make multiple instances of the missile in the most efficient manner possible? Secondly, as you know if you've played an overhead rpg like FF3, the sprite changes when moving in different directions, what is the most efficient way of doing this in your opinion? Please use this code as an example, pretending Sieg is the main character. Lastly, what's the best way to do collision detection? What I did in this piece of crap game works for it but I imagine it would get very tedious to do with a sprite in an open world with multiple objects like trees and such around.
#include <SFML/Graphics.hpp>
#include <windows.h>
#include <SFML/Audio.hpp>
#include <iostream>
int main()
{
    // Load the music from an OggVorbis file
    sf::Music Zepplin;
    if (!Zepplin.OpenFromFile("music.ogg"))
        return EXIT_FAILURE;

    // Play it
    Zepplin.Play();


    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 30), "Flibbidy");



    // Load Background /////////////////////////////////////////
    sf:: Image BackgroundFile;
    if (!BackgroundFile.LoadFromFile("background.png"))
        return EXIT_FAILURE;

    sf::Sprite Background(BackgroundFile);

       // Load the sprite image from a file
    sf::Image SiegFile;
    if (!SiegFile.LoadFromFile("floob.png"))
        return EXIT_FAILURE;

    // Create the sprite
    sf::Sprite Sieg(SiegFile);


    // Change its properties
    Sieg.SetColor(sf::Color(0, 255, 255));
    Sieg.SetPosition(200.f, 100.f);
    Sieg.SetScale(2.f, 2.f);
    Sieg.SetCenter(26, 18);

    sf::Image MissileFile;
    if(!MissileFile.LoadFromFile("missile.png"))
        return EXIT_FAILURE;

    sf::Sprite Missile(MissileFile);
    App.Draw(Missile);
    Missile.SetPosition(500, 600);
    Missile.SetCenter(53, 37);

    // Start game loop
    while (App.IsOpened())
    {

        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Get elapsed time
        float ElapsedTime = App.GetFrameTime();
        int Random = sf::Randomizer::Random(0, 600);

        // Move the sprite
        if (App.GetInput().IsKeyDown(sf::Key::Left))  Sieg.Move(-100 * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(sf::Key::Right)) Sieg.Move( 100 * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(sf::Key::Up))    Sieg.Move(0, -100 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Down))  Sieg.Move(0,  100 * ElapsedTime);

        // Rotate the sprite
        if (App.GetInput().IsKeyDown(sf::Key::Add))      Sieg.Rotate(- 100 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Subtract)) Sieg.Rotate(+ 100 * ElapsedTime); // Clear screen

        App.Clear();
       int PosX = Sieg.GetPosition().x;
       int PosY = Sieg.GetPosition().y;
       int MissilePosX = Missile.GetPosition().x;
       int MissilePosY = Missile.GetPosition().y;
       Missile.Move(-500 * ElapsedTime, 0);
       if (PosX - MissilePosX < 10)

       if ((MissilePosX - PosX) < 20 && (PosY - MissilePosY) < 20 && (MissilePosY - PosY) < 20)
       {
       Sieg.FlipX(true);
       }
       else
       {
       Sieg.FlipX(false);
       }
        // Display spritein our window
        App.Draw(Background);
        App.Draw(Sieg);
        App.Draw(Missile);

            if (MissilePosX < 50)
        {
        App.Draw(Missile);

        Missile.SetPosition(800, Random);

        }
        // Display window contents on screen
        App.Display();
    }
return EXIT_SUCCESS;
}
// I have no idea how to use this class, I copied and pasted it from a tutorial on this //site and messed around with it to the best of my ability to get it to not interfere with //the debugging/compiling of the program, I have no idea how it ties in with sprite //management though and can't get it to do anything productive.
class Missile2
{
public :
// What is this "const::string& Missileflub" what purpose does it serve?
    static bool Init(const std::string& Missileflub)
    {
        return MissileFile2.LoadFromFile("missile.png");
    }

    Missile2()
    {
        MissileCopy.SetImage(MissileFile2); // every sprite uses the same unique image
    }

private :

    static sf::Image MissileFile2; // shared by every instance

    sf::Sprite MissileCopy; // one per instance
};
[/cpp]
« Last Edit: April 23, 2013, 08:53:21 pm by Laurent »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Help with collision detection and sprite management
« Reply #1 on: April 23, 2013, 08:23:40 pm »
Quote from: Dan581
My first question is how do I make multiple instances of the missile in the most efficient manner possible?
Use STL containers like std::vector.

Quote from: Dan581
Secondly, as you know if you've played an overhead rpg like FF3, the sprite changes when moving in different directions, what is the most efficient way of doing this in your opinion?
Have different images in your texture, and change the sprite's texture rect depending on the direction.

Quote from: Dan581
Lastly, what's the best way to do collision detection?
You can start with rectangle or circle collision. The first one can be implemented using sf::FloatRect::intersects(), the second one by measuring the distance between objects and comparing it to their collision radius.

Please use the code tags (left drop-down field). And use SFML 2, version 1.6 is terribly outdated.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: