I am programming a side-view tennis game, inspired by an old arcade game, using Javscript and HTML5 canvas elements. The player can move left and right and holds a racket at arms length which can be rotated 360 degrees around the shoulder joint.
I am quite happy with the collision detection and resulting deflection angle between the racket and the ball in the case where the player stands still and the racket does not move. I use a ray-casting approach for this collision detection between the vector that represents the racket and the trajectory vector for the ball as proposed here.
However, I am having trouble implementing the collision detection between the rotating racket and the ball. When the racket is swung, the area in which a collision would apply has a shape similar to a circular segment , but the racket rotates rotates to fast and the collision detection does not pick it up in most cases. The image bellow illustrates the problem, the red arrow indicates the direction of the swinging racket.
The following code snippet from the player entity's update function shows how the racket is updated. arcmx
and armcy
is the location of the shoulder joint, rstart_
and rend_
are the beginning and end of the racket.
if(KEY_STATUS.rleft || KEY_STATUS.rright) {
if(KEY_STATUS.rright) {
this.rangle += this.rspeed;
} else if (KEY_STATUS.rleft) {
this.rangle -= this.rspeed;
}
}
this.armcx = this.x + this.width/2;
this.armcy = this.y + 46;
this.rstartx = this.armcx + Math.cos(this.rangle)*40;
this.rstarty = this.armcy + Math.sin(this.rangle)*40;
this.rendx = this.armcx + Math.cos(this.rangle)*71;
this.rendy = this.armcy + Math.sin(this.rangle)*71;
I am pretty lost on this problem and appreciate any hints on how to approach it.