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

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

0 Members and 1 Guest are viewing this topic.

helloworld1234

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with changing a sprite direction
« Reply #15 on: April 23, 2015, 05:14:45 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.

I no you're right, but everything i experiment with fails, i get everything else just can't get my head around this collision detection so hey ho

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Help with changing a sprite direction
« Reply #16 on: April 23, 2015, 06:27:51 pm »
*scraps entire long attempt at being helpful and starts again because misread your code*
You are not actually permenantly changing anything in your if loops. If you put a velocity variable in your struct instead of speed and then just multiply the x or y value by -1 when you collide with the edge, and then move by velocity in the program loop then it should work okay. Hope this helps. If you still don't get it, I'll try making an simple example.

P.S. repeated initialization in a game loop is a bad idea partially because it is inefficient and partially because it resets the value every time and this can be confusing.

P.P.S. I would advise against names like ptpf as it can be difficult for other people to know what it means.

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Help with changing a sprite direction
« Reply #17 on: May 15, 2015, 08:48:46 pm »
In my first game I made, I made shapes rebound off walls with simply:


... // Make the shape bounce

if (!boundingbox.intersects(boundingbox2))
{
shape1.move(0, 0.1);
}
 
Hope this helps  :)

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Help with changing a sprite direction
« Reply #18 on: May 16, 2015, 04:29:12 am »
Everything i try just doesn't work  :'(

Can anyone help?

I need the ball to rebound off the borders

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;
        }
};

First off you don't need to store 4 vectors you only need one of them and simply manipulate it to your heart's content.

Like this:

class BallData
{
        sf::Vector2f direction;
        sf::Vector2f startpos;  
        float        speed;    
        bool         active;   
        BallData(sf::Vector2f directioninput)
        {
                         
                direction = directioninput; // Can be given a random direction in several ways.
                startpos = sf::Vector2f(0.0f,0.0f);
                speed    = 200.0f;
                active   = false;
        }
        void setDirection(sf::Vector2f directioninput){/**/}
        sf::Vector2f getDirection(){/**/}
};


 

// Some other file............

Pseudo Code

~Hit lower wall or upper wall
Reflect the Y axis

ball.setDirection(sf::Vector2f(ball.getDirection().x,ball.getDirection().y*-1)

~Hit Left or Right Paddle
Reflect the X axis

ball.setDirection(sf::Vector2f(ball.getDirection().x*-1,ball.getDirection().y)


 


Keep in mind depending on how the playing area is setup this could change a bit.

The general Idea is it is simpler than it looks.  Also learning to use the debugger and the breakpoints of it would also help.  If I remember right most IDEs will let you click in the margin to set breakpoints.

I have many ideas but need the help of others to find way to make use of them.