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

Author Topic: The shapes are not been position correctly?  (Read 3200 times)

0 Members and 1 Guest are viewing this topic.

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
The shapes are not been position correctly?
« on: May 16, 2014, 04:41:41 am »
So i'm trying to position the rectangle in the middle of the window, however, everytime i increase the y value of the rectangle position, it seems to go down instead. Here is the code:
// MyPongGame.cpp : Defines the entry point for the console application.
// Using SFML Game Library

#include "stdafx.h"
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>

class CreatingShapes
{
public:
        sf::RectangleShape paddleShape;
        sf::CircleShape ballShape;

       
        CreatingShapes(sf::Vector2f paddlePosition, sf::Vector2f paddleSize, sf::Color paddleColor){
                paddleShape.setPosition(paddlePosition);
                paddleShape.setSize(paddleSize);
                paddleShape.setFillColor(paddleColor);
        }
        CreatingShapes(sf::Vector2f ballPosition, float ballRadius, sf::Color ballColor)
        {
                ballShape.setPosition(ballPosition);
                ballShape.setRadius(ballRadius);
                ballShape.setFillColor(ballColor);
        }



};

int main()
{
        sf::RenderWindow renderWindow(sf::VideoMode(500, 350), "My Pong Game");

        CreatingShapes leftPaddle(CreatingShapes(sf::Vector2f(5,300), sf::Vector2f(10,75), sf::Color::Blue));
        CreatingShapes rightPaddle(CreatingShapes(sf::Vector2f(485,250), sf::Vector2f(10,75), sf::Color::Red));
        CreatingShapes pongBall(CreatingShapes(sf::Vector2f(250, 175), 5, sf::Color::Magenta));
       

        while(renderWindow.isOpen()){ //The Game loop or Main loop
                //Events may only be declared in the same function as the window object
                sf::Event event; //Creates SFML Event object
               
                while(renderWindow.pollEvent(event)){//Check which events were triggered, whther its Key pressed, mouse moved, etc.
               
                        if(event.type == sf::Event::Closed){ //if Close Request event was clicked then:
                       
                                renderWindow.close(); //closes window
                        }else if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        {
                        //      leftPaddle.paddleShape.move(0, -3.0f);
                        }else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        {
                                //leftPaddle.paddleShape.move(0, 3.0f);
                        }
        }
       
               
                //Clear the screen
                renderWindow.clear(sf::Color::Cyan);

                //draw everything here
                renderWindow.draw(leftPaddle.paddleShape);
                renderWindow.draw(rightPaddle.paddleShape);
                renderWindow.draw(pongBall.ballShape);
               
                //end the current frame
                renderWindow.display();
        }

    return 0;
}

 

How can i get the rectangle in the middle?

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: The shapes are not been position correctly?
« Reply #1 on: May 16, 2014, 05:55:55 am »
So i'm trying to position the rectangle in the middle of the window, however, everytime i increase the y value of the rectangle position, it seems to go down instead. Here is the code:

It's normal, (0,0) is the upper left part of the screen. Increasing y value will make your rectangle to go down.

How can i get the rectangle in the middle?

x = Screen Width / 2 - Rectangle width / 2
y = Screen Height / 2 - Rectangle Height / 2

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: The shapes are not been position correctly?
« Reply #2 on: May 16, 2014, 08:23:26 pm »
i thought that (0,0) was the bottom left hand corner? I was assuming that the paddle will increase whenever you increase the Y value rather than decrease.
 
How can i get it to start on (0,0) from the bottom left hand corner!

And thank you for the middle variables.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: The shapes are not been position correctly?
« Reply #3 on: May 16, 2014, 08:28:37 pm »
You should probably go read all of the official tutorials.  They're short, to the point, and will answer all of your questions.  For instance, the top-left corner thing is very clearly stated in the Sprites and Textures part:

Quote
By default, the origin for these three transformations is the top-left corner of the sprite. If you want to set the origin to a different point (for example the center of the sprite, or another corner), you can use the setOrigin function.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: The shapes are not been position correctly?
« Reply #4 on: May 16, 2014, 09:56:15 pm »
A coordinate system that starts at the top-left and extends right (x-axis) and down (y-axis) is pretty common in computer graphics. I would suggest to just get used to it.

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
The shapes are not been position correctly?
« Reply #5 on: May 17, 2014, 02:56:04 am »

A coordinate system that starts at the top-left and extends right (x-axis) and down (y-axis) is pretty common in computer graphics. I would suggest to just get used to it.

Oh really? I didn't know that since when I started it, the position (0,0) was at the bottom left hand corner! That's why I was confused but now it's cleared! And I managed to do all the graphic parts needed, now to work on the event and collision