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

Author Topic: Help with changing a sprite direction  (Read 7434 times)

0 Members and 1 Guest are viewing this topic.

helloworld1234

  • Newbie
  • *
  • Posts: 9
    • View Profile
Help with changing a sprite direction
« on: April 23, 2015, 12:38:34 pm »
Does anyone know why when my sprite intersects with a border it doesn't rebound in the correct direction it just stops

THE PROBLEM : The problem i have is that when the ball hits the top border it just stops, if i change the values sometimes it stops or just carries on going past the border. I need it so the ball bounces and rebounds off them so the ball is still in play


Everything i try just doesn't work  :'(

Can anyone help?

I need the ball to rebound off the borders

The ball:
        sf::Vector2u basize = this->_wndw.getSize();

        this->_ball.setTexture(this->_sprite_res.get(71));
        this->_ball_data.startpos     = this->_player.getPosition() + sf::Vector2f(-8,-40);
        this->_ball.setPosition(this->_ball_data.startpos);
        this->_ball.setScale(sf::Vector2f(0.5f,0.5f));
        LJMUGame::setOriginCentre(this->_ball);

Borders:
        this->_top_edge  = sf::FloatRect(50.0f,128.0f,tscreenx,10.0f);
        this->_btm_edge  = sf::FloatRect(0.0f,tscreeny,tscreenx,10.0f);
        this->_left_edge = sf::FloatRect(-10.0f,0.0f,10.0f,tscreeny);
        this->_right_edge = sf::FloatRect(tscreenx,0.0f,10.0f,tscreeny);

Structs:
struct BallData
{
        sf::Vector2f directionup;
        sf::Vector2f directiondown;
        sf::Vector2f directionleft;
        sf::Vector2f directionright;
        sf::Vector2f startpos;  
        float        speed;    
        bool         active;   
        BallData()
        {
                         
                directiondown = sf::Vector2f(0.0f,1.0f);
                directionup = sf::Vector2f(0.0f,-1.0f);
                directionleft = sf::Vector2f(1.0f,0.0f);
                directionright = sf::Vector2f(-1.0f,0.0f);
                startpos = sf::Vector2f(0.0f,0.0f);
                speed    = 200.0f;
                active   = false;
        }
};


Update ball:(what i've done so far, fails)
        //-------UPDATE THE BALL-----------------------------------------
        if(this->_ball_data.active)
        {
        sf::Vector2f balldirection = this->_ball_data.directionup * this->_ball_data.speed  * ptpf.asSeconds();
        this->_ball.move(balldirection);
       
       
        //-------UPDATE THE BALL COLLISIONS TOP EDGE------------------------------
         if(this->_ball.getGlobalBounds().intersects(this->_top_edge))
        {

        sf::Vector2f balldirection = this->_ball_data.directiondown * this->_ball_data.speed * ptpf.asSeconds();
        this->_game_data.score += 10;
        this->_ball.move(balldirection);
        }
« Last Edit: April 23, 2015, 04:14:54 pm by helloworld1234 »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help with changing a sprite direction
« Reply #1 on: April 23, 2015, 02:19:00 pm »
Maybe my "bouncing ball" example can provide some inspiration/help :-)
https://github.com/SFML/SFML/wiki/Source:-Bouncing-ball

helloworld1234

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with changing a sprite direction
« Reply #2 on: April 23, 2015, 02:29:46 pm »
Hmm had a look still struggling because of the different layout, i'm a proper noob so might need a bit of a starter bump

blojayble

  • Newbie
  • *
  • Posts: 19
  • whoa
    • View Profile
Re: Help with changing a sprite direction
« Reply #3 on: April 23, 2015, 02:32:59 pm »
First of all, why do you use "this->" everywhere? It's just a snippet but I'm quite sure you don't need to.
Secondly, why do you use separate vector for every direction? One will do better, also it would clean up your code. :D

It's hard to see what's going on, comment says bottom edge, code checks top edge, we do not know what's inside the move method and so on.
My guess would be that when you expect it to rebound you apply both direction up and direction down, which are cancelling each other, making it stop.
0 bottles of beer on the wall,
0 bottles of beer!
Take one down, pass it around,
4,294,967,295 bottles of beer on the wall!

helloworld1234

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with changing a sprite direction
« Reply #4 on: April 23, 2015, 03:16:47 pm »
Yeah sorry forgot to change the comment from bottom to top

also i no they are cancelling each other out but even when i put it higher it still doesn't rebound

i use this-> because its just the way we've been told to do it

helloworld1234

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with changing a sprite direction
« Reply #5 on: April 23, 2015, 04:11:08 pm »
Everything i try just doesn't work  :'(

Can anyone help?

I need the ball to rebound off the borders

The ball:
        sf::Vector2u basize = this->_wndw.getSize();

        this->_ball.setTexture(this->_sprite_res.get(71));
        this->_ball_data.startpos     = this->_player.getPosition() + sf::Vector2f(-8,-40);
        this->_ball.setPosition(this->_ball_data.startpos);
        this->_ball.setScale(sf::Vector2f(0.5f,0.5f));
        LJMUGame::setOriginCentre(this->_ball);

Borders:
        this->_top_edge  = sf::FloatRect(50.0f,128.0f,tscreenx,10.0f);
        this->_btm_edge  = sf::FloatRect(0.0f,tscreeny,tscreenx,10.0f);
        this->_left_edge = sf::FloatRect(-10.0f,0.0f,10.0f,tscreeny);
        this->_right_edge = sf::FloatRect(tscreenx,0.0f,10.0f,tscreeny);

Structs:
struct BallData
{
        sf::Vector2f directionup;
        sf::Vector2f directiondown;
        sf::Vector2f directionleft;
        sf::Vector2f directionright;
        sf::Vector2f startpos;  
        float        speed;    
        bool         active;   
        BallData()
        {
                         
                directiondown = sf::Vector2f(0.0f,1.0f);
                directionup = sf::Vector2f(0.0f,-1.0f);
                directionleft = sf::Vector2f(1.0f,0.0f);
                directionright = sf::Vector2f(-1.0f,0.0f);
                startpos = sf::Vector2f(0.0f,0.0f);
                speed    = 200.0f;
                active   = false;
        }
};


Update ball:
        //-------UPDATE THE BALL-----------------------------------------
        if(this->_ball_data.active)
        {
        sf::Vector2f balldirection = this->_ball_data.directionup * this->_ball_data.speed  * ptpf.asSeconds();
        this->_ball.move(balldirection);
       
       
        //-------UPDATE THE BALL COLLISIONS TOP EDGE------------------------------
         if(this->_ball.getGlobalBounds().intersects(this->_top_edge))
        {

        sf::Vector2f balldirection = this->_ball_data.directiondown * this->_ball_data.speed * ptpf.asSeconds();
        this->_game_data.score += 10;
        this->_ball.move(balldirection);
        }

blojayble

  • Newbie
  • *
  • Posts: 19
  • whoa
    • View Profile
Re: Help with changing a sprite direction
« Reply #6 on: April 23, 2015, 04:15:08 pm »
Try to use debugger(or just printf) to check which variable is problematic, ie its value is different from what you would expect it to be.
0 bottles of beer on the wall,
0 bottles of beer!
Take one down, pass it around,
4,294,967,295 bottles of beer on the wall!

helloworld1234

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with changing a sprite direction
« Reply #7 on: April 23, 2015, 04:20:06 pm »
Blo, i think the problem is that my vector is always keeping the first value it is assigned, which is why it always is going up

Is there a way to unassign the vector from it to give it a new one on each collision?
« Last Edit: April 23, 2015, 04:24:57 pm by helloworld1234 »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Help with changing a sprite direction
« Reply #8 on: April 23, 2015, 04:25:59 pm »
i think the problem is that my vector is always keeping the first value it is assigned

Don't guess or just think, use your debugger and know for sure what is happening. We are not your debuggers...
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

helloworld1234

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with changing a sprite direction
« Reply #9 on: April 23, 2015, 04:33:49 pm »
i think the problem is that my vector is always keeping the first value it is assigned

Don't guess or just think, use your debugger and know for sure what is happening. We are not your debuggers...

When i press my debugger it just runs the program and nothing about the vector is mentioned in the debug, i'm new to this so i must be misunderstanding

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help with changing a sprite direction
« Reply #10 on: April 23, 2015, 04:35:49 pm »
Set a breakpoint at the interresting position. Inspect variable values and single-step the program and watch how they change. Compare to your expectations.

helloworld1234

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with changing a sprite direction
« Reply #11 on: April 23, 2015, 04:39:02 pm »
i think the problem is that my vector is always keeping the first value it is assigned

Don't guess or just think, use your debugger and know for sure what is happening. We are not your debuggers...

When i press my debugger it just runs the program and nothing about the vector is mentioned in the debug, i'm new to this so i must be misunderstanding


Figured it out i put a break point after both the vectors they return

+               balldirection   {x=0.00000000 y=-5.3351998 }    sf::Vector2<float>

+               balldirection   {x=0.00000000 y=-5.3351998 }    sf::Vector2<float>
 

So that means its keeping its first value and not changing?

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Help with changing a sprite direction
« Reply #12 on: April 23, 2015, 04:45:46 pm »
Not sure if you are aware, but note that your 2 balldirection vectors are not the same variable even though they have the same name. The second time you are defining a new variable with the same name, but in a different scope. After your if statement, it will be like the second assignment never happened because the second version of the vector will go out of scope.

In addition to using the debugger you can also just think through what your code is doing line by line, specifically the "update ball" logic.  If your ball is active you are going to move it up a bit each frame. if you have a collision you move it down a bit. This is already problematic because the down movement is likely  going to cancel out the up movement and the ball won't move anywhere. You are moving the ball down by the same amount you moved it up. You shouldn't move your ball up in the case where you are colliding.

 ... But even if it didn't cancel out and you actually started moving down, as soon as you aren't colliding any more it would just move up again. You are only trying to move down in the case you are colliding. Any time you aren't colliding you are moving the ball up. This is likely not what you want.

There are many ways to approach this problem, though this sounds like it might be a homework assignment so I'm reluctant to give you concrete answers. Assuming your ball just needs to bounce up and down, one simple idea is to use a variable to hold what state the ball is in. If the ball is in the "Up" state, then move the ball up. When the ball is in the "Down" state, then move the ball down.

If your ball needs to bounce in arbitrary directions you will need to figure out how to store the ball's current trajectory and modify it when you collide with a wall.

Having said all of that, Jesper Juhl's advice on learning how to use a debugger is a great idea.
« Last Edit: April 23, 2015, 04:51:46 pm by Arcade »

helloworld1234

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with changing a sprite direction
« Reply #13 on: April 23, 2015, 05:00:32 pm »
:( give up

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help with changing a sprite direction
« Reply #14 on: April 23, 2015, 05:12:22 pm »
I'm not trying to be mean, but if you give up this easily on a problem this simple you won't go far as a developer.

You've been given plenty of information to enable you to solve your problem.
You have a tiny working example of a bouncing ball - read it, understand it.
You've been given hints on how to use a debugger to solve your problem; now go learn more about debuggers.
And you've been given various other tips to help you in the right direction; read them again and reflect on them.

Programming is all about solving problems and a big part of that is researching and learning new things + experimenting. Now go learn, experiment & don't give up.

 

anything