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

Author Topic: SFML 2.0 Collision detection  (Read 66812 times)

0 Members and 1 Guest are viewing this topic.

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
SFML 2.0 Collision detection
« on: August 27, 2011, 01:06:56 pm »
Hey guys, is there any working collision_detection for SFML 2.0 that you know of? I tried finding it but I can only find some for 1.6 version which aint working on 2.0 unfortunately.

Best regards

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
SFML 2.0 Collision detection
« Reply #1 on: August 27, 2011, 01:55:03 pm »
What type of collision detection do you need exactly? There are some source codes on the Wiki, and maybe you'll find more when searching for old forum threads.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
SFML 2.0 Collision detection
« Reply #2 on: August 27, 2011, 02:41:44 pm »
First of all thank you for the fast reply Nexus!

I am looking for a collision that works on SFML version 2.0.
I tried the one on http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection%5B but I think it is for a older version since it is giving me a long list of errors.

Probably you dont know the game, but it would be the best example to explain what I am trying to archive. I have a INT gravity which pushes my character down.

Now I want him to be able to walk left, right and jump (already done that).

Now I want to add in game stuff like platforms and the ground which the character can walk on. What I want to archive is the following:

1. Character can walk on the floor (it is one large image (width)).
2. Character can jump on platforms but also fall off.
3. Character can walk into platforms or building, if he does so the movement should stop (i.e. left or right walk or jumping or going up or down on a ladder).

The whole game is almost build out of blocks like in Maple story. So if I can get some 'bounding boxes' for example to check collusion on that would work fine I think.

A sample photo so you know what I am talking about i.e. world objects.

http://www.bscarillon.nl/webpaginas/Max/maplestory2.jpg

As you can see characters can climb up on the rope, walk on the platforms and fall off and stuff like that. Basicly all objects are build out of blocks (PNG pictures).

Yours sincerely.

GZ

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
SFML 2.0 Collision detection
« Reply #3 on: August 27, 2011, 04:52:23 pm »
You can check if the rectangles of two sprites are colliding.

Use :

sf::Rect<T>
sf::Rect< T >::Intersects(const Rect< T > & rectangle   )

Note : there are many ways to do a collision detection, there are a lot of threads talking about it on this forum

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
SFML 2.0 Collision detection
« Reply #4 on: August 27, 2011, 05:14:48 pm »
your seriously? thats all the code you need? :P
I was already trying something like (mostly text but u get the point)

Code: [Select]
   // declare an object in the world (floor)
    sf::Image floortile;
    floortile.LoadFromFile("world_objects/floor.png");

    sf::Texture floortileTexture;
    floortileTexture.LoadFromImage(floortile);

    sf::Sprite floortileSprite(floortileTexture);
    floortileSprite.SetPosition(70, 530);

    // get the objects width and height
    sf::Vector2f floorTileSize = floortileSprite.GetSize();
    int floorTileX = floorTileSize.x;
    int floorTileY = floorTileSize.y;

    /*  we now know the following:
        The object is 359px width and 81px high.
        It is located at 70x530 pixels pixels from the left top of the screen
        Our character is located at (for instance) 80x352 from the left top of the screen
        this means collision would occure between

        pixel 70 untill pixel 429 in width and...
        pixel 530 untill pixel 611 in height.

        If the current position of the sprite is the same or within the two lines of pixels
        set above, collision is occuring and walking speed should be set to 0.

    */


I looked at the link but no clue how to get it working with my stuff...

the floor:
Code: [Select]
sf::Image floortile;
    floortile.LoadFromFile("world_objects/floor.png");

    sf::Texture floortileTexture;
    floortileTexture.LoadFromImage(floortile);

    sf::Sprite floortileSprite(floortileTexture);
    floortileSprite.SetPosition(70, 530);


My character:

Code: [Select]
   /////////////////////////////////// SPRITE 1 FRAME 1 //////////////////////
    sf::Image character1;
    character1.LoadFromFile("character_sprites/character_1.png");

    sf::Texture charTexture1;
    charTexture1.LoadFromImage(character1);

    sf::Sprite charSprite1(charTexture1);
    charSprite1.SetPosition(80, 352); // do +100

    /////////////////////////////////// SPRITE 1 FRAME 2 //////////////////////
    sf::Image character2;
    character2.LoadFromFile("character_sprites/character_2.png");

    sf::Texture charTexture2;
    charTexture2.LoadFromImage(character2);

    /////////////////////////////////// SPRITE 1 FRAME 3 //////////////////////
    sf::Image character3;
    character3.LoadFromFile("character_sprites/character_3.png");

    sf::Texture charTexture3;
    charTexture3.LoadFromImage(character3);

    /////////////////////////////////// SPRITE 1 FRAME 4 //////////////////////
    sf::Image character4;
    character4.LoadFromFile("character_sprites/character_4.png");

    sf::Texture charTexture4;
    charTexture4.LoadFromImage(character4);

    /////////////////////////////////// SPRITE 1 FRAME 5 //////////////////////
    sf::Image character5;
    character5.LoadFromFile("character_sprites/character_5.png");

    sf::Texture charTexture5;
    charTexture5.LoadFromImage(character5);



But someone told me to do this:

Code: [Select]
if(r1.x() <= r2.x()+r2.width() && r1.x()+r1.width() >= r2.x() && r1.y() <= r2.y()+r2.height() && r1.y()+r1.height() >= r2.y())
    {
         // Collision!
    }
    else
    {
        // nothing...
    }


but I have no clue how to make a function like above...

r1.x gives the coordinates and width and height give the width and hight (doh)

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
SFML 2.0 Collision detection
« Reply #5 on: August 27, 2011, 08:32:21 pm »
You have sf::Rect<T>::Contains(...) that check if a sf::Rect is inside another one
and sf::Rect<T>::Contains(...) that check if a point is inside a sf::Rect

So, you don't need to recode these functions :D

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
SFML 2.0 Collision detection
« Reply #6 on: August 28, 2011, 11:03:03 am »
Ahhh oke I see that is awesome :)

could seomeone please do a sample code cause I am having a hard time implementing it on my character sprite and floortile sprite :(

Kiecker

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: SFML 2.0 Collision detection
« Reply #7 on: May 06, 2016, 08:09:35 pm »
in sfml there is a function that automatically checks collision in sfml

model is an example sf::RectangleShape

model.getGlobalBounds().intersects(rect.getGlobalBounds())

this code will return a bool value

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
AW: SFML 2.0 Collision detection
« Reply #8 on: May 06, 2016, 09:18:55 pm »
Please don't revive 5 years old threads.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

epicxd

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: AW: SFML 2.0 Collision detection
« Reply #9 on: November 29, 2017, 09:55:13 pm »
Please don't revive 5 years old threads.

For what it's worth his reply helped me a lot. Even though it's old doesn't mean it wasn't helpful  :)

 

anything