Jump to content
  • Advertisement
Sign in to follow this  
Sade_129

Controlling a ball with a player

This topic is 3942 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!. I'm trying to control a ball with a player. Both have a simple circle as sprite. I want that when the player collides with the ball, the ball moves along with the player in the same angle. When the player it's not moving, but press the Right or Left key for rotate, the ball also has to rotate, like describing an orbit around the player. I use this for moving the player:
if (Input.IsKeyDown(sf::Key::Left))
{
    Player.Rotate(1);
}
		
if (Input.IsKeyDown(sf::Key::Right))
{
    Player.Rotate(-1);
}		
		
if (Input.IsKeyDown(sf::Key::Up))
{
    float DestX = (cos(-Player.GetRotation() * (3.1416f / 180))) ;
    float DestY = (sin(-Player.GetRotation() * (3.1416f / 180)));
    float x = Player.GetLeft();
    float y = Player.GetTop();
    Player.SetLeft(x + DestX);
    Player.SetTop(y + DestY);
}

GetLeft() and GetTop() is the same that GetX(), GetY() GetRotation() is the same that GetAngle() After this I check if there is collision between the player and the ball. If there is collision, "collision" is set to true. If there is not collision, "collision" is set to false. Now, the logical say that Ball has to do the same thing that player is doing for moving, except that the ball has to be a little more forward, exactly "PlayerRadius + BallRadius", isn't? Well, I'm doing exactly that:
if (colision)
{
    DestX = (PlayerRadius+BallRadius) * cos(-Player.GetRotation()* (3.1416f / 180)) ;
    DestY = (PlayerRadius+BallRadius) * sin(-Player.GetRotation()* (3.1416f / 180)) ;

    float x = Player.GetLeft();
    float y = Player.GetTop();

    Ball.SetLeft(x + DestX );
    Ball.SetTop(y + DestY);
}

And doesn't work in the same that I want. When the player rotate, the ball doesn't rotate at all. Also, sometimes the ball it's been pushed by the player. What am I doing wrong? Thanks in advance for your help!

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!