gpt4 book ai didi

java - 如何正确检测碰撞?

转载 作者:行者123 更新时间:2023-11-30 01:35:42 24 4
gpt4 key购买 nike

这是我第一次使用 LibGDX 制作游戏,当两个东西发生碰撞时,我试图提高我的游戏分数,但它根本没有记录碰撞。

这是我在 Player 类中的内容:

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.CircleShape;
import com.badlogic.gdx.physics.box2d.EdgeShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.mygdx.summative.MyGdxGame;
import com.mygdx.summative.Screens.PlayScreen;

public class Player extends Sprite {

public World world;
public Body b2dbody;
private TextureRegion peteStand;

public Player(World world, PlayScreen screen ){
super(screen.getAtlas().findRegion("goomba"));
this.world = world;
definePlayer();
peteStand = new TextureRegion(getTexture(), 227,1,16,16);
setBounds(227,1,16/ MyGdxGame.PPM,16/MyGdxGame.PPM);
setRegion(peteStand);
}

public void update (float dt ){
setPosition(b2dbody.getPosition().x - getWidth() /2, b2dbody.getPosition().y - getHeight()/2);
}

public void definePlayer (){
BodyDef bdef = new BodyDef();
bdef.position.set(32 / MyGdxGame.PPM,32 /MyGdxGame.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2dbody = world.createBody(bdef);

FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(6/MyGdxGame.PPM);

fdef.shape = shape;
b2dbody.createFixture(fdef);

EdgeShape body = new EdgeShape(); // used for collision dectection
body.set(new Vector2(-1/MyGdxGame.PPM,7/MyGdxGame.PPM), new Vector2(1/MyGdxGame.PPM,7/MyGdxGame.PPM)); // sets up borders for collision on player
fdef.shape = body ;
fdef.isSensor = true;
b2dbody.createFixture(fdef).setUserData("body"); // used to identify collisions to see if one if the player
}

}

当云来袭的时候,我想增加分数,所以我在Object类中这样写:

public class Cloud extends InteractiveTileObject {

public Cloud(World world, TiledMap map, Rectangle bounds) {
super(world, map, bounds);
fixture.setUserData(this);
}

@Override
public void onBodyHit() {
Gdx.app.log("Cloud" , "Collision");
Hud.addScore(200);
}
}

我错过了什么?这是 ContactListener 类中的联系方法:

    @Override
public void beginContact(Contact contact) { // when to things start to collide

// figures out with fixture is which in a collision of two objects
Fixture fixA = contact.getFixtureA();
Fixture fixB = contact.getFixtureB();

// checks of one is players body
if (fixA.getUserData() == "body" || fixB.getUserData() == "body"){
Fixture body = fixA.getUserData() == "body" ? fixA : fixB;
Fixture object = body == fixA ? fixB : fixA;

// checks if the object the player collided with is a interactive object
if (object.getUserData() instanceof InteractiveTileObject) {
((InteractiveTileObject) object.getUserData() ).onBodyHit();
}

}
}

最佳答案

通常,fixA.getUserData() == "body" 是错误的,因为您比较的是引用的相等性。要检查 String 在内容方面是否等于另一个字符串,即按字典顺序,您应该执行 fixA.getUserData().equals("body") 或更安全的方法"body".equals(fixA.getUserData()).

顺便说一句,“玩家” 标识符可能更合适,因为从技术上讲,物理世界中的一切都是一个物体。

关于java - 如何正确检测碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35139437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com