Controlling a ball with a player

Started by
-1 comments, last by Sade_129 16 years, 6 months ago
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!

This topic is closed to new replies.

Advertisement