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

Author Topic: [SOLVED] vector2f.y multiplification not working arkanoid clone  (Read 1828 times)

0 Members and 1 Guest are viewing this topic.

a1s2d334

  • Newbie
  • *
  • Posts: 1
    • View Profile
[SOLVED] vector2f.y multiplification not working arkanoid clone
« on: November 02, 2018, 07:37:30 pm »
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

#include <iostream>

using namespace sf;

constexpr int windowWidth{800}, windowHeight{600};
constexpr float ballRadius{10.f}, ballVelocity{8.f};
constexpr float wallWidth{100.f}, wallHeight{35.f}, wallPad{15.f};
constexpr int wallCol{7}, wallRow{5};

struct Ball
{
    CircleShape shape;
    Vector2f velocity{-ballVelocity,-ballVelocity};
    Ball(float mX,float mY)
    {
        shape.setPosition(mX,mY);
        shape.setRadius(ballRadius);
        shape.setFillColor(Color::Red);
        shape.setOrigin(ballRadius,ballRadius);
    }
   
    void update()
    {
       
        if( x() > windowWidth  || x() < 0)
            velocity.x *= -1;
        if( y() > windowHeight || y() < 0)
            velocity.y *= -1;
        shape.move(velocity);
    }
    float x() { return shape.getPosition().x; }
    float y() { return shape.getPosition().y; }
};

Ball ball{windowWidth/2,windowHeight/2};

struct Wall
{
    RectangleShape shape;
    bool dead=false;
    Wall()
    {
        shape.setSize(Vector2f(wallWidth,wallHeight));
        shape.setFillColor(Color::Blue);
        shape.setOrigin(wallWidth,wallHeight);
    }
    void update()
    {
        float ballx = ball.x()+wallWidth;
        float bally = ball.y()+wallHeight;
        float xoff = -1, yoff=-1;
        bool result;
        if(!dead)
        {
            for(int i=0;i<2;i++)
            {
                for(int j=0;j<2;j++)
                {
                    if(shape.getGlobalBounds().contains(ball.x()+ballRadius*xoff,ball.y()+ballRadius*yoff))
                    {
                        ball.velocity.y *= -1.f;
                        shape.setFillColor(Color::Black);
                        dead=true;
                    }
                    xoff*=-1;
                    if(xoff<0)
                        yoff*=-1;
                }
            }

           
            //if( (ballx == x() && (bally > y() && bally < y()+wallHeight)) && (bally == y() && ballx < x()+wallWidth))
            /*if( (ballx > x() && ballx < x()+wallWidth) && (bally > y() && bally < y()+wallHeight) )
            {
                shape.setFillColor(Color::Black);
                dead=true;
                ball.velocity.y*= -1;
            }*/

        }
    }
    void setPos(float mX,float mY)
    {
        shape.setPosition(mX,mY);
    }
    float x() { return shape.getPosition().x; }
    float y() { return shape.getPosition().y; }
};

int main()
{
    Event event;
    //Ball ball{windowWidth/2,windowHeight/2};
    Wall walls[wallCol*wallRow];
    //Ball ball{800,600};
    RenderWindow window{{windowWidth,windowHeight}, "Arkanoid"};
    window.setFramerateLimit(60);
    float xoff = ((windowWidth - wallCol*wallWidth - wallPad*(wallCol-1))/2);
    for(int i=0;i<wallCol;i++)
    {
        for(int j=0;j<wallRow;j++)
        {
            walls[i*wallRow+j].setPos((i+1)*wallWidth+i*wallPad+xoff,(j+1)*wallHeight+j*wallPad+wallPad);
        }
    }

    while(true)
    {
        window.pollEvent(event);
        window.clear(Color::Black);
        if(Keyboard::isKeyPressed(Keyboard::Key::Escape)) break;
       
        for(int i=0;i<wallCol*wallRow;i++)
        {
            window.draw(walls[i].shape);
            walls[i].update();
        }
        std::cout << ball.velocity.y << std::endl;
        ball.update();
        window.draw(ball.shape);
        window.display();
    }
    return 1;
}

i'm making arkanoid clone and trying to make the ball bounce off the obstacles
and

if(shape.getGlobalBounds().contains(ball.x()+ballRadius*xoff,ball.y()+ballRadius*yoff))
{
    ball.velocity.y *= -1.f;
    shape.setFillColor(Color::Black);
     dead=true;
}

this part that setting fill color and dead to true works always
but changing velocity doesn't always work...
i'm really confused, it works sometimes sometimes not.
and the same exact expression in ball update function works always.
what am i doing wrong  :-\

edit:
the problem was that ball was hitting two block at the same time and it just seemed ball going through
the solution was to get elapsed time of hitting moments and compare them
« Last Edit: November 03, 2018, 03:20:36 pm by a1s2d334 »