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

Author Topic: Getting a rect from a shape/implementing collision detection  (Read 2964 times)

0 Members and 1 Guest are viewing this topic.

aIck

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
aIck here!
I finally felt ballsy enough to try and program something, and that something would be yet another Pong clone (start small they all say). I was pleasantly surprised that I was able to draw those sweet white rectangles on the screen and making them moveable with keyboard was rather easy, but now I'm struggling on collisions and such. I need to stop those paddles from escaping the screen, and later make the ball bounce off the paddles and walls.

Here's the code for a paddle:
    sf::RectangleShape paddle1;
    paddle1.setSize(sf::Vector2f(20, 100));
    paddle1.setFillColor(Color::White);
    paddle1.setPosition(20, 150);
    paddle1.getGlobalBounds();

I figured getGlobalBounds() could be the way to go, but I don't quite know how to advance from here. Any help? Suggestions? Corrections?

I'm using SFML2 if that matters.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Getting a rect from a shape/implementing collision detection
« Reply #1 on: May 14, 2013, 10:40:30 am »
getGlobalBounds returns an sf::Rect which has the nice functions contains() and intersects(), with which you can either test against one point or another rect. From there you can do some easy collision detection. If you notice that the paddle are outside the window, you reset them to be within the window and thus prevent them for leaving.
There are many many possibilities and since you're new to all of this, you should research a bit on the topic and try different things, just to get an understanding of it.

Besides that, if you're ever stuck with Pong, you can always take a look at SFML's Pong example. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

aIck

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Getting a rect from a shape/implementing collision detection
« Reply #2 on: May 14, 2013, 11:31:15 am »
Thanks mate, that example helped quite a lot. No more escaping paddles.

I'm still confused about Rect's though, like how can I actually use it after using getGlobalBounds(). I've tried stuff like if paddle1.x or paddle1.top (at least 'top' was an attribute on a Rect) has a value that puts them out of the screen, it will be adjusted so the paddle can't exit the screen. This hasn't been working at all, as I get compile error:
error: 'class sf::RectangleShape' has no member named 'top'
Obviously it's still using Shape, so how am I able to use Rect instead?

(Apologies for bad text, I felt nauseous after reading it too.)

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Getting a rect from a shape/implementing collision detection
« Reply #3 on: May 14, 2013, 11:39:43 am »
top, left, width and height are 4 attributes of sf::Rect, which is contained by sf::RectangleShape and can be accessed from it with getGlobalBounds().

I guess you want to check the position of this shape by playing with intersects() of sf::Rect, but you will want to rectify the position of sf::RectangleShape

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Getting a rect from a shape/implementing collision detection
« Reply #4 on: May 14, 2013, 12:18:02 pm »
sf::RectangleShape and sf::Rect<T> are two completly different classes.

You should definitely read the official tutorials on SFML and questions regarding member functions/variables can be easily looked up in the official documentation. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything