a "pass" funktion in a ball game? looks bad.

Hi
Im doing a ball game and is working on the pass and shoot funktions.
Right now im doing so that i have the ball x and y and while the ball hasent intesected with a player it moves 3 pixels in the dir towards the player. 3 pixels in x and 3 in y until the ball have the same x and y as the player.
The problem is that this dont look nice at all… the ball is moving werry bad.

I was woundering if anyone have a better idea in how to make this look better and natural.
I have been thinking about using pytagoras mathematics to calculate the length of the hypotenusa(i spell bad i know :frowning: ) and by that somehowe make the ball go 3 pixels on that line… but the problem is in how to calculate the x and y to the endpoint… ???

But someone may have a mutch better sulution…
Thanks for your time… :slight_smile:

If you keep moving the ball towards the player, you’re going to get a homing pass that will kinda take all the skill out of the game :wink:

Heres how i’d probably go about things:

  1. At the start of the pass, find the direction the ball should travel in (probably due to start and target relative positions).

  2. Per update, if not hit the player, then move along the direction vector.

// At start of throw:
float dx = target.x - start.x;
float dy = target.y - start.y;
// Normalise the direction vector
float length = Math.sqrt( dx*dx + dy*dy );
dx = dx / length;
dy = dy / length;


// Per update:
ballX += dx * speed * timeDelta;
ballY += dy * speed * timeDelta;