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

Author Topic: Window coordinates  (Read 2459 times)

0 Members and 1 Guest are viewing this topic.

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
Window coordinates
« on: June 19, 2010, 02:13:24 am »
Can somebody tell me if there is something wrong with my code?
Code: [Select]

// Ball.hpp

#ifndef BALL_HEADER
#define BALL_HEADER

#include <SFML/Graphics.hpp>

class Ball {

public:
Ball();
~Ball();

void Update(float ElapsedTime);
void ChangeX();
void ChangeY();
void Draw(sf::RenderWindow &cWin);

float GetX() const { return X; }
float GetY() const { return Y; }

private:
sf::Shape cBall;
float X;
float XSpeed;
float Y;
float YSpeed;

};

#endif


Code: [Select]

//Ball.cpp

#include "Ball.hpp"
#include <iostream>

Ball::Ball(): X(0.0f), XSpeed(50.0f), Y(0.0f), YSpeed(50.0f) {

   cBall = sf::Shape::Circle(100, 100, 10, sf::Color(100, 100, 100));
}

Ball::~Ball() {

}

void Ball::Update(float ElapsedTime) {

   cBall.Move(XSpeed * ElapsedTime, YSpeed * ElapsedTime);
   sf::Vector2f Pos = cBall.GetPosition();
   X = Pos.x;
   Y = Pos.y;

   std::cout << X << " " << Y << " - " << XSpeed << " " << YSpeed << std::endl;
}

void Ball::ChangeX() {

   XSpeed *= -1.0f;
}

void Ball::ChangeY() {

   YSpeed *= -1.0f;
}

void Ball::Draw(sf::RenderWindow &cWin) {

   cWin.Draw(cBall);
}


Code: [Select]

// Main.cpp

#include "Ball.hpp"

int main() {

sf::RenderWindow cWin(sf::VideoMode(800, 600), "test");
cWin.SetFramerateLimit(60);
sf::Event Event;

Ball cBall;

while ( cWin.IsOpened() ) {
while ( cWin.GetEvent(Event) ) {
if ( Event.Type == sf::Event::Closed )
cWin.Close();
}

cBall.Update(cWin.GetFrameTime());
if ( cBall.GetX() > 780.0f || cBall.GetX() < 20.0f )
cBall.ChangeX();
if ( cBall.GetY() > 580.0f || cBall.GetY() < 20.0f )
cBall.ChangeY();

cBall.Draw(cWin);

cWin.Display();
cWin.Clear();
}

return 0;
}

It compiles and runs. But it runs wierd.
The ball moves +50 on X and +50 on Y the first frame, then it goes -50, -50 on the next frame and repeats until I click the window and hold for 2-3 secs, then when I let go, the ball starts moving. Although the coordinates printed in the console seem fine, the ball is render off by some pixels. Also, the limits are off, everything is off!

I searched the forum for some time now, I couldn't find anything relevant. So I decided to make a new post.

Sorry for my bad English, hopefully everybody understands what I'm saying.

Terrydil

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Window coordinates
« Reply #1 on: June 19, 2010, 08:56:23 pm »
After creating cBall with sf::Shape, try initializing its position with cBall.SetPosition().  I think its initial coordinates are causing ChangeX() and ChangeY() to get called every frame, which just makes it oscillate between moving one way and the other.  If you set it to at least 20, 20 then ChangeX/Y won't get called every time.

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
Window coordinates
« Reply #2 on: June 19, 2010, 11:20:21 pm »
I saw that the initial arguments in sf::Shape::Circle are actually it's center position. Which is kind of confusing. If I put the center to 0 and put after the SetPosition to 100, 100, it seems almost right. The if statement with ChangeX/Y works fine, but the coordinates ar still off.

I think, and this is only a sugestion, the tutorials on shapes and views should be have more explications. I can't figure out what's wrong.


My main question: HOW DO I HANDLE POSITIONINGS AND COORDINATES IN SFML?
Thank you.

Zweistein

  • Newbie
  • *
  • Posts: 29
    • View Profile
Window coordinates
« Reply #3 on: June 20, 2010, 12:43:21 am »
Quote
XSpeed *= -1.0f;


So you get -50; +50; -50; +50....

Quote
cBall.Move(XSpeed * ElapsedTime, YSpeed * ElapsedTime);


So you move -50*ElapsedTime, +50*ElapsedTime, -50*ElapsedTime, ....

Quote
until I click the window and hold for 2-3 secs

ElapsedTime with be much higher in that moment, because it won t call the update Methods while clicking on the windowtitle.