如何解决对象坐标未出现在正确的位置
我必须调整场景坐标才能找到有用的重力。 因此,我决定减少屏幕坐标,以使物体上的重力行为正常。 但是我现在有一个问题,就是当我键入sprite.setPosition(0,0)时,精灵似乎没有出现在正确的位置。
这是我的代码:
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.Box2d.Body;
import com.badlogic.gdx.physics.Box2d.BodyDef;
import com.badlogic.gdx.physics.Box2d.FixtureDef;
import com.badlogic.gdx.physics.Box2d.polygonShape;
import com.badlogic.gdx.physics.Box2d.CircleShape;
import com.badlogic.gdx.physics.Box2d.World;
public class MyGdxGame extends ApplicationAdapter {
public OrthographicCamera camera;
Sound sound;
BitmapFont font,font2;
SpriteBatch batch;
Texture img1,img2,img3,img4,img5,img6;
Sprite sprite1,sprite2,sprite3;
World world;
Body solKose,sagKose,ustKose,altKose,body1,body2,body3;
int width=0;
float axes=0;
private int enemyGoalCounter,playerGoalCounter=0;
Sphere _player;
Sphere _enemyPlayer;
Sphere ball;
Vector2 directionVector;
Vector3 touchCoordinate;
int direction=-1;
@Override
public void create () {
camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
camera.viewportWidth = Gdx.graphics.getWidth()/100;
camera.viewportHeight = Gdx.graphics.getHeight()/100;
camera.update();
font = new BitmapFont();
font2 = new BitmapFont();
font.setColor(Color.RED);
font2.setColor(Color.BLUE);
touchCoordinate = new Vector3(0,0);
sound = Gdx.audio.newSound(Gdx.files.internal("taktak.wav"));
batch = new SpriteBatch();
img1 = new Texture("sphere2.png");
img2 = new Texture("sphere2.png");
img3 = new Texture("ball.png");
sprite1 = new Sprite(img1);
sprite2 = new Sprite(img2);
sprite3 = new Sprite(img3);
sprite1.setPosition(0,0);
sprite2.setPosition(0,0);
world = new World(new Vector2(0,0),true);
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(sprite1.getX()/2,sprite1.getY()/2);
body1 = world.createBody(bodyDef);
CircleShape shape = new CircleShape();
shape.seTradius(1f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.restitution=0.6f;
body1.createFixture(fixtureDef);
shape.dispose();
BodyDef bodyDef2 = new BodyDef();
bodyDef2.type = BodyDef.BodyType.DynamicBody;
bodyDef2.position.set(sprite2.getX()/2,sprite2.getY()/2);
body2 = world.createBody(bodyDef2);
CircleShape shape2 = new CircleShape();
shape2.seTradius(1f);
FixtureDef fixtureDef2 = new FixtureDef();
fixtureDef2.shape = shape2;
fixtureDef.restitution=0.6f;
body2.createFixture(fixtureDef2);
shape2.dispose();
}
@Override
public void render () {
System.out.println();
camera.update();
play();
inputController(); // A function for keyboard input controlling
math(); // For Collision controlling
world.step(1f/60f,6,2);
sprite1.setPosition(body1.getPosition().x,body1.getPosition().y);
sprite2.setPosition(body2.getPosition().x,body2.getPosition().y);
Gdx.gl.glClearColor(0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(sprite1,sprite1.getX(),sprite1.getY(),2,2);
batch.draw(sprite2,sprite2.getX(),sprite2.getY(),2);
batch.end();
}
@Override
public void dispose () {
batch.dispose();
world.dispose();
}
void play() {
}
void inputController() {
if(Gdx.input.isTouched()){
Vector2 directionVector = new Vector2(0,0);
touchCoordinate.x = Gdx.input.getX();
touchCoordinate.y = Gdx.input.getY();
camera.unproject(touchCoordinate);
double distance = Math.pow(sprite1.getWidth()/2,2)-(Math.pow((sprite1.getoriginX()-touchCoordinate.x),2)+Math.pow((sprite1.getoriginY()-touchCoordinate.y),2));
System.out.println(touchCoordinate);
if(distance>0){
body1.applyForcetoCenter(20,20,true);
}else if(distance==0){
body1.applyForcetoCenter(20,true);
}else{
////
}
}
//_player.setCenterPosition(touchCoordinate.x,touchCoordinate.y);
}
void math() {
goalSystem();
}
void restartPositions(){
}
void goalSystem() {
/*
if(!goal) {
if(ball.getCenterPosition(ball.position.x,ball.position.y).y < 20) {
if((ball.getCenterPosition(ball.position.x,ball.position.y).x >= Gdx.graphics.getWidth()/2-325/2) && (ball.getCenterPosition(ball.position.x,ball.position.y).x <= Gdx.graphics.getWidth()/2+325/2)) {
goal=true;
enemyGoalCounter++;
restartPositions();
}
}
if(ball.getCenterPosition(ball.position.x,ball.position.y).y > Gdx.graphics.getHeight()-20) {
if((ball.getCenterPosition(ball.position.x,ball.position.y).x <= Gdx.graphics.getWidth()/2+325/2)) {
goal=true;
playerGoalCounter++;
restartPositions();
}
}
}
*/
}
public class Sphere{
Vector2 firstPos,firstDirection;
float firstSpeed;
Vector2 position,direction,centerPosition;
float x,y,width,height;
float radius;
float xCenter,yCenter;
float speed;
float veLocity;
public Sphere(float x,float y,float radius,float width,float height) {
this.x = x;
this.y = y;
this.radius = radius;
this.width = width;
this.height = height;
this.position = new Vector2(x,y);
this.direction = new Vector2(x,y);
this.speed = 0;
this.firstSpeed = speed;
this.firstPos = new Vector2(x,y);
this.firstDirection = new Vector2(x,y);
}
Vector2 getCenterPosition(float x,float y) {
centerPosition = new Vector2(x+width/2,y);
return centerPosition;
}
void setCenterPosition(float x,float y){
position.x = x-width/2;
position.y = y-height/2;
}
void setSpeed(float speed) {
this.speed = speed;
}
float getSpeed() {
return this.speed;
}
}
}
图片:
解决方法
我认为问题是相机和视口,而不是渲染位置问题。设置摄像机和视口的代码可以更改。如果使用视口,请-确定渲染的坐标范围,该坐标范围是任意的。然后根据需要缩放此渲染以适合屏幕。在您自己的情况下,您在设置自己的视口参数之前获得了设备的宽度和高度(即使纵横比变化,跨设备也不是恒定的)? 所以试试吧
camera = new OrthographicCamera(100,500); //You pick as you like
camera.position.set(camera.viewportWidth / 2,camera.viewportHeight / 2,0); //centre camera
viewport = new FitViewport(camera.viewportWidth,camera.viewportHeight,camera);//stretch proportionally
也请阅读
How Camera works in Libgdx and together with Viewport
(而且您也没有在y处添加偏移量。
Vector2 getCenterPosition(float x,float y) {
centerPosition = new Vector2(x+width/2,y);
....
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。