Sign in to follow this  

Controlling a ball with a player

This topic is 3728 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

This topic is 3728 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this