C++ && SFML Collision / Scrolling & questions

AliceLiddell 101 Oct 16, 2012 at 11:05

Hi I made this

(Input: up, down, left and right move the player, F11 full screens F12 windows ESC exits)

I am trying to keep the square from going outside of the window. Is it really bad that I don’t know how to do this if I want to become a professional game developer soon and work being self employed? I’ve programmed for years in high level languages, I just recently started using mid level languages like C++. I really enjoy this it means a lot to me and due to medical problems I can’t hold a job involving leaving my house. I am not sure what practical approach to take to make a basic collision detection system I can build from. I have read on various forums including this one you could make a rectangle the size of the inside window, make it clear and keep the other squares from leaving the inside. I’m not sure though I have tried using views to give the illusion of the rectangle moving, and from there tried making the rectangle move on its own to create a belt scrolling effect. I am mainly trying to figure this out to make my own engine my final question is judging by what you can read in my code am I on the right track to being able to work with other programmers and teams? I think if I can’t understand basic collision without asking for help it might seem like very unprofessional, how can I go about learning any information like this using Google or other resources on the net so I can be quiet about not knowing something and get to work. I’m sorry if this is disorganized or a little hard to read I have horrible grammar, I want to be able to be more productive and worth something to a team and not get burned out so easily. It took me almost a month since I started working with sfml to get this far I’m kind of worried.

5 Replies

Please log in or register to post a reply.

fireside 141 Oct 17, 2012 at 16:31

It’s a little better to just post the code you are having a problem with. Most people don’t like to download someone’s code on their machines for people they don’t know. There should be plenty of resources on the net if you just do a search for rectangular collision c++ or sdl.
Game programming takes time, so just be patient. If you need the money right away, you could be in trouble. If you are creative, there might be a chance to make some money with an iPhone or Android game. Those would probably be your best markets going independently, so you should think about how you should program for them and I’m not sure SDL is the best solution. You could also put a small game on BigFish, or possibly Steam if you wanted to do the PC. BigFish might be the safer bet, because Steam is getting crowded and it’s harder to get approval. You might have to hire an artist if you don’t do that or if you can’t find a team and that could end up being a loss, but the game is going to have to look and play good if you intend to sell it. Also, the more people on your team, the more the sometimes small amount of money you make gets divided. I’m not sure c++ would be that important, but I’m sure I would get arguments from that. Myself, if I were choosing to port to a mobile device, I would find an engine that ported to both Android and Iphone. They do a lot of testing that you wouldn’t be able to do. Even for a PC game, I would still choose an engine for the same reason. Good luck with it. There is a lot of competition out there, but many people are making some money and it is something you can do at home. The only trouble is it can take quite a while to get a game out and it might not do well.

AliceLiddell 101 Oct 18, 2012 at 13:30

Oh I see

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
const int SCREEN_HEIGHT = 600;
const int SCREEN_WIDTH = 480;
const int SCREEN_BPP = 32;
    int main()
    sf::RenderWindow App(sf::VideoMode(SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_BPP), "Engine");
    sf::Image BackgroundImage;
    if (!BackgroundImage.LoadFromFile("background.jpg"))
    return EXIT_FAILURE;
    sf::Sprite Background(BackgroundImage);
    sf::Image Image;
    if (!Image.LoadFromFile("Player.png"))
    return EXIT_FAILURE;
    sf::Sprite Sprite(Image);
    sf::Vector2f StartPos(100, 200);
    sf::Vector2f Center(1000, 1000);
    sf::Vector2f HalfSize(400, 300);
    sf::View View(Center, HalfSize);
    Sprite.SetCenter(32, 32);
    while (App.IsOpened())
    sf::Event Event;
    while (App.GetEvent(Event))
    if (Event.Type == sf::Event::Closed)
    if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
    if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F11))
    App.Create(sf::VideoMode(800, 600, 32), "SFML Window", sf::Style::Fullscreen);
    if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F12))
    App.Create(sf::VideoMode(SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_BPP), "SFML Window");
    float ElapsedTime = App.GetFrameTime();
    float Offset = 200.f * App.GetFrameTime();
    if (App.GetInput().IsKeyDown(sf::Key::Left)) Sprite.Move(-100 * ElapsedTime, 0);
    if (App.GetInput().IsKeyDown(sf::Key::Right)) Sprite.Move( 100 * ElapsedTime, 0);
    if (App.GetInput().IsKeyDown(sf::Key::Up))    Sprite.Move(0, -100 * ElapsedTime);
    if (App.GetInput().IsKeyDown(sf::Key::Down))  Sprite.Move(0,  100 * ElapsedTime);
    App.Clear(sf::Color(207, 194, 149));
    return EXIT_SUCCESS;

