示例#1
0
	void update(sf::Time timeChange)
	{
		sf::Vector2f p1Movement(0, 0);
		sf::Vector2f p2Movement(0, 0);


		if (p1MovingUp)
			p1Movement.y -= paddleSpeed;
		if (p1MovingDown)
			p1Movement.y += paddleSpeed;
		if (p2MovingUp)
			p2Movement.y -= paddleSpeed;
		if (p2MovingDown)
			p2Movement.y += paddleSpeed;

		p1Paddle.move(p1Movement * timeChange.asSeconds());
		p2Paddle.move(p2Movement * timeChange.asSeconds());


		// Check collision
		if (ball.getPosition().y < 0 || ball.getPosition().y > 480){
			ballMovement.y *= -1;
		}
		if (ball.getGlobalBounds().intersects(p1Paddle.getGlobalBounds()) || ball.getGlobalBounds().intersects(p2Paddle.getGlobalBounds())){
			ballMovement.x *= -1;
		}



		// Scoring
		if (ball.getPosition().x > 600){
			// P1 scores
			ball.setPosition(300, 240);
			p1Score++;
			p1ScoreText.setString(std::to_string(p1Score));
		}
		else if (ball.getPosition().x < 0){
			// P2 scores
			ball.setPosition(300, 240);
			p2Score++;
			p2ScoreText.setString(std::to_string(p2Score));
		}

		ball.move(ballMovement * timeChange.asSeconds());


	}
bool Knight::collisionSP(sf::CircleShape & player) {
	sf::FloatRect circle1Box = swordCircles[0].getGlobalBounds();
	sf::FloatRect circle2Box = swordCircles[1].getGlobalBounds();
	sf::FloatRect circle3Box = swordCircles[2].getGlobalBounds();
	//if not swinging, we don't want collision to register so return false
	//if sword already intersected, return false to prevent multiple intersections on one swing (light, quick check)
	if (!isSwinging || swordIntersected) {
		return false;
	}
	//full collision check between sword circles and circle of player
	bool ret = false;
	sf::Vector2f playerCenter = sf::Vector2f(player.getGlobalBounds().left + player.getGlobalBounds().width / 2.0f, player.getGlobalBounds().top + player.getGlobalBounds().height / 2.0f);
	// circle 1
	if (circle1Box.intersects(player.getGlobalBounds())) {
		sf::Vector2f circle1Center = sf::Vector2f(circle1Box.left + circle1Box.width / 2.0f, circle1Box.top + circle1Box.height / 2.0f);
		float dx = (playerCenter.x - circle1Center.x) * (playerCenter.x - circle1Center.x);
		float dy = (playerCenter.y - circle1Center.y) * (playerCenter.y - circle1Center.y);
		ret = dx + dy < std::pow(player.getRadius() + swordCircles[0].getRadius(), 2);
		if (ret) {
			swordIntersected = true;
			return true;
		}
	}
	// circle 2
	if (circle2Box.intersects(player.getGlobalBounds())) {
		sf::Vector2f circle2Center = sf::Vector2f(circle2Box.left + circle2Box.width / 2.0f, circle2Box.top + circle2Box.height / 2.0f);
		float dx = (playerCenter.x - circle2Center.x) * (playerCenter.x - circle2Center.x);
		float dy = (playerCenter.y - circle2Center.y) * (playerCenter.y - circle2Center.y);
		ret = dx + dy < std::pow(player.getRadius() + swordCircles[1].getRadius(), 2);
		if (ret) {
			swordIntersected = true;
			return true;
		}
	}
	// circle 3
	if (circle3Box.intersects(player.getGlobalBounds())) {
		sf::Vector2f circle3Center = sf::Vector2f(circle3Box.left + circle3Box.width / 2.0f, circle3Box.top + circle3Box.height / 2.0f);
		float dx = (playerCenter.x - circle3Center.x) * (playerCenter.x - circle3Center.x);
		float dy = (playerCenter.y - circle3Center.y) * (playerCenter.y - circle3Center.y);
		ret = dx + dy < std::pow(player.getRadius() + swordCircles[2].getRadius(), 2);
		if (ret) {
			swordIntersected = true;
			return true;
		}
	}
	return false;
}
bool tr::CColisiometro::Interseccion(sf::RectangleShape rectangle, sf::CircleShape circle, sf::RectangleShape* choque = nullptr)
{	
	sf::FloatRect rect2 = rectangle.getGlobalBounds();
	sf::FloatRect rect1 = circle.getGlobalBounds();
	sf::FloatRect intersection;

	if (rect2.intersects(rect1, intersection))
	{
		if (choque != nullptr)
		{
			//le regreso la zona del choque			
			choque->setPosition(sf::Vector2f(intersection.left, intersection.top));
			choque->setSize(sf::Vector2f(intersection.width,intersection.height));			
		}
		return true;
	}else
	{
		return false;
	}
}