Java 不同对象的坐标错误
简言之: 我使用以下方法创建多边形对象:Java 不同对象的坐标错误,java,libgdx,coordinates,coordinate-systems,Java,Libgdx,Coordinates,Coordinate Systems,简言之: 我使用以下方法创建多边形对象: public static float[][] getPolygonArrays(float cx, float cy, float R, int sides) { float[] x = new float[sides]; float[] y = new float[sides]; double thetaInc = 2 * Math.PI / sides; double theta
public static float[][] getPolygonArrays(float cx, float cy, float R, int sides) {
float[] x = new float[sides];
float[] y = new float[sides];
double thetaInc = 2 * Math.PI / sides;
double theta = (sides % 2 == 0) ? thetaInc : -Math.PI / 2;
for (int j = 0; j < sides; j++) {
x[j] = (float) (cx + R * Math.cos(theta));
y[j] = (float) (cy + R * Math.sin(theta));
theta += thetaInc;
}
return new float[][]{x, y};
}
游戏世界代码:
公共类游戏世界{
私有ArrayListplatforms=新ArrayList();
私人正交摄影机;
私人舞台;
私有数组activeFigures;
私人池<图>图池;
private long lastFigureTime=TimeUtils.nanoTime();
公共游戏世界(){
setCamera(新的正交摄影机());
getCamera().setToOrtho(true,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
设置阶段(新阶段());
getStage().setViewport(新屏幕视口(getCamera());
初始化工具();
createPlatforms();
getStage().addListener(新的InputListener()){
公共布尔接地(InputEvent事件、浮点x、浮点y、整数指针、整数按钮){
浮动度=配置平台旋转度;
if(x0){
平台平台=getPlatforms().get(i);
如果(!platform.isLocked()&&platform.isRotatable()&&platform.getShape().getPolygon()包含(x,y)){
platform.addAction(Actions.rotateBy(度数,1,插值.反弹));
打破
}
}
返回true;
}
});
setInputProcessor(getStage());
}
私有void initializePools(){
setActiveFigures(新数组());
setFiguresPool(新池(){@Override
受保护的图形newObject(){
返回新图形();
}
});
}
私有void createPlatforms(){
float max=Gdx.graphics.getHeight()/(Gdx.graphics.getWidth()/(Gdx.graphics.getWidth()/2));
float x=Gdx.graphics.getWidth()/2;
float y=Gdx.graphics.getHeight()/2;
浮动侧=10f;
颜色=颜色。红色;
createPlatform(x,y,max*Config.THIRD_PLATFORM_RADIUS_MULTIPLIER,sides,color,true,false,false,null);
}
私有平台createPlatform(浮动x、浮动y、浮动半径、浮动边、颜色、布尔可旋转、布尔锁定、布尔isEmpty、平台相关){
平台平台=新平台(0,0,半径,边,颜色,isEmpty,this);
平台移动到(x,y);
平台。可旋转(可旋转);
平台。设置锁定(锁定);
getPlatforms().add(平台);
getStage().addActor(平台);
if(relatedTo!=null){
Related to.AddRelationship(平台);
}
返回平台;
}
专用地物createFigure(浮动x、浮动y){
Figure=getFiguresPool().Get();
figure.init(这个,0,0,10f,4f,颜色。深灰色);
图.移动到(x,y);
getActiveFigures().add(图);
getStage().addActor(图);
返回数字;
}
公众人物({
如果(getActiveFigures().size>=10)返回;
如果(TimeUtils.nanoTime()-getLastFigureTime()=0;){
figure=getActiveFigures().get(i);
if(图.isAlive()==false){
getActiveFigures().removeIndex(i);
getFiguresPool().free(图);
}否则{
图1.更新(增量);
}
}
}
公共空心调整大小(整型宽度、整型高度){
getCamera().setToOrTo(真、宽、高);
getStage().getViewport().update(宽度、高度、真值);
for(平台:getPlatforms()){
平台。调整大小(真实、宽度、高度);
}
对于(图:getActiveFigures()){
图.调整大小(真、宽、高);
}
}
}
游戏渲染器代码:
公共类游戏渲染器{
私人shaperender shaperender;
私人游戏世界;
私人SpriteBatch SpriteBatch;
公共游戏渲染器(游戏世界){
setWorld(世界);
设置ShaperEnder(新的ShaperEnder());
getShapeRenderer().setProjectionMatrix(getWorld().getCamera().combined);
setSpriteBatch(新SpriteBatch());
getSpriteBatch().setProjectionMatrix(getWorld().getCamera().combined);
}
公共无效呈现(){
Gdx.gl.glClearColor(0f、0.2f、0.4f、1f);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
getWorld().getCamera().update();
getSpriteBatch().setProjectionMatrix(getWorld().getCamera().combined);
getShapeRenderer().setProjectionMatrix(getWorld().getCamera().combined);
getWorld().getStage().act(Gdx.graphics.getDeltaTime());
getWorld().getStage().draw();
renderGameObjects();
}
私有void renderGameObjects(){
renderPlatforms();
renderFigures();
}
私有void renderFigures(){
getShaperEnder().begin(shaperEnder.ShapeType.Line);
对于(图:getWorld().getActiveFigures()){
图.render(getSpriteBatch(),getShapeRenderer());
}
getShaperEnder().end();
}
私有void renderPlatforms(){
getShaperEnder().begin(shaperEnder.ShapeType.Line);
for(平台:world.getPlatforms()){
render(getSpriteBatch(),getShapeRenderer());
}
getShaperEnder().end();
}
}
平台代码:
公共类平台扩展GameObject{
私有ArrayList<扇区>扇区=新ArrayList<扇区>();
私有ArrayList关系=新ArrayList();
私有布尔可旋转=真;
private boolean locked=false;
私有空心初始化(浮动cx、浮动cy、浮动半径、浮动边、颜色){
设定位置(cx,cy);
设置半径(半径);
setShape(ShapeType.POLYGON.getInstance)(新浮点[]{
public static float[] mergeCoordinates(float[][] vertices) throws Exception {
if (vertices.length != 2 || vertices[0].length != vertices[1].length) throw new Exception("No valid data");
ArrayList<Float> mergedArrayList = new ArrayList<Float>();
float[] mergedArray = new float[vertices[0].length * 2];
for(int i = 0; i < vertices[0].length; i++) {
mergedArrayList.add(vertices[0][i]);
mergedArrayList.add(vertices[1][i]);
}
int i = 0;
for (Float f : mergedArrayList) {
mergedArray[i++] = (f != null ? f : Float.NaN);
}
return mergedArray;
}