所以我一直试图在我身体在世界各地移动的地方移动精灵。所以我使用了精灵的setPosition方法,但事实证明它并没有帮助。我的形象只位于(0,0)位置,我无法理解为什么。
以下是代码:
package com.example.shapetest;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
class ShapeTest implements ApplicationListener {
SpriteBatch batch;
World world;
OrthographicCamera camera;
Box2DDebugRenderer debugRenderer;
Texture squareTx;
Sprite square;
Body body;
@Override
public void create() {
batch = new SpriteBatch();
world = new World(new Vector2(0,-10),false);
squareTx = new Texture(Gdx.files.internal("sq.png"));
square = new Sprite(squareTx);
debugRenderer = new Box2DDebugRenderer();
camera = new OrthographicCamera();
camera.setToOrtho(false);
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
bd.position.set(new Vector2((Gdx.graphics.getWidth() / 2) * 0.01f, Gdx.graphics.getHeight() * 0.01f));
body = world.createBody(bd);
BodyDef bd2 = new BodyDef();
bd2.type = BodyType.StaticBody;
bd2.position.set(new Vector2(Gdx.graphics.getWidth()/2 * 0.01f, 100 * 0.01f));
Body body2 = world.createBody(bd2);
square.setSize(100,100);
PolygonShape shape = new PolygonShape();
shape.setAsBox(0.5f,0.5f);
PolygonShape shape2 = new PolygonShape();
shape2.setAsBox(1.6f,0.3f);
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.restitution = 0.2f;
fd.shape = shape;
fd.friction = 0.4f;
body.createFixture(fd);
body2.createFixture(shape2, 0f);
shape.dispose();
}
@Override
public void resize(int width, int height) {
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Vector2 pos = body.getPosition();
square.setPosition(pos.x ,pos.y);
camera.update();
batch.setProjectionMatrix(camera.combined);
Matrix4 cameraCopy = camera.combined.cpy();
debugRenderer.render(world, cameraCopy.scl(100f));
world.step(1/60f,10,10);
batch.begin();
square.draw(batch);
batch.end();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
batch.dispose();
}
}
结果如下:
答案 0 :(得分:0)
body.getPosition()
返回小值,因此对于绘图,您需要将该值转换为像素值。
根据你的说法,精灵大小是100像素,它代表动态物理体,即物理中具有0.5f * 2 = 1;
单位的polygonShape,即假设1单位/米的物理等于100像素。
Vector2 pos = body.getPosition(); // value return in physics unit
square.setPosition(pos.x*100 ,pos.y*100);