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

Author Topic: [SOLVED] How would I cause collision to the window?  (Read 3058 times)

0 Members and 1 Guest are viewing this topic.

TheBaldPanda

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
[SOLVED] How would I cause collision to the window?
« on: October 02, 2013, 01:51:59 pm »
Me again. I am still attempting my pong clone. I have movement and everything down. It's just that when I move the paddle up, it continues forever, is there a way to, erm, stop that?

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

float ballSpeed = 400.0f;
float paddleSpeed = 10000.0f;
float ballVelX = 10;
float ballVelY = 10;


sf::Clock cloc;
sf::Time tim;


bool isPlaying = true;

sf::Vector2i screenSize(800,600);

int main()
{

    sf::CircleShape pongBall;
    //sf::CircleShape::
    pongBall.setFillColor(sf::Color::Red);
    pongBall.setRadius(25.0f);
    pongBall.setPosition(400, 300);

    sf::RectangleShape paddle1;
    // sf::RectangleShape::
    paddle1.setFillColor(sf::Color::Yellow);
    paddle1.setSize(sf::Vector2f(50, 150));
    paddle1.setPosition(0, 175);

    sf::RectangleShape paddle2;
    // sf::RectangleShape::
    paddle2.setFillColor(sf::Color::Yellow);
    paddle2.setSize(sf::Vector2f(50, 150));
    paddle2.setPosition(750, 175);

    pongBall.setPosition(400, 300);
    paddle1.setPosition(1, 250);
    paddle2.setPosition(784, 250);

    sf::FloatRect boundingBox = paddle1.getGlobalBounds();

    sf::FloatRect boundbox2 = paddle2.getGlobalBounds();
    if(boundingBox.intersects(boundbox2)){

    }

    float t = rand() % 1;
    float w = rand() % 1;



    sf::RenderWindow window(sf::VideoMode(screenSize.x, screenSize.y), "Pro");


    cloc.restart();

    while(window.isOpen()){
        sf::Event event;

    window.pollEvent(event);
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
    // left key is pressed: move our character
    paddle1.move(0, -1);
}
    if (sf::Event::Closed){
        window.close();
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        paddle1.move(0, 1);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    {
        paddle2.move(0, -1);
        if (paddle2.getPosition().x >= window.getPosition().x){



        }
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
        paddle2.move(0, 1);
    }
    window.clear(sf::Color::Red);
    window.draw(paddle1);
    window.draw(paddle2);
    window.display();


}







}

 
« Last Edit: October 02, 2013, 11:48:14 pm by TheBaldPanda »

Tobberoth

  • Guest
Re: How would I cause collision to the window?
« Reply #1 on: October 02, 2013, 01:59:50 pm »
Why not simply check position and compare it to window size?

Something like:
if (pongball.GetPosition().x < 0 || pongball.GetPosition().x > screensize.x)
{
    // Broke window bounds
}

Of course, for the paddle you would do the same thing but use paddle.GetPosition().y and compare it to screensize.y.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How would I cause collision to the window?
« Reply #2 on: October 02, 2013, 02:15:10 pm »
If you don't want your paddle to move out of the window upper bound, you have to prevent your paddle top bound (paddle1.getPosition().y if you didn't change its origin) to move farther than the window top bound (0). Just set the paddle y position to 0 when it gets < 0.
Similarly for the lower bound, you'll want to prevent your paddle lower bound (paddle1.getPosition().y + paddle1.getSize().y) to move lower than the window lower bound (window.getSize().y). So if the the paddle lower bound is > to the window bottom bound, you want to set the paddle lower bound to the window lower bound which translate to setting the paddle y position to the window lower bound minus the paddle height.

TheBaldPanda

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: How would I cause collision to the window?
« Reply #3 on: October 02, 2013, 10:41:50 pm »
Can I have an example please? I have been able to do it for the upward collision, but not the downwards.
Similarly for the lower bound, you'll want to prevent your paddle lower bound (paddle1.getPosition().y + paddle1.getSize().y) to move lower than the window lower bound (window.getSize().y). So if the the paddle lower bound is > to the window bottom bound, you want to set the paddle lower bound to the window lower bound which translate to setting the paddle y position to the window lower bound minus the paddle height.

what exactly do you mean?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: How would I cause collision to the window?
« Reply #4 on: October 02, 2013, 10:53:37 pm »
What he said was already pretty explicit and complete so I guess I'll try writing it in half-C:

if(paddle1.getPosition().y + paddle1.getSize().y > window.getSize().y), then the paddle is too low