我正在使用多边形进行碰撞检测方法。我有一块岩石,朝着一定方向移动。
当我运行应用程序时,多边形将其呈现到一个完美的矩形中,当时岩石在启动时就在其中。
然而,它不是用 rock移动。
public Polygon box;
private int width;
public int height;
public float [] vertices;
public Rock (float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(x, y);
velocity = new Vector2(20, 0);
vertices = new float[] {position.x, position.y, position.x+width, position.y, position.x+width, position.y+height,
position.x, position.y+height};
box = new Polygon ();
box.setOrigin(width/2, height/2);
}
public void RockMove (float delta) {
position.add(velocity.cpy().scl(delta));
box.setVertices(vertices);
box.setPosition(position.x, position.y);
}
岩石的精灵批次
public void render (float delta) {
batch.begin();
batch.enableBlending();
batch.draw(rock, rock.GetX(), rock.GetY(), rock.GetWidth(), rock.GetHeight());
batch.disableBlending();
batch.end();
}
这是渲染类中的一些代码
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(Color.RED);
shapeRenderer.polygon(Rock.vertices);
shapeRenderer.end();
再次,多边形显示出完美的表现,但不会移动。我没有任何错误。
编辑:我在岩石移动方法中再次定义了顶点,现在它们与岩石一起移动。
public void RockMove (float delta) {
position.add(velocity.cpy().scl(delta));
box.setVertices(vertices);
box.setPosition(position.x, position.y);
vertices = new float[] {position.x, position.y, position.x+width,
position.y, position.x+width, position.y+height,
position.x, position.y+height};
}
但由于某种原因,碰撞检测行不通。
不要在更新中再次定义顶点,而不是您应该使用polygon的转换版。
Polygon polygon=new Polygon();
float transformed[] = polygon.getTransformedVertices();
变换ventices是缩放,旋转和位置翻译后多边形的顶点。