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

Author Topic: moving a ball over the window  (Read 1684 times)

0 Members and 2 Guests are viewing this topic.

schuki

  • Newbie
  • *
  • Posts: 8
    • View Profile
moving a ball over the window
« on: October 16, 2014, 05:35:52 pm »
Hi there,
for my studies I have to program a simple game. There for I chose "Break out" and sfml. I created a class ball and reset the position:

void Ball::ResetPosition(int windowWidth, int windowHeight)
{
        _moveToX = -1.f;
        _moveToY = -1.f;
        float distanceToGround = 10.f;
        float ballPositionX = windowWidth / 2.f;
        float ballPositionY = windowHeight - _paddleSize.y - distanceToGround - (_ball.getSize().y) - 4;
        _ball.setPosition(ballPositionX, ballPositionY);
}

For collision testing I don't want to move the ball randomly on the screen. There for I created to variables, the first one is for the movement direction to the x-axis and the second one for the y-axis.

My game thread calls from now on the function MoveBall:
void Ball::MoveBall()
{
        _ball.move(_moveToX, _moveToX);
}

The idea was the following... If I set the direction to -1,-1 the ball would move towards to the left corner of my window. If I change the direction to 1 and -1 it would move to the right side of the window.

My problem now is, that the ball doesn't behave like expected:

vectordirection
1, 1down
-1, -1left
-1, 1left
1, -1down


Is there something I wrong with my code? Or has the ball possibly a own coord system?

rgds
_schuki

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: moving a ball over the window
« Reply #1 on: October 16, 2014, 06:13:34 pm »
Perhaps my Bouncing ball example will help you :)

Edit:
Quote
My game thread ...
Ehh, why would you complicate such a simple thing with threads. My advice: stay far away from threads as long as you possibly can. They add more complexity than one would think.
« Last Edit: October 16, 2014, 06:31:41 pm by Jesper Juhl »

schuki

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: moving a ball over the window
« Reply #2 on: October 16, 2014, 06:37:44 pm »
it's not a thread like sf::Thread, it's just the while(window.isOpen()) loop =)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: moving a ball over the window
« Reply #3 on: October 16, 2014, 06:58:23 pm »
That's the game/main loop, don't confuse terms. :)

I always wonder with uni projects, why do we have to solve your issues, when you (usually) get paid assistants from the uni? ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

schuki

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: moving a ball over the window
« Reply #4 on: October 16, 2014, 07:42:47 pm »
haha  ;D because they don't know how to solve the problem. I'll review my code and I'll have a look at the link you posted.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: moving a ball over the window
« Reply #5 on: October 18, 2014, 12:57:14 am »
You may want to consider not mixing up moveToX with moveToY  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything