Android libGDX+;box2d触球反弹
我试图用libGDX和box2d让球在接触时反弹, 它不会反弹,只是像太阳一样转过来, 有人能帮我吗Android libGDX+;box2d触球反弹,android,libgdx,box2d,Android,Libgdx,Box2d,我试图用libGDX和box2d让球在接触时反弹, 它不会反弹,只是像太阳一样转过来, 有人能帮我吗 @Override public void create() { batch = new SpriteBatch(); BallTexture = new Texture("data/ball.png"); BallSprite = new Sprite(BallTexture); BallSprite.setPosition(-BallSprite.getWi
@Override
public void create() {
batch = new SpriteBatch();
BallTexture = new Texture("data/ball.png");
BallSprite = new Sprite(BallTexture);
BallSprite.setPosition(-BallSprite.getWidth() / 2,
-BallSprite.getHeight() / 2);
world = new World(new Vector2(0, -10f), true);
BodyDef BallDef = new BodyDef();
BallDef.type = BodyDef.BodyType.DynamicBody;
BallDef.position.set((BallSprite.getX() + BallSprite.getWidth() / 2)
/ PIXELS_TO_METERS,
(BallSprite.getY() + BallSprite.getHeight() / 2)
/ PIXELS_TO_METERS);
BallBody = world.createBody(BallDef);
CircleShape BallShape = new CircleShape();
BallShape.setRadius(3f);
FixtureDef BallFixDeff = new FixtureDef();
BallFixDeff.shape = BallShape;
BallFixDeff.density = 2.5f;
BallFixDeff.friction = 3f;
BallFixDeff.restitution = .75f;
BallBody.createFixture(BallFixDeff);
BallShape.dispose();
//floor
BodyDef floorDef = new BodyDef();
floorDef.type = BodyDef.BodyType.StaticBody;
float w = Gdx.graphics.getWidth() / PIXELS_TO_METERS;
float h = Gdx.graphics.getHeight() / PIXELS_TO_METERS - 50
/ PIXELS_TO_METERS;
floorDef.position.set(0, 0);
FixtureDef FloorFixDeff = new FixtureDef();
EdgeShape FloorEdgeShape = new EdgeShape();
FloorEdgeShape.set(-w / 2, -h / 2, w / 2, -h / 2);
FloorFixDeff.shape = FloorEdgeShape;
floorBody = world.createBody(floorDef);
floorBody.createFixture(FloorFixDeff);
FloorEdgeShape.dispose();
Gdx.input.setInputProcessor(this);
debugRenderer = new Box2DDebugRenderer();
camera = new OrthographicCamera(Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
}
@Override
public void render() {
camera.update();
world.step(1f / 60f, 6, 2);
BallBody.applyTorque(torque,true);
BallSprite.setPosition(
(BallBody.getPosition().x * PIXELS_TO_METERS) - BallSprite.getWidth()
/ 2,
(BallBody.getPosition().y * PIXELS_TO_METERS) - BallSprite.getHeight()
/ 2);
BallSprite.setRotation((float)Math.toDegrees(BallBody.getAngle()));
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
debugMatrix = batch.getProjectionMatrix().cpy().scale(PIXELS_TO_METERS,
PIXELS_TO_METERS, 0);
batch.begin();
if(drawBall)
batch.draw(BallSprite, BallSprite.getX(), BallSprite.getY(), BallSprite.getOriginX(), BallSprite.getOriginY(), BallSprite.getWidth(), BallSprite.getHeight(), BallSprite.getScaleX(), BallSprite.getScaleY(), BallSprite.getRotation());
batch.end();
debugRenderer.render(world, debugMatrix);
}
@Override
public void dispose() {
BallTexture.dispose();
world.dispose();
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
//BallBody.applyForce(0f,10f,screenX,screenY,true);
BallBody.applyLinearImpulse(0f, 0.8f, screenX, screenY, true);
return false;
}
}
你的问题以单位为单位。Box2D使用仪表
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
//BallBody.applyForce(0f,10f,screenX,screenY,true);
BallBody.applyLinearImpulse(0f, 0.8f, screenX, screenY, true);
return false;
}
此代码导致出现问题。
用这个代替
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector3 temp = new Vector3();
camera.unproject(temp.set(screenX, screenY, 0));
BallBody.applyLinearImpulse(0f, 0.8f, temp.x, temp.y, true);
return false;
}
你应该先取消坐标投影。不要在代码中乱扔像素,要学会如何正确使用相机。您的相机的属性应该以米为单位,而不是以像素为单位。对不起,以上各项都没有帮助, 我补充说:
BallBody.applyLinearImpulse(new Vector2(-5, 450), BallBody.getLocalCenter(), true);
它是有效的,但现在我必须检查与身体或精灵的碰撞,但我在网上发现的一切似乎都不起作用
这是我的密码:
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
float centerX,centerY,radius,distance;
centerX = BallSprite.getX() + BallSprite.getWidth()/2;
centerY = BallSprite.getY() + BallSprite.getHeight()/2;
radius = BallSprite.getHeight()/2;
distance = (float)Math.sqrt(Math.pow((screenX-centerX),2)+Math.pow((screenY-centerY),2));
if (distance <= radius)
BallBody.applyLinearImpulse(new Vector2(-5, 450), BallBody.getLocalCenter(), true);
public boolean接地(int screenX、int screenY、int指针、int按钮){
//TODO自动生成的方法存根
浮动中心X、中心Y、半径、距离;
centerX=BallSprite.getX()+BallSprite.getWidth()/2;
centerY=BallSprite.getY()+BallSprite.getHeight()/2;
radius=BallSprite.getHeight()/2;
距离=(float)Math.sqrt(Math.pow((screenX centerX),2)+Math.pow((screenY centerY),2));
如果(距离),你不应该将此作为答案发布。对原始帖子进行编辑,或者发表评论,让人们知道你编辑了它就可以了。