Rectangle collision (SFML 2.0)

Perhaps somebody saw my post about how I set up SFML 2.0 for cross-compilation. I have one more thing to share. It is in no way necessarily connected to SFML 2.0, but that was what I used here, so modifications would have to be made for other frameworks (or other languages than C++).

What I have made is a very simple demo where a “character” with a rectangular hitbox may be moved with the arrow keys and unable to go through other solid objects, also with rectangular hitboxes – walls, simply. Topdown view.

Here is the source code:
http://pastebin.com/gHvAQ1as

To note:

  • The arrow keys are used to steer.
  • The character’s sprite is 24×24 and the obstacle sprites are 64×64. I made two quick and ugly sprites.
    thing obstacle
  • The resourcePath() thing is an OS X only thing needed before the resource paths. It comes with the OS X OpenGL demo for SFML 2.0.
  • The obstacles are stored as sf::Vector2f‘s (coördinates) in an std::vector (container). Different types of vectors, thus. The same sprite handle is used to draw all of them.

This is the only thing I’ve done for LD that may be considered a “warmup” so far. I did it during a one-hour train ride when I had nothing better to do. Perhaps somebody will find this useful during the competition.

Tags: collision detection rectangle example sfml