# Goalkeeper Jumping Algorithm?

This may be a "best way to" question, so may be susceptible to opinion-based answers (which is ok for me). But I would also like if there are any tutorials , research papers , etc.

I'm trying to make a 3D free kick game, and I want to decide on the Goalkeeper algorithm.

Usually (and in my game) the shot is a function of direction , power and swerve. Maybe wind can be a factor too. So the shots trajectory is pre-deterministic, i.e., the point that intersects the goal (or out) plane is determined as soon as the shot fires.

So what could be a good approach for the goalkeeper to jump (or walk) ?

I have two approaches in mind :

1- determine the point, and jump to there. 2- when the ball is closer than a threshold distance, estimate the point by the ball's velocity vector, and jump to there.

The problem with the first approach is, goalkeeper will always save the shot, which I want to prevent. So there should be some kind of randomness, or the goalkeeper's ability to jump and walk will be restricted.

The problem with the second approach is, when there is a high amount of swerve, the goalkeeper will always fail to save the shot.

So how could these approaches be better?

Thanks for any help !

## Edit:

• Think Pong AI; The paddle can only move so fast. It has to try and guess where the ball is going and position itself accordingly. If it guesses right, it has a good chance of stopping the ball, if it guesses wrong, it'll likely miss. The goal keeper has a small area around him where he can move very quickly, in the form of jumping. This is a movement he can only use once. Your question is a "best way" type question. While you may be OK receiving opinion based answers, that's not the purpose of this site. I suggest you implement something and come back with specific problems if there are any. – MichaelHouse Dec 4 '13 at 14:32
• Just played the game: very fun! I played for about 30 mins to get the feel of it, and I think your keeper-AI is spot on. I've played FIFA games and the like for a few years now, and I can say the game feels pretty natural as well. – igrad Mar 14 '14 at 6:05

You know where the ball will hit the goal, and you know when this will happen. So you could set up some basic variables such as "reaction speed", "movement speed" and/or "jumping force".

A goal-keeper with a high reaction- and movement-speed will be able to catch most shots, while a goal-keeper with worse stats won't be able to do so.

If you have the ball-travel time t, and the player reaction time rt, then the effective time for the player to react will be t - rt. This is the time he has to move from his position to the position where the ball will hit the goal. So if (t - rt) * movement_speed > distance_to_ball, then the ball can be saved.

Of course it should become gradually more difficult to catch a ball that's further away from the keeper. Also you might want to introduce some kind of randomness... so the rule could be:

if( distance_to_ball / ((t - rt) * movement_speed) <= RAND() ){
// ball catched
}


Where RAND() would return a random float between 0 and 1.

The "Swerve" could just be some factor that reduces the players actual time to react.

Also instead of using a linear approach like above, you could experiment with another falloff (cubic, exponential etc.), so that shots fired really close to the goalkeeper have a much higher chance of getting caught.

I think the most important aspect is that the gameplay is fun and diverse. There's so much luck involved in saving a penalty-shot, that a realistic simulation/algorithm isn't going to produce much better results than something close to completely random.

Try to tweak the formula in a way that it rewards skill (eg. if you practice to land good shots, your chances of scoring are increased) but still allows a complete newcomer to score a goal as well.

• I got a good idea from these comments. Randomness could be a function of distance. i.e., the closer the shot, the more the randomness is. Thanks ! – jeff Dec 4 '13 at 16:00
• Another factor, which occurs in FIFA's penalty kicks: the keeper has to anticipate the shot's target, and has to move that way to block. So they will sometimes guess poorly and fail by diving the wrong way, or by diving at all when they should have stay centered. – Seth Battin Dec 6 '13 at 23:09
• @Seth Battin haha. my keeper does that too :) – jeff Dec 30 '13 at 10:48

In reality, a goalkeeper needs to do two things:

1. watch the ball and notice when its trajectory will cause it to fly into the goal
2. when this is the case, jump into the way

(a real soccer fan will now likely tell me about 20 other jobs a soccer goalkeeper has to perform, but bear with me, I am a nerd who hates sports).

