Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.44 KB | None | 0 0
  1. import java.awt.geom.Point2D;
  2. import java.util.Random;
  3.  
  4.  
  5. public class Robutton {
  6.    
  7.     final static int radius = 10;
  8.    
  9.     private double directionDegree;
  10.     private double directionX;
  11.     private double directionY;
  12.     Point2D.Double pos;
  13.    
  14.     private Coin coin = null;
  15.    
  16.     private boolean collisionDetected = false;
  17.     private double collisionTime = 2.0;
  18.     private double nextDegree;
  19.     private Point2D.Double nextPos;
  20.     private Coin nextCoin = null;
  21.    
  22.    
  23.    
  24.     public Robutton(Point2D.Double pos) {
  25.         this.pos = pos;
  26.     }
  27.    
  28.    
  29.    
  30.     public double distance(Robutton rob) {
  31.         return pos.distance(rob.getPos())-radius*2;
  32.     }
  33.    
  34.    
  35.    
  36.     public void setDirectionDegree(double degree) {
  37.         directionDegree = degree % 360;
  38.  
  39.         directionX = Math.cos(Math.toRadians(directionDegree));
  40.         directionY = -Math.sin(Math.toRadians(directionDegree));
  41.     }
  42.    
  43.    
  44.    
  45.     public void move(double time) {
  46.         pos.x += directionX*time;
  47.         pos.y += directionY*time;
  48.         if (coin != null) {
  49.             coin.pos.x = this.pos.x;
  50.             coin.pos.y = this.pos.y;
  51.         }
  52.         if (collisionTime == time) {
  53.             this.setDirectionDegree(nextDegree);
  54. //            System.out.println("nextDegree set to " + nextDegree);
  55.             this.coin = nextCoin;
  56.         }
  57.         collisionTime = 2.0;
  58.         if (pos.x > 190 || pos.y > 190)
  59.             System.out.println("fail6");
  60.     }
  61.    
  62.    
  63.    
  64.     public void moveSimple() {
  65.         pos.x += directionX;
  66.         pos.y += directionY;
  67.         if (coin != null) {
  68.             coin.pos.x = this.pos.x;
  69.             coin.pos.y = this.pos.y;
  70.         }
  71.     }
  72.    
  73.    
  74.     public void moveSimple(double xdelta, double ydelta) {
  75.         pos.x += xdelta;
  76.         pos.y += ydelta;
  77.         if (coin != null) {
  78.             coin.pos.x = this.pos.x;
  79.             coin.pos.y = this.pos.y;
  80.         }
  81.     }
  82.    
  83.    
  84.    
  85. //    public void collideBound(int x, int y) {
  86. //        if (pos.x - radius < 0) {
  87. //            setDirectionDegree((360 - directionDegree + 180));
  88. ////            directionX = -directionX;
  89. //        }
  90. //        else if (pos.x + radius > x) {
  91. //            setDirectionDegree((360 - directionDegree + 180));
  92. ////            directionX = -directionX;
  93. //        }
  94. //        if (pos.y - radius < 0) {
  95. //            setDirectionDegree(360 - directionDegree);
  96. ////            directionY = -directionY;
  97. //        }
  98. //        else if (pos.y + radius > y) {
  99. //            setDirectionDegree(360 - directionDegree );
  100. ////            directionY = -directionY;
  101. //        }
  102. //    }
  103.    
  104.     public double collideBound(int x, int y, double time) {
  105.         double nextX = pos.x + directionX * time;
  106.         double nextY = pos.y + directionY * time;
  107.         double collisionTimeX = 2.0, collisionTimeY = 2.0;
  108.         if (nextX - radius < 0) {
  109.             collisionTimeX = (pos.x - radius) / (-directionX);
  110.             nextDegree = 360 - directionDegree + 180;
  111.         }
  112.         else if (nextX + radius > x) {
  113.             collisionTimeX = (x - radius - pos.x) / (directionX);
  114.             nextDegree = 360 - directionDegree + 180;
  115.         }
  116.         if (nextY - radius < 0) {
  117.             collisionTimeY = (pos.y - radius) / (-directionY);
  118.             nextDegree = 360 - directionDegree;
  119.         }
  120.         else if (nextY+ radius > y) {
  121.             collisionTimeY = (y - radius - pos.y) / (directionY);
  122.             nextDegree = 360 - directionDegree;
  123.         }
  124.         collisionTime = Math.min (collisionTimeX, collisionTimeY);
  125. //        System.out.println("time: " + collisionTime);
  126.         if (time < 0)
  127.             System.out.println("fail4");
  128.         if ((this.pos.x + directionX * collisionTime > 190 || this.pos.y + directionY * collisionTime > 190) && collisionTime < 2.0) {
  129.             System.out.println((this.pos.x + directionX * collisionTime) + " " + (this.pos.y + directionY * collisionTime));
  130.             System.out.println("fail5");
  131.         }
  132.         return collisionTime;
  133.     }
  134.    
  135.    
  136.     public void collideBoundSimple(int x, int y) {
  137.         if (pos.x - radius < 0) {
  138.             nextDegree = 360 - directionDegree + 180;
  139.         }
  140.         else if (pos.x + radius > x) {
  141.             nextDegree = 360 - directionDegree + 180;
  142.         }
  143.         if (pos.y - radius < 0) {
  144.             nextDegree = 360 - directionDegree;
  145.         }
  146.         else if (pos.y+ radius > y) {
  147.             nextDegree = 360 - directionDegree;
  148.         }
  149.         setDirectionDegree(nextDegree);
  150.     }
  151.    
  152.    
  153.    
  154.     public double collideRobutton(Robutton rob, double time) {
  155.        
  156.         double currDistance = this.distance(rob);
  157.         if (currDistance > 2) return time;
  158.        
  159.         Point2D.Double thisNextPos = new Point2D.Double (this.pos.x + this.directionX * time, this.pos.y + this.directionY * time);
  160.         Point2D.Double robNextPos = new Point2D.Double (rob.pos.x + rob.directionX * time, rob.pos.y + rob.directionY * time);
  161.        
  162.         double nextDistance = thisNextPos.distance(robNextPos)-radius*2;
  163.        
  164. //        System.out.println("curr: " + currDistance);
  165. //        System.out.println("next: " + nextDistance);
  166.         if (currDistance < -(1e-6) || currDistance > 400)
  167.             System.out.println("fail2");
  168.         if (nextDistance < (1e-6d)) {
  169.             double beta, gamma;
  170. //            double tmp = (currDistance/(currDistance-nextDistance))*0.99;
  171.             double tmp = movingSphereIntersectsMovingSphere(this.pos.x, this.pos.y, this.directionX, this.directionY, radius,
  172.                                                             rob.pos.x, rob.pos.y, rob.directionX, rob.directionY, radius)*0.99;
  173.             if (tmp <= 0) return time;
  174.             rob.collisionTime = tmp;
  175.             this.collisionTime = tmp;
  176.             Random rnd = new Random();
  177.             Point2D.Double thisNextRealPos = new Point2D.Double (this.pos.x + this.directionX * tmp, this.pos.y + this.directionY * tmp);
  178.             Point2D.Double robNextRealPos = new Point2D.Double (rob.pos.x + rob.directionX * tmp, rob.pos.y + rob.directionY * tmp);
  179.             do {
  180.                 this.nextDegree = (directionDegree+180+rnd.nextInt(180)-90)%360;
  181.                 rob.nextDegree = (rob.directionDegree+180+rnd.nextInt(180)-90)%360;
  182.                 double alpha = Math.toDegrees(Math.atan2(thisNextRealPos.y-robNextRealPos.y, robNextRealPos.x-thisNextRealPos.x));
  183.                 if (alpha < 0) alpha+=360;
  184.                
  185.                 gamma = this.nextDegree - alpha;
  186.                 beta = rob.nextDegree - alpha;
  187.                 if (gamma < 0) gamma = 360-gamma;
  188.                 if (beta < 0) beta = 360-beta;
  189.                 beta %= 360;
  190.                 gamma %= 360;
  191.             } while(!(beta < gamma || beta > 360-gamma));
  192.             System.out.println("new time: " + tmp);
  193.             if (tmp < 0)
  194.                 System.out.println("fail");
  195.             return tmp;
  196.         }
  197.         return time;
  198.     }
  199.    
  200.    
  201.     public void collideRobuttonSimple(Robutton rob) {
  202.         double xdistance = this.pos.x - rob.pos.x;
  203.         double ydistance = this.pos.y - rob.pos.y;
  204.         double distance = Math.sqrt(xdistance*xdistance + ydistance*ydistance);
  205.         if (distance < this.radius*2) {
  206.             double xtrans = xdistance * (20-distance)/(distance);
  207.             double ytrans = ydistance * (20-distance)/(distance);
  208.             this.pos.x += xtrans*0.5;
  209.             rob.pos.x -= xtrans*0.5;
  210.             this.pos.y += ytrans*0.5;
  211.             rob.pos.y -= ytrans*0.5;
  212.             Random rnd = new Random();
  213.             double beta, gamma;
  214.             do {
  215.                 this.nextDegree = (directionDegree+180+rnd.nextInt(180)-90)%360;
  216.                 rob.nextDegree = (rob.directionDegree+180+rnd.nextInt(180)-90)%360;
  217.                 double alpha = Math.toDegrees(Math.atan2(this.pos.y-rob.pos.y, rob.pos.x-this.pos.x));
  218.                 if (alpha < 0) alpha+=360;
  219.                
  220.                 gamma = this.nextDegree - alpha;
  221.                 beta = rob.nextDegree - alpha;
  222.                 if (gamma < 0) gamma = 360-gamma;
  223.                 if (beta < 0) beta = 360-beta;
  224.                 beta %= 360;
  225.                 gamma %= 360;
  226.             } while(!(beta < gamma || beta > 360-gamma));
  227.         }
  228.         this.setDirectionDegree(this.nextDegree);
  229.         rob.setDirectionDegree(rob.nextDegree);
  230.     }
  231.    
  232.    
  233.    
  234.     private double movingSphereIntersectsMovingSphere(double center1X,
  235.             double center1Y, double speed1X, double speed1Y, double radius1,
  236.             double center2X, double center2Y, double speed2X, double speed2Y,
  237.             double radius2) {
  238.  
  239.          // Rearrange the parameters
  240.          double centerX = center1X - center2X;
  241.          double centerY = center1Y - center2Y;
  242.          double speedX = speed1X - speed2X;
  243.          double speedY = speed1Y - speed2Y;
  244.          double radius = radius1 + radius2;
  245.          double radiusSq = radius * radius;
  246.          double speedXSq = speedX * speedX;
  247.          double speedYSq = speedY * speedY;
  248.          double centerXSq = centerX * centerX;
  249.          double centerYSq = centerY * centerY;
  250.  
  251.          double termBsq4ac = (radiusSq - centerYSq) * speedXSq + (radiusSq - centerXSq)
  252.                * speedYSq + 2.0 * speedX * speedY * centerX * centerY;
  253.          double termMinusB = -speedX * centerX - speedY * centerY;
  254.          double term2a = speedXSq + speedYSq;
  255.  
  256.          if (termBsq4ac < 0) {
  257.             // No intersection
  258.             // Moving spheres may cross at different times, or move in parallel.
  259.             return -1;
  260.          } else {
  261.             // Discard negative t (hit if moving backwards)
  262.             // If two positive t, accept the smallest. The larger hits at the
  263.             //  opposite side.
  264.             termBsq4ac = Math.sqrt(termBsq4ac);
  265.             double sol1 = (termMinusB + termBsq4ac) / term2a;
  266.             double sol2 = (termMinusB - termBsq4ac) / term2a;
  267.             // Check solution
  268.             if (sol1 > 0 && sol2 > 0) {
  269.                return Math.min(sol1, sol2);
  270.             } else if (sol1 > 0) {
  271.                return sol1;
  272.             } else if (sol2 > 0) {
  273.                return sol2;
  274.             } else {
  275.                return -1; // both solutions negative
  276.             }
  277.          }
  278.       }
  279.    
  280.    
  281.     public double collideCoin(Coin coin, double time) {
  282.         Point2D.Double thisNextPos = new Point2D.Double (this.pos.x + this.directionX * time, this.pos.y + this.directionY * time);
  283.        
  284.         if (thisNextPos.distance(coin.pos) < Robutton.radius + Coin.radius) {
  285.             double tmp = movingSphereIntersectsMovingSphere(this.pos.x, this.pos.y, this.directionX, this.directionY, radius,
  286.                     coin.pos.x, coin.pos.y, 0, 0, Coin.radius)*0.99;
  287.             this.collisionTime = tmp;
  288.             this.nextCoin = coin;
  289.             return tmp;
  290.         }
  291.         return time;
  292.     }
  293.    
  294.    
  295.     public void collideCoinSimple(Coin coin) {
  296.         if (this.pos.distance(coin.pos) < this.radius + coin.radius) {
  297.             this.coin = coin;
  298.         }
  299.    }
  300.    
  301.    
  302.     public void setAfterCollisionDirection() {
  303.         this.setDirectionDegree(nextDegree);
  304.     }
  305.    
  306.    
  307.    
  308.     public Point2D.Double getPos() {
  309.         return pos;
  310.     }
  311.    
  312.    
  313.     public int getXInt() {
  314.         return (int) pos.x;
  315.     }
  316.    
  317.    
  318.    
  319.     public int getYInt() {
  320.         return (int) pos.y;
  321.     }
  322.  
  323.  
  324.  
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement