Buggy Implementation #4: My implementation works, except for the line-shape intersect calculation. It works most of the time, except sometimes it iterates farther and farther from the actual intersection. The code is: double x=getX() + Math.sin(e.getBearingRadians()+getHeadingRadians())*e.getDistance(); double y=getY() + Math.cos(e.getBearingRadians()+getHeadingRadians())*e.getDistance(); Point2D.Double enemyPosition=new Point2D.Double(x,y); Point2D.Double myPosition=new Point2D.Double(getX(),getY()); double ang=Math.asin((e.getVelocity()*Math.sin(e.getHeadingRadians()-(e.getBearingRadians()+getHeadingRadians())))/(20-(3*3))); Line2D.Double enemyMovement=new Line2D.Double(enemyPosition, projectPoint(enemyPosition,e.getHeadingRadians(),2000*sign(e.getVelocity()))); Line2D.Double bulletMovement=new Line2D.Double(myPosition, projectPoint(myPosition,getHeadingRadians() + e.getBearingRadians()+ang,2000)); dist=new Line2D.Double(myPosition, enemyPosition); //Tango's lineintersect method Point2D.Double intersect=new Point2D.Double(0,0);//point you are actually firing at double num = (enemyMovement.getY2()-enemyMovement.getY1())*(enemyMovement.getX1()-bulletMovement.getX1()) - (enemyMovement.getX2()-enemyMovement.getX1())*(enemyMovement.getY1()-bulletMovement.getY1()); double denom = (enemyMovement.getY2()-enemyMovement.getY1())*(bulletMovement.getX2()-bulletMovement.getX1()) - (enemyMovement.getX2()-enemyMovement.getX1())*(bulletMovement.getY2()-bulletMovement.getY1()); intersect.x = bulletMovement.getX1() + (bulletMovement.getX2()-bulletMovement.getX1())*num/denom; intersect.y = bulletMovement.getY1() + (bulletMovement.getY2()-bulletMovement.getY1())*num/denom; out.println("NoWalls : " + intersect); //nanos iterative lineintersectwithshape method if(!field.contains(intersect)){ //is where you are firing at in the battlefield? int TRACE_DEPTH = 20; Point2D middle; boolean containsFirst; if ((containsFirst = field.contains(enemyMovement.getP1())) == field.contains(enemyMovement.getP2())){ } for (int i = 0; i < TRACE_DEPTH; i++) { out.println("Iteration " + i); middle = new Point2D.Double((enemyMovement.getX1() + enemyMovement.getX2()) / 2, (enemyMovement.getY1() + enemyMovement.getY2()) / 2); if (containsFirst != field.contains(middle)){ enemyMovement = new Line2D.Double(enemyMovement.getP1(), middle); }else{ enemyMovement = new Line2D.Double(middle, enemyMovement.getP2()); } out.println(((enemyMovement.getX1() + enemyMovement.getX2()) / 2) + "," + ((enemyMovement.getY1() + enemyMovement.getY2()) / 2)); } out.println("Out of loop"); intersect.x=(enemyMovement.getX1() + enemyMovement.getX2()) / 2; intersect.y=(enemyMovement.getY1() + enemyMovement.getY2()) / 2; } ang=Math.atan2(intersect.getX()-myPosition.getX(),intersect.getY()-myPosition.getY()); setTurnGunRightRadians(Utils.normalRelativeAngle(ang-getGunHeadingRadians())); setFire(3); Of course, you would replace the three with the bullet power you're using. But help with the intersection? (It's in debug mode btw, which is why it's printing out all the text) --Starrynte
|