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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - lilbigwill99

Pages: [1]
1
General / Re: Problem with click and drag
« on: December 21, 2016, 02:35:31 am »
Thank you! I new it was a simple fix I just couldn't think of where I went wrong. What do you mean by world space and screen space?

2
General / Problem with click and drag
« on: December 20, 2016, 06:03:15 pm »
Hi, I am simply trying to click and drag a simple square. The following code compiles, but doesn't seem to work at all. The problem code:


if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {

            if (square.getGlobalBounds().contains(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y)) {
                std::cout << "you good dude" << std::endl;
                square.setPosition(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y);
            }
        }


 

cout doesn't output anything to the console, so i know that it has to be an issue with the first if statement, but I do not know any alternatives. Thank you for your time!

3
General / Re: Ball in Pong only moves in 4 directions
« on: November 24, 2016, 08:33:48 am »

Does this describe what you meant by "trip out"?



Haha thank you I don't know why I didn't figure that one out on my own

4
General / Re: Ball in Pong only moves in 4 directions
« on: November 23, 2016, 01:29:01 am »
Thank you everyone for your replies, I'm glad you guys are cool on this website haha. 8)

5
General / Re: Ball in Pong only moves in 4 directions
« on: November 22, 2016, 02:57:22 am »
I can't provide any code, but the basic idea is to have a direction vector and when you collide, you can simply change the x & y direction values. If you want to, yoi can calculate the angle between the direction and the surface and calculate the values so you get the same output angle. For axis aligned lines, you just need to either negate the x or the y value depending how the line is oriented.

Thank you for your time and answer, but I don't think I quite understand lol. How exactly would I go about getting the angle at which I can use for an output angle? I'm pretty sure you said I should store my x and y directions in a vector right? I'm not sure how that helps me at the moment seeing as how I am not keeping track of the balls location (the only time I've used vectors was for keeping track of previous events, im only a month into c++)

6
General / Ball in Pong only moves in 4 directions
« on: November 22, 2016, 12:08:56 am »
I was wondering how you would go about trying to change the direction of the ball when a collision happens a little relative to the angle that the ball hit at, rather than what I have been doing and just changing the direction of the x or y axis. Could someone show this in code, and why they did what they did and why what I am doing is wrong? Also, if someone wants bonus points, could you tell me why the ball trips out when it hits the top or bottom of a paddle?


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

using std::cout;
using std::endl;

