坦克地形交互在我的坦克游戏中不能正常工作

3j86kqsm  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(299)

我已经为这个坦克游戏工作了一段时间了,我已经达到了我想办法让我的坦克与周围环境,主要是地形相互作用的程度。我创建地形的方法是使用一个点列表,由我最喜欢的新地形创建器:噪波贴图生成。但是坦克和地形的相互作用不起作用。
为了让坦克的Angular 本身根据地形,我想尝试使用一个矩形2D来象征我的坦克的踏板,和一个多边形来象征地形(我只使用一块足够大的地形,以适应坦克顶部)。我从坦克当前x位置的地面上的踏板矩形的中心开始。然后,我把这个矩形旋转,让我的程序给我任何可能的位置,踏板可以放置(使用Angular 和y位置)。如果找不到,它会向上移动矩形,然后一次又一次地尝试,直到找到一个打开的位置。
不过,我的问题是,我从程序中得到的只是一个y值,它离地形太远,并且旋转了0度。有什么想法吗?
我的坦克的油漆和运动:

  1. public void paintSelf(Graphics g) {
  2. // never change for each object, so final
  3. final int w1 = (int)mapRange(size, 0, 10, 0, barrel.getWidth()); //80
  4. final int h1 = (int)mapRange(size, 0, 10, 0, barrel.getHeight()); //10
  5. final int w2 = (int)mapRange(size, 0, 10, 0, base.getWidth()); //80
  6. final int h2 = (int)mapRange(size, 0, 10, 0, base.getHeight()); //20
  7. double barAngleRad = Math.toRadians(this.anglePointing);
  8. double basAngleRad = Math.toRadians(this.rotation);
  9. Graphics2D g2d = (Graphics2D)g;
  10. AffineTransform backup = g2d.getTransform();
  11. AffineTransform transBar = new AffineTransform();
  12. AffineTransform transBas = new AffineTransform();
  13. // x, y: the points to rotate around
  14. // this is for the barrel: ignore it
  15. transBar.rotate( barAngleRad, x, y - h2 );
  16. g2d.transform( transBar );
  17. g2d.drawImage(barrel, x - (int)mapRange(size, 0, 10, 0, 21), y - (int)mapRange(size, 0, 10, 0, 4) - h2, w1, h1, null);
  18. g2d.setTransform( backup );
  19. transBas.rotate( basAngleRad, x, y);
  20. g2d.transform( transBas );
  21. g2d.drawImage(base, x - (int)mapRange(size, 0, 10, 0, 34), y - h2, w2, h2, null);
  22. g2d.setTransform( backup ); // restore previous transform
  23. g.setColor(Color.red);
  24. g.drawOval(x, y, 1, 1);
  25. g.drawOval(x, y - h2, 1, 1);
  26. }
  27. public void repositionTankCenter(TerrainFour terrain) {
  28. //this finds the Y on the terrain at the X of the tank,
  29. // creates a rectangle at that Y in the shape of the treads
  30. // on the tank, tests rotations for no intersection with the
  31. // terrain, and once it finds one (or more) it sets the tank's
  32. // Y value to that height and it's angle to that angle -----
  33. // creating variables to use--
  34. final int c_x = this.getX();
  35. int c_y = terrain.getpList(c_x).y + terrain.getY();
  36. final int s_x = (int)(c_x - this.getWidth()/2 - 1);
  37. final int e_x = (int)(c_x + this.getWidth()/2 + 1);
  38. final int tw = (int)(this.getTreadWidth());
  39. final int th = (int)(this.getTreadHeight());
  40. ArrayList<Point2D.Double> angle_heightList = new ArrayList<Point2D.Double>();
  41. final AffineTransform at = new AffineTransform();
  42. Rectangle2D.Double sTreads;
  43. Rectangle2D boundRect;
  44. boolean found = false;
  45. double angle;
  46. int deg;
  47. // terrain polygon
  48. final Polygon T = new Polygon();
  49. for (int i = 0; i < e_x - s_x; i++)
  50. T.addPoint( terrain.getpList(i + s_x).x + terrain.getX(), terrain.getpList(i + s_x).y + terrain.getY());
  51. T.addPoint(e_x, 800);
  52. T.addPoint(s_x, 800);
  53. do {
  54. // treads (of tank) Rectangle
  55. sTreads = new Rectangle2D.Double(c_x - tw/2, c_y - th, tw, th);
  56. System.out.println(c_y);
  57. for (int j = 292; j < 428; j++) {//from 292deg moving right to 68deg
  58. deg = j % 360;
  59. angle = Math.toRadians(deg);
  60. // rotating the treads to a new angle
  61. at.rotate(angle, sTreads.getCenterX(), sTreads.getCenterY());
  62. Shape s = at.createTransformedShape(sTreads);
  63. // rotate back
  64. at.rotate(-angle, sTreads.getCenterX(), sTreads.getCenterY());
  65. // creating a rectangle with the new position
  66. boundRect = s.getBounds2D();
  67. //comparing the treads' new position to the polygon:
  68. // if it doesn't intercect, then a position is found
  69. // and added to a list
  70. if (!T.intersects(boundRect) ) {
  71. angle_heightList.add( new Point2D.Double(angle, boundRect.getCenterY()) );
  72. found = true;
  73. }
  74. }
  75. c_y--;
  76. } while (!found);
  77. //gets the correct position and sets the tank's
  78. // angle and Y value ------------------------
  79. int a = (int)(angle_heightList.size() / 2);
  80. this.setRotation( angle_heightList.get(a).x );
  81. this.setY( (int)angle_heightList.get(a).y );
  82. }

这是我认为必要的地形

  1. public class TerrainFour {
  2. private int x, y, width, minHeight, maxHeight, variance;
  3. private int numberOfPoints;
  4. private double distBetweenPoints;
  5. private ArrayList<Point> pList;
  6. private FBM fbm;
  7. public TerrainFour(int x, int y, int width, int minHeight, int maxHeight, double distBetweenPoints, int variance) {
  8. this.x = x;
  9. this.y = y;
  10. this.width = width;
  11. this.minHeight = minHeight;
  12. this.maxHeight = maxHeight;
  13. this.numberOfPoints = width + 2;
  14. this.distBetweenPoints = distBetweenPoints - 1;
  15. this.variance = variance;
  16. createTerrain();
  17. }
  18. //how I learned about Noise Generation (and FBM)
  19. // https://rtouti.github.io/graphics/perlin-noise-algorithm
  20. private void createTerrain() {
  21. int a, b;
  22. //creates points and checks against variance because I
  23. // like to have a more interesting (height-wise) terrain
  24. // -- currently prefer 180 as the variance -----------
  25. do {
  26. a = minHeight;
  27. b = maxHeight;
  28. generatePoints();
  29. for (int i = 0; i < pList.size(); i++) {
  30. if (pList.get(i).y > a)
  31. a = pList.get(i).y;
  32. if (pList.get(i).y < b)
  33. b = pList.get(i).y;
  34. }
  35. } while (a - b < variance);
  36. }
  37. private void generatePoints() {
  38. pList = new ArrayList<Point>();
  39. //an FBM object represents fractial brownian motion - basically
  40. // means you layer incrementally advanced noisemaps on top of
  41. // each other to create a more realistic (natural) terrain
  42. // I've been using 7 octaves ---------------------------
  43. fbm = new FBM(7, 21234998, 0.7);
  44. int s = (int)(Math.random() * 100000);
  45. for (double t = 0.01 + s; t < numberOfPoints * 0.01 + s; t += 0.01) {
  46. // fbm works with 2Dnoise.. I don't need 2D so I gave t twice
  47. double a = fbm.noise(t, t);
  48. a = mapRange(a, -0.1, 1.25, minHeight, maxHeight);
  49. pList.add(new Point((int)((t - s)/0.01 + distBetweenPoints), (int)a));
  50. }
  51. }
  52. private static double mapRange(double value, double low1, double high1, double low2, double high2) {
  53. return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
  54. }

如果有人能帮忙的话,我会非常感激,如果我没有提供足够的信息(代码)来解决这个问题,请告诉我,我会补充更多。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题