   Basketball Simulation - how-to?
  Hey all,I was wondering how some of you (familiar with the sport, of course) would approach simulating a game of basketball. I was thinking that, at the simplest level, it would just be a series of semi-random actions whose probabilities are based on each player's many statistics - i.e. the probability that a player chooses to shoot is based on x, y, and z. In this fashion, there's no need for any kind of AI, and if you modelled it correctly (how each attribute affects what), I think you could get some decent results - of course there wouldn't be any visuals to go with it. What do you guys think? What's another (better?) way of approaching this problem?- carb   
Quote:Original post by carbHey all,I was wondering how some of you (familiar with the sport, of course) would approach simulating a game of basketball. I was thinking that, at the simplest level, it would just be a series of semi-random actions whose probabilities are based on each player's many statistics - i.e. the probability that a player chooses to shoot is based on x, y, and z. In this fashion, there's no need for any kind of AI, and if you modelled it correctly (how each attribute affects what), I think you could get some decent results - of course there wouldn't be any visuals to go with it. What do you guys think? What's another (better?) way of approaching this problem?- carbThis approach sounds like it completely discards the information about the surroundings. No point guard is going to go for a jump shot if they're at half-court, no matter how good they might be at making field goals. You need to take existing information into account rather than being purely statistical about the process.   ;  The probability of a player electing to shoot should be a function of the player's position, the player's shooting range, the player's opponent/coverage, the player's allocated shots-per-game, the player's position on the depth charts, the shot clock, the game situation (you don't want your fifth-best shooter taking ill-advised Hail Mary's from your end in any situation, much less when down by twelve with two minutes on the clock) and so forth.Oh, yeah, there's AI involved.Simulating basketball is actually quite sophisticated from a top-down perspective. In fact, it may prove impractical to model every single input and evaluate on a per-player basis, so a better solution might be a bottom-up approach that reduces the number of inputs that need to be considered at any point.The most fundamental task it to subdivide a game into a number of states, starting with offense/defense. It stands to reason that when your team is in possession of the ball, you are on offense, and when your opponent has the rock, you're on defense. What about when the ball is loose?Top of the tree: offense/defense/loose-ball tri-state.Because it's the most compact of the top three states, we'll consider it first. In the loose-ball state, the first priority is to get the ball back, the second being to get into scoring position. Since possession of the ball can only be achieved by those actually close to it, however, your logic should probably have players within a certain radius of the ball try to get to it by scrambling, diving, picking and so forth while players outside that radius start to drift toward cutting/scoring lanes such that if a teammate retrieves the ball, he can immediately pass and have the recipient in scoring position. The use of the word drift is important here; if the players outside the radius move too aggressively toward the opponent's basket and then an opposing player retrieves the ball, it leaves their own basket completely unguarded. Consequently, the logic has to find a balance between playing for the score and playing for defense, which should probably be influenced by the speed and offensive skill of the individual players outside the radius.In the defense state we have a number of sub-states: far-baseline-inbound, frontcourt-sideline-inbound, backcourt-sideline-inbound, baseline-inbound, frontcourt, halfcourt, painted-area. Basketball is a positional game, both in terms of how two players react to each other (matchups) and in terms of how offense and defense are (should be) played depending on where on the court the action is taking place. Your logic needs to reflect that.Playing painted-area defense, for example, is largely the responsibility of your big men, though not exclusively, and varies according to the matchup. Ben Wallace guards Shaq very differently than he guards Iverson inside of 15 feet: the one he tries to impede from backing his way to the basket or block on the quick turnaround layup/dunk, the other he tried to bump on a drive and then block his view of the goal and perhaps get a swipe at any released shot. On the flip side, Chauncey Billups would try to stay with AI by moving his feet, hands up, while he might not even try to guard Shaq and concede the point, or try to steal the ball down low if he sees an opening.Each of the other defensive sub-states have similarly variable logic based on the precise actors involved. There are additional specific responses depending on how much time is left on the shot clock, 5-second clock, three-in-the-key count, backcourt clock and game clock: a pesky guard might try to take advantage of an opposing 2-guard or 3 tasked with advancing the ball by hounding him for the steal or at least ensuring he doesn't cross halfcourt within eight seconds while a big man might call for a double- or triple-team on an opposing guard who has picked up his dribble within the arc with mere seconds on the clock. You also have to factor in the defensive scheme which the coach has elected to run - half-court trap, 2-3 zone, 2-1-2 zone, man-to-man, etc.The complexities of playing good defense are such that it becomes apparent both why there are so few good defensive teams in real world basketball at any level anywhere and why video game basketball seems to lack it entirely, aside from code that rewards the CPU with cheap, easier steals from you.On the offensive side of things, you have a variety of locational sub-states just as with defense. In addition, you have two major sub-goals: improve position and score. Improving position includes advancing the ball from backcourt in-bound to front court; displacing a defender to improve separation and thus shot probabilities; using picks, cuts and handoffs to create wide-open or minimally contested scoring opportunities and so forth. Scoring is then a matter of, on the basis of the current position and coverage, selecting a shot - jumpshot, runner, floater, hook, lay-up, dunk, etc - that is most likely to result in a basket.Current basketball simulations do a terrible job at providing means of improving position. For instance, in the real world it is not uncommon to see the use of sudden direction change lead to a temporary loss of balance/footing for just long enough to give the offensive player an open shot or opening for a drive to the basket. Not so in the virtual world, where you have to execute one of the pre-canned crossover animations to displace your opponent. This is an easy way to differentiate your game from the current pack, by embracing the fact that basketball is all about rhythm - luring your opponent into a false one and then enacting instantaneous syncopation to give yourself the advantage.Obviously, I could go on and on laboriously about the sort of decomposition of states you need to do to provide a "realistic" basketball game. That's beside the point, though. What matters is this: by breaking down the totality of possible actions into small groups that are valid choices only within certain states, you simplify your code and game immensely, making it much easier to test, fix and enhance, among other things.Happy hacking.   ;  Thanks Oluseyi - what a great post.I was actually reading a number of your posts on the subject, as you've stated before what an avid sports fanatic you are, though none are as specific as this ;)I began thinking about it more and more, and it makes sense to physically simulate players on a court, rather than model a bunch of probabilistic equations. I suppose the easiest thing is to represent players/ball/basket as circles, and perform a bunch of ray-circle intersection tests to answer questions like ... Is there a defender between me and the basket? Is there an open teammate to pass to? Bigger players with a wide reach might have bigger radii, and so on. I suppose you could go on forever with this.In any case, I'm pretty excited about it all now ;)   