here is the code its a good thing to note I am a beginner some research I’ve done and your reply has lead me to believe that I should really concentrate on some more OOP concepts and that I can’t spend all day researching. I really thought I would be done after almost a year and a half of studying C++ but I guess it’s a language that will be good to know for the future.

fireside 141 Oct 19, 2012 at 18:12

I don’t really see any collision code there. You are only showing two things, a background and a sprite. The sprite can only collide with the edges of the background, so you would get those positions and then test your player’s rectangle’s edges against them. I’m really not familiar with SDL, so I don’t know if they have a way to get them. Since it’s only your background, the edge should be at 0 for the left x value, and the width of the background for the right x value. Since this is the background, you could just make the dimensions of it smaller and then stop the player when it reaches the edge of that smaller rectangle. Say the player is 64 pixels wide. Then the edge that it needs to stop on the background is background.width - 32 when he is traveling in the right direction. When traveling left, he needs to stop at 32, so he still appears to be completely on the background. You have to put those conditions in your code after each input line.
Here’s your line for left:

if (App.GetInput().IsKeyDown(sf::Key::Left)) Sprite.Move(-100 * ElapsedTime, 0);
There is nothing to stop it, so you have to add a condition.
if(Sprite.GetPosition() < 32) Sprite.SetPosition(32);
That’s the best I can do here, it would be best if you could find a tutorial on collision detection.

AliceLiddell 101 Oct 20, 2012 at 12:57

Thanks to your wonderful Idea of looking on Google I found a library of collision detection functions.


I think now I can move on to doing what your saying, but I want to organize those conditions better and fully take advantage of OOP and all of the benefits, realizing how powerful a simple line of code is and how powerful it can be to keep syntax very clean I think I should add a ObjectManager to the game. I want to make my objects right the first time and I might write out tests and organize my code on a design document. I am not sure though I did notice there are multiple methods and it can look really messy in places like an event handler, can I make a event handler in a class file and then use it to contain conditions and then call the library in my main program? Is that over complicating things, I really am learning a lot at once and the only reason I think I get bogged down is because I want to organize my code and keep making it evolve slowly.

fireside 141 Oct 20, 2012 at 21:31

I’m all for object oriented code, but generally I do procedural code first to gain an understanding and later arrange it into an object. I think the important thing is to write a lot of code and get experience debugging, coordinating, etc. In a small program like this, it’s really not necessary to go beyond functions in most instances, and that’s the best place to start breaking up your code into organized chunks. As your program grows, then you can arrange functions and data into classes. If you are dealing with a more strict OO language like C# or Java, then you really do have to think in terms of objects, but it’s not always convenient without some experience. You could easily just use someone else’s collision library, but I think it’s better to get a basic understanding by writing your own simple rectangular collision library, or at least a function that checks if two rectangles have collided. The same with simple physics. Not anything like a complex library, but how do you write code to make something fall reasonably realistically and attach it to your player’s jump? Those are good problems to work out prior to moving on to complex libraries that do everything for you. It doesn’t have to be particularly neat, either. That’s for later when they get very large. The experience of solving the problem with code is more important for learning. BTW, I love Alice in Wonderland and even wrote a short adventure game about it. It was kind of the beginning of the fantasy genre.