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

Author Topic: how to get the position of a cirlce.  (Read 1378 times)

0 Members and 1 Guest are viewing this topic.

gelatine

  • Newbie
  • *
  • Posts: 7
    • View Profile
how to get the position of a cirlce.
« on: June 28, 2012, 09:38:20 pm »
hello i am making an pong game but when i can't get the position of the ball. i have tried to use ball.GetPosition() but that didn't work. so could someone tell me how to do that.
 this is my code:
sf::Shape ball=sf::Shape::Circle(395, 330, 10, sf::Color(200,200,200));
ball.Move(-49.1934955f * ElapsedTime, -98.386991f * ElapsedTime);
std::cout<<ball.GetPosition()<<std::endl;
 
full code under this if someone wants to test.
#include "stdafx.h"
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Shapes"/*, sf::Style::Fullscreen*/);

        //define shapes
        sf::Shape panel1=sf::Shape::Rectangle(20, 300, 30, 360, sf::Color::White);
        sf::Shape panel2=sf::Shape::Rectangle(770, 300, 780, 360, sf::Color::White);
        sf::Shape ball=sf::Shape::Circle(395, 330, 10, sf::Color(200,200,200));
     
        bool game=false;
        int direction=0;

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

                        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                                App.Close();

                        if (Event.Type == sf::Event::KeyPressed && Event.Key.Code==sf::Key::Space)
                game=true;
        }

        // Clear screen
        App.Clear();

        // Draw shapes
                App.Draw(panel1);
                App.Draw(panel2);
                App.Draw(ball);
       
                //elapsed time
                float ElapsedTime = App.GetFrameTime();
               
                //move panels
                if (App.GetInput().IsKeyDown(sf::Key::Up))    panel2.Move(0, -110 * ElapsedTime);
                if (App.GetInput().IsKeyDown(sf::Key::Down))  panel2.Move(0, 110 * ElapsedTime);

                if (App.GetInput().IsKeyDown(sf::Key::U))     panel1.Move(0, -110 * ElapsedTime);
                if (App.GetInput().IsKeyDown(sf::Key::J))     panel1.Move(0, 110 * ElapsedTime);

                if (game) {
                        if (direction==0) {
                                ball.Move(-49.1934955f * ElapsedTime, -98.386991f * ElapsedTime);
                                               //sqrt(110²/5)                     <-- *2
                                std::cout<<ball.GetPosition()<<std::endl;
                        }
                }

        // Finally, display the rendered frame on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}
« Last Edit: June 28, 2012, 09:46:03 pm by gelatine »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: how to get the position of a cirlce.
« Reply #1 on: June 28, 2012, 09:58:14 pm »
... that didn't work.

This isn't really usefull. If something doesn't work the way you inteded it to work, then you have to tell other people, what your intention was and what really happend. (e.g. your DVD won't play on your computer, can you just go an give the PC to an IT guy and say: "It doesn't work" and he'll figure out that you were talking about a DVD?) ;)

But since you've provided the code, I can see what you're doing wrong. GetPosition() returns a sf::Vector2 which doesn't have a operation overload for << and thus can't just be printed out. You'd need to change that line to:

std::cout<<ball.GetPosition().x<<","<<ball.GetPosition().y<<std::endl;

Additionally I'd advice you to use SFML 2. The tutorials aren't yet all completly uptodate but with the documentation you can quite easily transcribe it on your own.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gelatine

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: how to get the position of a cirlce.
« Reply #2 on: June 28, 2012, 10:07:26 pm »
o ok thank you ;d