int main()
{

    srand(time(0));

    const int WINDOW_WIDTH = 720;
    const int WINDOW_HEIGHT = 480;

    sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "PONG BOIII");
    window.setFramerateLimit(60);
    sf::Event event;

    ///////paddles///////

    const int PADDLE_WIDTH = 25;
    const int PADDLE_HEIGHT = 100;

    sf::RectangleShape paddle1(sf::Vector2f(PADDLE_WIDTH, PADDLE_HEIGHT));
    paddle1.setOrigin(PADDLE_WIDTH / 2, PADDLE_HEIGHT / 2);
    paddle1.setPosition(50, WINDOW_HEIGHT / 2);

    sf::RectangleShape paddle2(sf::Vector2f(PADDLE_WIDTH, PADDLE_HEIGHT));
    paddle2.setOrigin(PADDLE_WIDTH / 2, PADDLE_HEIGHT / 2);
    paddle2.setPosition(WINDOW_WIDTH - 50, WINDOW_HEIGHT / 2);
    float AIspeedDown = 0.5;
    float AIspeedUP = -0.5;

    /////the ball////////

    const int BALL_RADIUS = 10;
    float bX = 0.5;
    float bY = 0.5;

    sf::CircleShape theBall(BALL_RADIUS);
    theBall.setPointCount(300);
    theBall.setOrigin(BALL_RADIUS / 2, BALL_RADIUS / 2);
    theBall.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
    theBall.setFillColor(sf::Color::Cyan);

    sf::Clock clock;
    sf::Time time;

    while (window.isOpen()) {

        while (window.pollEvent(event)) {

            if (event.type == sf::Event::Closed) {
                window.close();
            }

        } //ends events

        //moves paddle
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
            paddle1.move(0, -0.5 * time.asMilliseconds());
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
            paddle1.move(0, 0.5 * time.asMilliseconds());
        }

        //checking window collision
        if (paddle1.getPosition().y - paddle1.getOrigin().y <= 0) {
            paddle1.move(0, 0.5 * time.asMilliseconds());
        }

        if (paddle1.getPosition().y + paddle1.getOrigin().y >= WINDOW_HEIGHT) {
            paddle1.move(0, -0.5 * time.asMilliseconds());
        }

        //ball stuff
        theBall.move(bX * time.asMilliseconds(), bY * time.asMilliseconds());

        if (theBall.getPosition().y <= 0 || theBall.getPosition().y >= WINDOW_HEIGHT) {
            bY *= -1;
        }

        if (theBall.getPosition().x >= WINDOW_WIDTH) {
            theBall.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
            bX = 0.5;
        }

        if (theBall.getPosition().x <= 0) {
            theBall.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
            bX = 0.5;
        }

        if (theBall.getGlobalBounds().intersects(paddle1.getGlobalBounds())) {

            bX *= -1;
        }

        if (theBall.getGlobalBounds().intersects(paddle2.getGlobalBounds())) {

            bX *= -1;
        }

        //AI for enemy paddle
        if (paddle2.getPosition().y < theBall.getPosition().y) {

            paddle2.move(0, AIspeedDown * time.asMilliseconds());
        }

        if (paddle2.getPosition().y > theBall.getPosition().y) {

            paddle2.move(0, AIspeedUP * time.asMilliseconds());
        }

        time = clock.restart();

        ////////update positions//////

        //////////////////////////////

        window.clear(sf::Color::Blue);

        //draw here//

        window.draw(paddle1);
        window.draw(paddle2);
        window.draw(theBall);

        /////////////

        window.display();

    } //ends game loop

    return 0;
}




 

7
General / Re: limited framerate issue
« on: November 18, 2016, 04:37:02 pm »
Thank you for the easy fix haha. I will check out the official tutorial because YouTube only helps so much :)

8
General / limited framerate issue
« on: November 18, 2016, 08:45:58 am »
I'm not even sure how to word my problem, but the problem is that I was told that by limiting the framerate, the program would be able to run on any computer the same, just maybe a bit laggy but still the same. the way the youtube video explained it, he had a sprite move a certain distance multiplied by the elapsed time between the last frame. all of that made sense. But now I'm having a problem trying to implement that same idea into events. The problem for me is that sometimes the paddle skips when going up, and I really don't know why. I have tried this several times with almost the same results.  Is there a simple fix for this? I'm using W for up, which is using the limited framerate to move, where as S is down, and it moves without a limited framerate. Here's my code:



#include <iostream>
#include <sfml/graphics.hpp>

int main()
{

    sf::RenderWindow window(sf::VideoMode(480, 480), "Pong");
    sf::Event event;

    sf::RectangleShape Paddle1(sf::Vector2f(40, 100));
    Paddle1.setOrigin(20, 50);
    Paddle1.setPosition(50, 240);
    Paddle1.setFillColor(sf::Color::Red);
    Paddle1.setOutlineThickness(-1);
    Paddle1.setOutlineColor(sf::Color::Black);

    window.setFramerateLimit(60);





    while(window.isOpen())
    {

        sf::Clock clock;
        sf::Time time = clock.getElapsedTime();

        while(window.pollEvent(event))
        {

            if(event.type == sf::Event::Closed)
            {

                window.close();
            }




            if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            {
                Paddle1.move(0, -0.5 * time.asMicroseconds());

                std::cout << Paddle1.getPosition().y << std::endl;
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            {
                Paddle1.move(0, 10);
                std::cout << Paddle1.getPosition().y << std::endl;
            }




            clock.restart().asMicroseconds();


        }//ends polling




        window.clear(sf::Color::White);

        ////////////////////////


        window.draw(Paddle1);


        ////////////////////////

        window.display();

    }//ends game loop



    return 0;
}









 








Pages: [1]
anything