The kick is central to the HaxBall experience. In building HaxArena, some complex problems arise from this apparently simple mechanic. In this post, I will attempt to contrast HaxBall and HaxArena with respect to the kick. As always, all questions and comments are welcome.
HaxArena players and ball are modelled as circles. Detecting whether a kicking player is able to kick the ball is theoretically simple: check the distance between the player’s centre and the ball’s centre; if this distance is equal to the sum of the radii, they are considered touching and the ball should be shot.
There are a few problems with this approach. The first one is about rounding error: the distance will never exactly equal the sum of the radii. The second one is about playability: if you lift your finger off the move button for a split second and then try to shoot, the ball will roll off your player ever so slightly, and the shot will not happen.
To mitigate these effects, we consider the ball to be shot if the distance between the bodies is within a certain small threshold. The value of this threshold has to be determined by trial and error and I invite you fine people to help me out with that.
The effect of a successful shot is that an impulse force is applied to the ball from the direction of the shooting player. The ball blasts off in the desired direction.
Because a player needs to be “close enough” to a ball to be considered shooting it, it’s possible for the impulse force to be applied on more than one successive frame, resulting in a ridiculously powerful shot. Obviously this is not desired: only a single impulse force should be applied per contact. How can we achieve this?
HaxBall solves it by clearing the player’s kicking state whenever a successful ball kick is detected. If a player wants to kick the ball more than once, he will have to release the kick key and press it again. To produce a rapid-fire effect, the player has to spam the “x” key as quickly as he can.
HaxBallers are clever people, so what do they do? They install macro hacks that simulate rapid key presses and releases in the Flash client. Such a player can now usually out-muscle a player who is spamming the “x” key manually. Is this ethical? Cheating? You decide.
In HaxArena, I’m proposing to let the player simply hold down the “x” key, but when a successful kick is detected, the player gets a 100ms (or so) kick cool down, during which the player cannot kick the ball again. When the cool down expires, another successful kick can be detected. In this way, everybody starts out on a level playing field and nobody has to hack the client.
There is one drawback that I can see with this approach: maybe the HaxBall kick-reset idea is actually preferred in some cases, and now it would be impossible. I’m not pro enough to know if this is a problem, what do you think?
A rocket (called other things in other places) is when two players kick the ball at the same (or nearly the same) time, producing a very powerful shot. This idea is still possible in HaxArena, since each player’s kick cool down is tracked separately.
A double wall bounce — there’s probably a better term for this but you get the idea — is when a player kicks the ball off the wall towards himself, and then kicks it again almost immediately downfield. This is similar to a rocket, except that the two impulses are provided by the same player. In HaxArena, it is possible to execute this maneuver by simply holding down “x”, but it takes some practice to figure out the timing and the proper angle of attack.
It has been observed in the comments that letting the ball roll closer to your player before shooting produces a stronger shot. Let me share my observations from HaxBall and HaxArena alike on that subject.
First, if you nudge the ball forward by moving before taking the shot, the result is more powerful. This is because the ball is already moving with positive momentum when the impulse is applied.
Second, if the ball is coming toward you when you take the shot, the result is weak indeed. This is because the impulse is added to the negative speed. If, for example, a red player takes a shot, and a blue player makes a save with the “x” key, the ball will roll ever so slowly away from the blue player. This is because of the small amount of speed that the ball lost in flight after being shot by the red player.
So I think the observation may amount to this: if the ball is rolling towards you, you can produce a stronger shot by first letting it hit you, then shooting. This is because the ball will then pick up two impulses: one from the collision with you, and a second from the shot. I don’t know if simply closing the distance between the player and the ball actually increases the strength of the shot. I also don’t know if the player’s velocity at the time of impact affects the impulse force applied to the ball by a successful kick. Please share your experience!
When I get proper physics simulations out, I invite everyone to try out these ideas, and let me know how they compare to HaxBall. My aim, as I have said many times, is to reproduce HaxBall as closely as possible so that your long hours of experience will not go to waste.