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

Author Topic: Collision boundingbox  (Read 2784 times)

0 Members and 1 Guest are viewing this topic.

raminlich

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Collision boundingbox
« on: October 15, 2015, 05:55:06 pm »
Hi,
first let say i put a 1024*1024 pic of space and i want to put a little shuttle on it and keep this shuttle on screen (make collision around space) i notice sfml already have bounding box detection but now.........
i dont know why this will work only for left part and up of space object but down and right does not work i mean shuttle will stop on left and up but on right and down the collision will not work why? :-\
this is the code that i use :
FloatRect bound = space_s.getGlobalBounds();
Vector2f point = obj.ship_s.getPosition();
if (bound.contains(point)){
                        obj.playerMove();
                }
                else{
                        obj.playerBack();
                }
 
and player cpp:
Sprite player::playerBack(){

                        ship_s.move(1, 1);
                return ship_s;
       
}
thanks

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Collision boundingbox
« Reply #1 on: October 15, 2015, 06:04:05 pm »
Because point is the top left corner of your sprite.
You could use the bottom right corner of your sprite for right and down collision.

raminlich

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Collision boundingbox
« Reply #2 on: October 15, 2015, 06:09:12 pm »
so...
is there any func that will do this or i must handle it my own?
in overall how can i do it?

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Collision boundingbox
« Reply #3 on: October 15, 2015, 07:02:43 pm »
Add the size of your sprite to the coordinates of its top left corner to get the coordinates of its bottom right corner.

And I guess you should back() in a different direction.
« Last Edit: October 15, 2015, 07:06:28 pm by G. »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Collision boundingbox
« Reply #4 on: October 15, 2015, 07:47:15 pm »
Alternatively you could get the bounds of your obj and see if the two bounding rects intersect at all using sf::Rect<T>::intersects(...) method.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

raminlich

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Collision boundingbox
« Reply #5 on: October 15, 2015, 10:25:20 pm »
Add the size of your sprite to the coordinates of its top left corner to get the coordinates of its bottom right corner.

And I guess you should back() in a different direction.
im sorry but im very inexperienced can you tell with code? :(
thanks
« Last Edit: October 15, 2015, 10:27:00 pm by raminlich »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Collision boundingbox
« Reply #6 on: October 16, 2015, 01:19:51 am »
What G. is saying is that you are currently getting the position of your shuttle (a single point) and then making sure that point is in the bounds of your space. However, your shuttle is not a single point. Your shuttle is a whole rectangle. The single point is the top left corner of the shutle rectangle, but you also need to take into account the bottom right point of the rectangle too. There are a couple of ways to do this. One of them being something like this
...
Vector2f point2 = obj.ship_s.getPosition();
point2.x += shuttleSpriteWidth;
point2.y += shuttleSpriteHeight;

if (bound.contains(point) && bound.contains(point2))
{
   ...
 

Alternatively, like zsbzsb mentioned, you could get the bounds of your shuttle sprite the same way you get the bounds of your space. Then you could just do something like

if (bound.intersects(shuttleBound))
{
   ...
 

However, I don't think this is quite what you want because it would allow your shuttle to only be partly inside of your space. It sounds like you are more interested in keeping the the shuttle completely inside of the space, which is what that first solution would do.

Edit: Also as G. mentioned, your back() function calls move(1,1). If you are moving right then this function is just going to move you more right when you actually probably want to go left.
« Last Edit: October 16, 2015, 01:23:11 am by Arcade »

 

anything