To perform these jobs, the goalkeeper has to react quickly and correctly. When the kick is performed from a longer distance and the player isn't shooting very fast, the keeper will have a longer time to react and will be more likely to catch it. But a shot from a close distance could not leave the keeper enough time to react at all or when he reacts, the surprise might cause him to react incorrectly. The performance of a goalkeeper isn't consistent. There are cases of amateur keepers catching seemingly impossible shots or world-class keepers making stupid blunders in harmless situations. Both occurences aren't frequent, but they happen from time to time. So a certain element of chance should be part of your AI.

To simulate this, you could give the AI keeper a certain probability per physics-tick to react on a goal-shot and perform the necessary action to prevent it (it might already be too late to do this succesfully, but it would look more human when he would try anyway). When the keeper reacts, you could also give them an additional probability to react incorrectly. This probability should be antiproportional to the distance which is left, so that "stupid mistakes" are more likely to happen in difficult situations than in easy ones. The exact probabilities could be variable depending on the skill-level of the keeper and/or your difficulty level.

My team is working in a Football MMO and our approach is to attribute goal keepers (and other players) with several attributes, such as JUMP, PHYSIQUE, AGILITY, REACTION, REASON, POSITIONING, to name a few. The probability to perform a task is then a weighted sum of the attributes of the player related to that task. In your case, the task is to defend the shoot. You can model this as simple or complex as you like, depending on the quantity of skills used. You can also save presets of the skills set and use them depending on the difficulty, for example. Finally, to get some randomness, just simulate a dice and use it to check if the returned value is within the probability threshold. If it is, the shoot is defended. If not, it is a goal.

As of playing as a goalkeeper, I have to suggest these: Not all keepers have the same ability to jump, intelligence or reflects. As stated before, a goalkeeper always looks at the ball - and in real world, a good response is to move so that the goalkeeper will always cover the ball no matter how far it is from the goal if a straight shot is achieved.

An idea could be this: the keeper is moving on side based on the ball. When a shot is performed, the goalkeeper will "fall" (put his body in the angle of the potential position of the ball when it reaches the goal line) and fall (with animation) after a while. The keeper will jump (move horizontally, vertically or diagonally up for a while and then fall) if the potential position of the ball on the line is in bigger distance than its body length.

The delay of this move can be associated with the reflects of the keeper and the how high will it jump with the skills. Now, about intelligence, you could set a random indicator of when it will be confused by the curve (and jump in wrong way or with delay). The higher the skill, the less the probability to get confused. Jump delay, height and confusion can probably be enough for simple cases (if the goalkeeper jumps with delay or not far enough, then a goal can be achieved).

And again this is very generic, and just the way I think of it.

The ways to handle this are different based on how you are assigning speed, swerve etc. I'm assuming you have a skill challenge to do that ("stop the moving needle in the right part of the gauge"-type thing or something similar). If so, then you want higher-swerve and higher-speed to equal higher-percentage chance of scoring. If they are just chosen values, then they shouldn't have such a direct effect, or the player will always choose them to be high.

You could set a percentage of the time you want the goalkeeper to save (based on the speed and swerve), and simply pick a random number, if it's below that percentage, play the correct animation (diving the right way) for the save, if it's higher, play the wrong one and allow the score. This is simple, but also a little "cheating"-like.

You could set an amount of time for the goalie to read the direction the ball is going, and then start moving him in that direction, and constantly allow him to adjust for the swerve until the last X% of the time it takes the ball to arrive, at which point the goalie can dive for the ball if it's within a range of where he is (and outside "move-to" range). "Better" goalies (higher levels?) will read sooner, move faster, and dive later(and further). This is more complicated, but more flexible, and more simulation-like.

• In my game, freekick shots are very similar to PES games. You adjust the direction (left - right) , then you adjust swerve (a point on the ball's surface), then the power. Your second approach is very similar to the one I've been using in my previous game. So I guess I need to improve that one :) Thanks ! – jeff Dec 5 '13 at 15:18