Lemm Ras
Lemm Ras

Reputation: 1032

Paddle collision side detection using Graphics

I've implemented a kind of Pong where the paddles (Rectangle2D) can rotate. To obtain more accuracy, many things are managed by Graphics2D. The rotation too is managed by the methods rotate(...) of the previous told class.

To reach a realistic bounce, I need to know where the ball hits the paddle (only the side, not the particular point).

I've tried to define (and rotate) two Rectangle2D that represent the back and the front side of the paddle and then recognize the bounce in one of these two by the method hit(Rectangle2D r, Shape s, boolean onStroke), but it doesn't work properly.

Here is the java class Graphics2D:

http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html

Have you any idea?

Upvotes: 1

Views: 194

Answers (1)

JavaHeapSpace
JavaHeapSpace

Reputation: 26

Assuming you have one rectangle for your paddle and know the center of rectangle and circle and the rotation of your rectangle. Assuming a rotation of zero means, your rectangle is aligned horizontally (width > height).

  • Calculate the difference vector (center of circle) - (center of rectangle)
  • Get the angle of that vector and subtract the rotation of your rectangle (angle of vector is Math.atan2(y, x))
  • The resulting value a tells you the relative direction of your circle
  • Make sure a lies between 0 and 2*pi

q = Math.atan2(height of rectangle, width of rectangle)

  • If a is between q and pi-q, your circle has hit on the front long (upper) side.
  • If a is lower than q or bigger than 2*pi-q it's on the right side.
  • If a is between pi-q and pi+q it's on the left side.
  • If a is between pi+q and 2pi-q it's hit the bottom of your rectangle

Upvotes: 1

Related Questions