# Controlling a ball with a player

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

## 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)
{

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!

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632936
• Total Posts
3009312
• ### Who's Online (See full list)

There are no registered users currently online

×