Java 如何将两个Box2D Libgdx实体连接在一起(实体和头部)
我有一个渲染的动画男孩和一个渲染的动画头部。它们都使用Box2d实体,并且都是圆形。我的问题是如何将两者结合在一起,使头部位于身体顶部。目前,身体可以四处移动,我只需要弄清楚如何将头部固定在上面。感谢您的帮助。我也试过关节,但我无法确定哪个关节有效,距离关节无效 这是我的身体课:Java 如何将两个Box2D Libgdx实体连接在一起(实体和头部),java,libgdx,sprite,box2d,Java,Libgdx,Sprite,Box2d,我有一个渲染的动画男孩和一个渲染的动画头部。它们都使用Box2d实体,并且都是圆形。我的问题是如何将两者结合在一起,使头部位于身体顶部。目前,身体可以四处移动,我只需要弄清楚如何将头部固定在上面。感谢您的帮助。我也试过关节,但我无法确定哪个关节有效,距离关节无效 这是我的身体课: public class isaac extends Sprite { public enum State { WALKINGY, WALKINGX, STANDING, }; public St
public class isaac extends Sprite {
public enum State { WALKINGY, WALKINGX, STANDING, };
public State currentState;
public State previousState;
public World world;
public Body b2Body;
private TextureRegion isaacStand;
private Animation<TextureRegion> isaacWalkX;
private Animation<TextureRegion> isaacWalkY;
private float stateTimer;
private boolean walkingRight;
private boolean walkingDown;
public Body b2Head;
public isaac(World world, playScreen screen){
super(screen.getAtlas().findRegion("isaac_body"));
this.world = world;
currentState = State.STANDING;
previousState = State.STANDING;
stateTimer = 0;
walkingRight = true;
walkingDown = true;
Array<TextureRegion> frames = new Array<TextureRegion>();
for(int i = 6; i < 8; i++)
frames.add(new TextureRegion(getTexture(), i*32, 0, 32, 32));
for(int i = 0; i < 6; i++)
frames.add(new TextureRegion(getTexture(), i*32, 32, 32, 32));
isaacWalkY = new Animation<TextureRegion>(0.1f,frames);
frames.clear();
for(int i = 0; i < 8; i++)
frames.add(new TextureRegion(getTexture(), i * 32, 64, 32, 32));
for(int i = 0; i < 2; i++)
frames.add(new TextureRegion(getTexture(), i * 32, 96, 32, 32));
isaacWalkX = new Animation<TextureRegion>(0.1f, frames);
isaacStand = new TextureRegion(getTexture(), 0,32, 32, 32);
defineIsaac();
setBounds(0,0,28 / icsGame.PPM, 28 / icsGame.PPM);
setRegion(isaacStand);
}
public void update(float dt){
setPosition(b2Body.getPosition().x - getWidth() / 2, b2Body.getPosition().y - getHeight() / 2);
setRegion(getFrame(dt));
}
public TextureRegion getFrame(float dt){
currentState = getState();
TextureRegion region;
switch(currentState){
case WALKINGY:
region = isaacWalkY.getKeyFrame(stateTimer, true);
break;
case WALKINGX:
region = isaacWalkX.getKeyFrame(stateTimer, true);
break;
case STANDING:
default:
region = isaacStand;
break;
}
if((b2Body.getLinearVelocity().x < 0 || !walkingRight) && !region.isFlipX()){
region.flip(true, false);
walkingRight = false;
}
else if((b2Body.getLinearVelocity().x > 0 || walkingRight) && region.isFlipX()){
region.flip(true, false);
walkingRight = true;
}
if((b2Body.getLinearVelocity().y > 0) && !region.isFlipY()){
region.flip(false, true);
walkingDown = false;
}
else if((b2Body.getLinearVelocity().y < 0) && region.isFlipY()){
region.flip(false, true);
walkingDown = true;
}
stateTimer = currentState == previousState ? stateTimer + dt : 0;
previousState = currentState;
return region;
}
public State getState(){
if(b2Body.getLinearVelocity().y > 0 || b2Body.getLinearVelocity().y < 0){
return State.WALKINGY;
}
else if((b2Body.getLinearVelocity().x > 0 || b2Body.getLinearVelocity().x < 0)){
return State.WALKINGX;
}
else{
return State.STANDING;
}
}
public void defineIsaac(){
BodyDef bdef = new BodyDef();
bdef.position.set(32 / icsGame.PPM, 32 / icsGame.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2Body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(4 / icsGame.PPM);
fdef.shape = shape;
b2Body.createFixture(fdef);
}
}
这是我的play screen课程:
public class playScreen implements Screen {
private icsGame game;
private TextureAtlas atlas;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Scenes.hud hud;
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private World world;
private Box2DDebugRenderer b2dr;
private isaac player;
private isaacHead head;
public playScreen(icsGame game) {
atlas = new TextureAtlas("isaac.pack");
this.game = game;
gameCam = new OrthographicCamera();
gamePort = new FitViewport(icsGame.V_WIDTH / icsGame.PPM,icsGame.V_HEIGHT / icsGame.PPM,gameCam);
hud = new hud(game.batch);
mapLoader = new TmxMapLoader();
map = mapLoader.load("Level1.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / icsGame.PPM);
gameCam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
world = new World(new Vector2(0,0), true);
b2dr = new Box2DDebugRenderer();
new b2WorldCreator(world, map);
player = new isaac(world, this);
head = new isaacHead(world, this);
}
public TextureAtlas getAtlas(){
return atlas;
}
@Override
public void show() {
}
public void handleInput(float dt){
if (Gdx.input.isKeyPressed(Input.Keys.A) && player.b2Body.getLinearVelocity().x >= -2) {
player.b2Body.setLinearVelocity(-1.5f,0f);
} else if (Gdx.input.isKeyPressed(Input.Keys.D) && player.b2Body.getLinearVelocity().x <= 2) {
player.b2Body.setLinearVelocity(1.5f,0f);
} else if (Gdx.input.isKeyPressed(Input.Keys.A) == Gdx.input.isKeyPressed(Input.Keys.D)) {
player.b2Body.setLinearVelocity(0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.W) && player.b2Body.getLinearVelocity().y <= 2)
player.b2Body.setLinearVelocity(0,1.5f);
if (Gdx.input.isKeyPressed(Input.Keys.S) && player.b2Body.getLinearVelocity().y >= -2)
player.b2Body.setLinearVelocity(0,-1.5f);
if ((Gdx.input.isKeyPressed(Input.Keys.A)) && (Gdx.input.isKeyPressed(Input.Keys.S)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(-1.5f, -1.5f);
}
if ((Gdx.input.isKeyPressed(Input.Keys.D)) && (Gdx.input.isKeyPressed(Input.Keys.S)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(1.5f, -1.5f);
}
if ((Gdx.input.isKeyPressed(Input.Keys.A)) && (Gdx.input.isKeyPressed(Input.Keys.W)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(-1.5f, 1.5f);
}
if ((Gdx.input.isKeyPressed(Input.Keys.D)) && (Gdx.input.isKeyPressed(Input.Keys.W)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(1.5f, 1.5f);
}
}
public void update(float dt){
handleInput(dt);
world.step(1/60f,6,2);
head.setPosition(player.b2Body.getPosition().x, player.b2Body.getPosition().y);
player.update(dt);
head.update(dt);
gameCam.update();
renderer.setView(gameCam);
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, gameCam.combined);
game.batch.setProjectionMatrix(gameCam.combined);
game.batch.begin();
player.draw(game.batch);
head.draw(game.batch);
game.batch.end();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
@Override
public void resize(int width, int height) {
gamePort.update(width,height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
map.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
hud.dispose();
}
}
公共类playScreen实现屏幕{
私人游戏;
私人地图集;
私人正交摄影机;
专用视口游戏端口;
私人场景。hud hud;
专用TmxMapLoader映射加载器;
私人平铺地图;
私有正交瓦片渲染器;
私人世界;
私人邮箱2DDR;
私人艾萨克球员;
二等伊萨克头;
公共播放屏幕(ICSCAME游戏){
atlas=新的纹理层(“isaac.pack”);
这个游戏=游戏;
gameCam=新的正交摄影机();
gamePort=新FitViewport(icsGame.V_宽度/icsGame.PPM,icsGame.V_高度/icsGame.PPM,gameCam);
hud=新hud(游戏批处理);
mapLoader=新的TmxMapLoader();
map=mapLoader.load(“Level1.tmx”);
渲染器=新的正交平铺贴图渲染器(贴图,1/icsGame.PPM);
gameCam.position.set(gamePort.getWorldWidth()/2,gamePort.getWorldHeight()/2,0);
世界=新世界(新矢量2(0,0),真);
b2dr=新的Box2DebugRenderer();
新的b2WorldCreator(世界,地图);
玩家=新艾萨克(世界,这个);
头=新艾萨克(世界,这个);
}
公共纹理图集(){
返回地图集;
}
@凌驾
公开展览({
}
公共无效手动输入(浮动dt){
if(Gdx.input.isKeyPressed(input.Keys.A)和&player.b2Body.getLinearVelocity().x>=-2){
player.b2Body.setLinearVelocity(-1.5f,0f);
}否则,如果(Gdx.input.isKeyPressed(input.Keys.D)和&player.b2Body.getLinearVelocity().x Hello Friend,我会再次问你,为什么要使用两个而不是一个(身体+头部在一起)?我认为应该更容易。使用aseprite在所有条件下绘制角色,向上动画、向下动画等,并将所有信息包含在你的包中。如果你在youtube上学习MarioBros教程,你将得到所有答案。希望它能帮到你。干杯!我正在制作isaac重拍的绑定,如果你有看到这个游戏,头部实际上可以从身体上分离出来,这意味着我必须用箭头键控制头部,用WASD控制身体。自从这篇文章以来,我发现我可以将b2body坐标发送到head类,然后用这些坐标将头部附加到身体上。我现在的问题是我将纹理向左翻转就像mariobros教程中的一样。问题是当我翻转一个纹理时,他们喜欢在身体上移动,所以如果我向右翻转身体精灵,向左翻转头部精灵,它看起来很奇怪,奇怪,我的意思是没有对齐,头部移动到身体的一侧,身体纹理移动到身体的另一侧。有没有翻转的方法使其移动到它所跟随的身体的另一侧的纹理?是的,只有一个身体可以移动角色。关于身体和头部(如果它们必须相互连接,则移动方向应灵活),在精灵角色类中,在getState()中包含更多条件方法,getFrame(float dt)方法,并在数组帧内添加动画。因此,您可以创建向下运行和头部向上翻转的条件,或向下运行和头部向左翻转的条件,或任何其他需要的条件。我还在libgdx中创建RPG原型,我被困在许多点中,以使我的游戏进入下一步。也许我们可以A保持联系,改变体验。干杯!!你好,朋友,我想再次问你,为什么你要用两件而不是一件(身体+头部在一起)?我认为应该更容易。使用aseprite在所有条件下绘制角色,向上动画、向下动画等,并将所有信息包含在你的包中。如果你在youtube上学习MarioBros教程,你将得到所有答案。希望它能帮到你。干杯!我正在制作isaac重拍的绑定,如果你有看到这个游戏,头部实际上可以从身体上分离出来,这意味着我必须用箭头键控制头部,用WASD控制身体。自从这篇文章以来,我发现我可以将b2body坐标发送到head类,然后用这些坐标将头部附加到身体上。我现在的问题是我将纹理向左翻转就像mariobros教程中的一样。问题是当我翻转一个纹理时,他们喜欢在身体上移动,所以如果我向右翻转身体精灵,向左翻转头部精灵,它看起来很奇怪,奇怪,我的意思是没有对齐,头部移动到身体的一侧,身体纹理移动到身体的另一侧。有没有翻转的方法使其移动到它所跟随的身体的另一侧的纹理?是的,只有一个身体可以移动角色。关于身体和头部(如果它们必须相互连接,则移动方向应灵活),在精灵角色类中,在getState()中包含更多条件方法,getFrame(float dt)方法,并在数组帧内添加动画。因此,您可以创建向下运行和头部向上翻转的条件,或向下运行和头部向左翻转的条件,或任何其他需要的条件。我还在libgdx中创建RPG原型,我被困在许多点中,以使我的游戏进入下一步。也许我们可以保持联系,改变体验。干杯!!
public class playScreen implements Screen {
private icsGame game;
private TextureAtlas atlas;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Scenes.hud hud;
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private World world;
private Box2DDebugRenderer b2dr;
private isaac player;
private isaacHead head;
public playScreen(icsGame game) {
atlas = new TextureAtlas("isaac.pack");
this.game = game;
gameCam = new OrthographicCamera();
gamePort = new FitViewport(icsGame.V_WIDTH / icsGame.PPM,icsGame.V_HEIGHT / icsGame.PPM,gameCam);
hud = new hud(game.batch);
mapLoader = new TmxMapLoader();
map = mapLoader.load("Level1.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / icsGame.PPM);
gameCam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
world = new World(new Vector2(0,0), true);
b2dr = new Box2DDebugRenderer();
new b2WorldCreator(world, map);
player = new isaac(world, this);
head = new isaacHead(world, this);
}
public TextureAtlas getAtlas(){
return atlas;
}
@Override
public void show() {
}
public void handleInput(float dt){
if (Gdx.input.isKeyPressed(Input.Keys.A) && player.b2Body.getLinearVelocity().x >= -2) {
player.b2Body.setLinearVelocity(-1.5f,0f);
} else if (Gdx.input.isKeyPressed(Input.Keys.D) && player.b2Body.getLinearVelocity().x <= 2) {
player.b2Body.setLinearVelocity(1.5f,0f);
} else if (Gdx.input.isKeyPressed(Input.Keys.A) == Gdx.input.isKeyPressed(Input.Keys.D)) {
player.b2Body.setLinearVelocity(0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.W) && player.b2Body.getLinearVelocity().y <= 2)
player.b2Body.setLinearVelocity(0,1.5f);
if (Gdx.input.isKeyPressed(Input.Keys.S) && player.b2Body.getLinearVelocity().y >= -2)
player.b2Body.setLinearVelocity(0,-1.5f);
if ((Gdx.input.isKeyPressed(Input.Keys.A)) && (Gdx.input.isKeyPressed(Input.Keys.S)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(-1.5f, -1.5f);
}
if ((Gdx.input.isKeyPressed(Input.Keys.D)) && (Gdx.input.isKeyPressed(Input.Keys.S)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(1.5f, -1.5f);
}
if ((Gdx.input.isKeyPressed(Input.Keys.A)) && (Gdx.input.isKeyPressed(Input.Keys.W)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(-1.5f, 1.5f);
}
if ((Gdx.input.isKeyPressed(Input.Keys.D)) && (Gdx.input.isKeyPressed(Input.Keys.W)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(1.5f, 1.5f);
}
}
public void update(float dt){
handleInput(dt);
world.step(1/60f,6,2);
head.setPosition(player.b2Body.getPosition().x, player.b2Body.getPosition().y);
player.update(dt);
head.update(dt);
gameCam.update();
renderer.setView(gameCam);
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, gameCam.combined);
game.batch.setProjectionMatrix(gameCam.combined);
game.batch.begin();
player.draw(game.batch);
head.draw(game.batch);
game.batch.end();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
@Override
public void resize(int width, int height) {
gamePort.update(width,height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
map.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
hud.dispose();
}
}