You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.8 KiB

  1. package collider;
  2. import gameobjects.*;
  3. import org.apache.logging.log4j.Logger;
  4. import org.apache.logging.log4j.LogManager;
  5. /** a {@link Collider} for Rectangles, usually used for {@link RectObject} instances.
  6. * @see gameobjects.RectObject#generateColliders()
  7. */
  8. public class RectCollider extends Collider {
  9. //double x;
  10. //double y;
  11. double w, h;
  12. private static Logger logger = LogManager.getLogger(RectCollider.class);
  13. /**
  14. * initializes this RectCollider.
  15. * calls superclass constructor of {@link Collider#Collider(String, GameObject)} with params String id and GameObject o.
  16. *
  17. * @param id String unique name for this RectCollider
  18. * @param o GameObject instance this RectCollider belongs to (cannot be null)
  19. * @param w width in pixels for the collider dimensions (> 0)
  20. * @param h height in pixels for the collider dimensions (>0)
  21. */
  22. public RectCollider(String id, GameObject o, double w, double h) {
  23. super(id, o);
  24. this.w = w;
  25. this.h = h;
  26. }
  27. public String toString() {
  28. return " " + w + " " + h + " ";
  29. }
  30. /**
  31. * checks collision with other Collider, which needs to be a RectCollider, too.
  32. * @param other RectCollider (is casted) to calculate collision with
  33. * @return true if collission is detected
  34. */
  35. public boolean checkCollisionRectRect(Collider other) {
  36. RectCollider r1 = this;
  37. RectCollider r2 = (RectCollider) other;
  38. if ((((r1.getX() + r1.w / 2.) >= (r2.getX() - r2.w / 2.)) && ((r1.getX() + r1.w / 2.) <= (r2
  39. .getX() + r2.w / 2.)))
  40. || (((r2.getX() + r2.w / 2.) >= (r1.getX() - r1.w / 2.)) && ((r2.getX() + r2.w / 2.) <= (r1
  41. .getX() + r1.w / 2.)))) {
  42. if ((((r1.getY() + r1.h / 2.) >= (r2.getY() - r2.h / 2.)) && ((r1.getY() + r1.h / 2.) <= (r2
  43. .getY() + r2.h / 2.)))
  44. || (((r2.getY() + r2.h / 2.) >= (r1.getY() - r1.h / 2.)) && ((r2.getY() + r2.h / 2.) <= (r1
  45. .getY() + r1.h / 2.)))) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. /**
  52. * checks collision with other Collider, which needs to be a CircleCollider
  53. * @param other CircleCollider (is casted) to calculate collision with
  54. * @return true if collission is detected
  55. */
  56. public boolean checkCollisionRectCirc(Collider other) {
  57. RectCollider r = this;
  58. CircleCollider c = (CircleCollider) (other);
  59. double circleDistX = Math.abs(c.getX() - (r.getX() ));
  60. double circleDistY = Math.abs(c.getY() - (r.getY() ) );
  61. logger.trace("c.x:"+c.x+" "+"c.y:"+c.y+" "+"c.r:"+c.r+" "+"r.x:"+r.getX()+" "+"r.y:"+r.getY()+" "+"r.w:"+r.w+" "+"r.h:"+r.h+" "+"circleDistX:"+circleDistX+" "+"circleDistY:"+circleDistY);
  62. if (circleDistX > (r.w / 2 + c.r))
  63. return false;
  64. if (circleDistY > (r.h / 2 + c.r))
  65. return false;
  66. if (circleDistX <= (r.w / 2)) {
  67. logger.trace("Collision Rect with circle");
  68. return true;
  69. }
  70. if (circleDistY <= (r.h / 2)) {
  71. logger.trace("Collision Rect with circle (second)");
  72. return true;
  73. }
  74. double cornerDistSqr = Math.pow(circleDistX - r.w / 2, 2) + Math.pow(circleDistY - r.h / 2, 2); // Satz
  75. // des
  76. // Pythagoras
  77. return (cornerDistSqr <= c.r * c.r); // falls true zurueckgegeben: Kollision
  78. }
  79. @Override
  80. public boolean collidesWith(Collider other) {
  81. // rect circ
  82. try {
  83. return checkCollisionRectCirc(other);
  84. } catch (Exception e) {
  85. // do nothing
  86. }
  87. // rect rect
  88. try {
  89. return checkCollisionRectRect(other);
  90. } catch (Exception e) {
  91. // do nothing
  92. }
  93. try {
  94. return other.collidesWith(this);
  95. } catch (Exception e) {
  96. // do nothing
  97. }
  98. throw new RuntimeException("Collider type not implemented!");
  99. }
  100. }