3 Replies - 1470 Views - Last Post: 15 March 2013 - 02:15 PM Rate Topic: -----

#1 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 125
  • Joined: 24-January 12

SFML collision problem

Posted 14 March 2013 - 06:57 PM

Made a PONG game following a tutorial. Apparently others who followed this tutorial had the same issue.
The ball goes all jittery if it hits the very top or bottom of the paddles. Other than that everything works fine.
Any ideas of what I can do to eliminate the jitters or is there something in the library that I am using wrong?

Code to create the shapes:
	// Shapes
	sf::RectangleShape paddle1;
	sf::RectangleShape paddle2;
	sf::RectangleShape ball;

	paddle1.setSize(sf::Vector2f(50,100));
	paddle1.setPosition(20,250);
	paddle1.setTexture(&texPaddle);

	paddle2.setSize(sf::Vector2f(50,100));
	paddle2.setPosition(730,250);
	paddle2.setTexture(&texPaddle);

	ball.setSize(sf::Vector2f(50,50));
	ball.setPosition(400,300);
	ball.setTexture(&texBall);


Collision Code:
		//Collision
		if(ball.getGlobalBounds().intersects(paddle1.getGlobalBounds()))
		{
			xVelBall *= -1;
			boink.play();
		}
		if(ball.getGlobalBounds().intersects(paddle2.getGlobalBounds())) 
		{
			xVelBall *= -1;
			boink.play();
		}



Is This A Good Question/Topic? 0
  • +

Replies To: SFML collision problem

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SFML collision problem

Posted 15 March 2013 - 07:20 AM

The most likely reason why your jitter is happening is because the collision detection code is repeatedly being evaluated. When the condition is true, something is supposed to happen like the ball bouncing in the opposite direction thus breaking reevaluation of the collision.

This post has been edited by ButchDean: 15 March 2013 - 07:20 AM

Was This Post Helpful? 1
  • +
  • -

#3 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 125
  • Joined: 24-January 12

Re: SFML collision problem

Posted 15 March 2013 - 11:25 AM

ahh, thanks for the hint, if i have this figured out, when the ball hits the top of the paddle the x *= -1 causes it to rebound back but the y axis is still going in the same direction so it causes it to jitter down the face of the paddle because the ball is effectively 'inside' the paddle and vice versa from the bottom. i will have to add some sanity checks to see if its hitting the top or bottom of the paddle and use that to change the y as well, either that or add a 1 pixel wide rectangle to the face of the paddle and use that for my collision detect.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SFML collision problem

Posted 15 March 2013 - 02:15 PM

Something like that. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1