#1 Members - Reputation: 100
Posted 10 February 2012 - 07:26 PM
would it be to find the corners of the sprite and setup an if statement or does sfml offer another way to do this?
I've been stuck on this for awhile now so thanks for your help.
#2 Members - Reputation: 121
Posted 10 February 2012 - 10:43 PM
#3 Members - Reputation: 1216
Posted 11 February 2012 - 01:27 AM
#4 Members - Reputation: 100
Posted 11 February 2012 - 09:17 AM
In my Global.h
sf::IntRect ppaddleRect(
ppaddle.GetPosition().x,
ppaddle.GetPosition().y,
ppaddle.GetSize().x,
ppaddle.GetSize().y);
sf::IntRect pballRect(
pball.GetPosition().x,
pball.GetPosition().y,
pball.GetSize().x,
pball.GetSize().y);
In my Update Function
if(pballRect.Intersects(ppaddleRect))
{
Xballspeed = -Xballspeed;
}
However, it seems that the two are always intersecting, so the ball now moves up and down. Does anyone know why. (when I comment out the if statement above the ball moves fine, but obviously doesn't bounce off the paddle either)
#5 Members - Reputation: 1216
Posted 11 February 2012 - 09:31 AM
Not that I recommend using globals.
#6 Members - Reputation: 241
Posted 11 February 2012 - 09:52 AM
SoulHeart, on 11 February 2012 - 09:17 AM, said:
In my Global.h
sf::IntRect ppaddleRect( ppaddle.GetPosition().x, ppaddle.GetPosition().y, ppaddle.GetSize().x, ppaddle.GetSize().y); sf::IntRect pballRect( pball.GetPosition().x, pball.GetPosition().y, pball.GetSize().x, pball.GetSize().y);
In my Update Function
if(pballRect.Intersects(ppaddleRect))
{
Xballspeed = -Xballspeed;
}
However, it seems that the two are always intersecting, so the ball now moves up and down. Does anyone know why. (when I comment out the if statement above the ball moves fine, but obviously doesn't bounce off the paddle either)
Are you updating the Rectangle's location every frame? Go ahead and print out the X, Y, width and height of the rectangles when they intersect, and you should see what's happening. All an intersection is a check like this (checking Rect1 and Rect2 intersect or not):
if (Rect1.X <= Rect2.X + Rect2.Width && Rect1.X + Rect1.Width >= Rect2.X && Rect1.Y <= Rect2.Y + Rect2.Height && Rect1.Y + Rect1.Height >= Rect2.Y)
BTW, I've started using a 2d physics library (chipmunk-physica, see my old blog link in my sig). it's simplifies things so much for me. No longer do I have to worry about collision detection, collision response, object movement, etc. the 2d physics library handles all that for me.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#7 Members - Reputation: 100
Posted 14 February 2012 - 03:06 PM
if (pball.GetPosition().x <= ppaddle.GetPosition().x + ppaddle.GetSize().x &&
pball.GetPosition().x + pball.GetSize().x >= ppaddle.GetPosition().x &&
pball.GetPosition().y <= ppaddle.GetPosition().y + ppaddle.GetSize().y &&
pball.GetPosition().y + pball.GetSize().y >= ppaddle.GetPosition().y)
{
Xballspeed = -Xballspeed;
}
sometimes the collision happens way to the right of the paddle, other times the collisions happens when the ball is coming from behind the paddle(the ball bounces off all of the walls for now) and other times the ball completely ignores the paddle. I must be missing something vital in my code.
#8 Members - Reputation: 241
Posted 14 February 2012 - 04:11 PM
SoulHeart, on 14 February 2012 - 03:06 PM, said:
if (pball.GetPosition().x <= ppaddle.GetPosition().x + ppaddle.GetSize().x &&
pball.GetPosition().x + pball.GetSize().x >= ppaddle.GetPosition().x &&
pball.GetPosition().y <= ppaddle.GetPosition().y + ppaddle.GetSize().y &&
pball.GetPosition().y + pball.GetSize().y >= ppaddle.GetPosition().y)
{
Xballspeed = -Xballspeed;
}
sometimes the collision happens way to the right of the paddle, other times the collisions happens when the ball is coming from behind the paddle(the ball bounces off all of the walls for now) and other times the ball completely ignores the paddle. I must be missing something vital in my code.Lets just look at the case where it happens way to the right of the paddle.
The only way a collision is possible is
if (pball.GetPosition().x <= ppaddle.GetPosition().x + ppaddle.GetSize().x)
is true
How can that be true, if the ball is way to the right? That line says, "The left side of the ball is to the left or equal to the right side of the paddle"
So, something is wrong in your code. Is GetSize() returning the right value? Are you drawing the objects based on the GetPosition() values? Print out the values when it collides, and check what it's telling you about the position and size of the objects.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#9 Members - Reputation: 100
Posted 20 February 2012 - 12:14 PM
#10 Members - Reputation: 241
Posted 20 February 2012 - 04:41 PM
SoulHeart, on 20 February 2012 - 12:14 PM, said:
Well, if getSize() returns the sprite before rotation, you have 2 options:
#1 (easiest) draw the paddle upright, not on it's side or,
#2, create your own GetSize(), which takes into account the current rotation of the sprite.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#11 Senior Moderators - Reputation: 2448
Posted 20 February 2012 - 05:06 PM
See herefor more information
ScapeCode - Blog | SlimDX
#12 Members - Reputation: 100
Posted 22 February 2012 - 06:30 PM
if (pball.GetPosition().x <= ppaddle.GetPosition().x + ppaddle.GetSize().x &&
pball.GetPosition().x + pball.GetSize().x >= ppaddle.GetPosition().x &&
pball.GetPosition().y <= ppaddle.GetPosition().y + ppaddle.GetSize().y &&
pball.GetPosition().y + pball.GetSize().y >= ppaddle.GetPosition().y)
but i can't get it to work when i rotate the sprite.(which I need it rotated)The sfml collision tests don't make sense to me and there is hardly any tutorials out there. I need someone to put it in black and white for me.(I'm a NOOB still lol)
Thanks guys
#13 Members - Reputation: 1216
Posted 22 February 2012 - 06:34 PM
SoulHeart, on 22 February 2012 - 06:30 PM, said:
if (pball.GetPosition().x <= ppaddle.GetPosition().x + ppaddle.GetSize().x && pball.GetPosition().x + pball.GetSize().x >= ppaddle.GetPosition().x && pball.GetPosition().y <= ppaddle.GetPosition().y + ppaddle.GetSize().y && pball.GetPosition().y + pball.GetSize().y >= ppaddle.GetPosition().y)but i can't get it to work when i rotate the sprite.(which I need it rotated)
#14 Members - Reputation: 241
Posted 23 February 2012 - 04:27 PM
SoulHeart, on 22 February 2012 - 06:30 PM, said:
if (pball.GetPosition().x <= ppaddle.GetPosition().x + ppaddle.GetSize().x && pball.GetPosition().x + pball.GetSize().x >= ppaddle.GetPosition().x && pball.GetPosition().y <= ppaddle.GetPosition().y + ppaddle.GetSize().y && pball.GetPosition().y + pball.GetSize().y >= ppaddle.GetPosition().y)but i can't get it to work when i rotate the sprite.(which I need it rotated)
The sfml collision tests don't make sense to me and there is hardly any tutorials out there. I need someone to put it in black and white for me.(I'm a NOOB still lol)
Thanks guys
Are you still talking about just the paddles being rotated? If you're paddles just stay up-right all the time (like classic PONG), then you should take your source image, and rotate it, and save it up-right, instead of rotating it after you load it. As I already suggested.
If you have a special pong where your paddle circles around the edge of the screen (or around a corner), then you're going to have to do a little bit of work, and it won't be beginner-easy work either.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


















