gpt4 book ai didi

java - Box2D 主体不渲染并崩溃

转载 作者:行者123 更新时间:2023-12-01 09:44:34 25 4
gpt4 key购买 nike

我只是想在屏幕上渲染 Box2D 主体。

这是我的核心类代码:

 public class Core extends ApplicationAdapter {

private World world;
private Box2DDebugRenderer rend;
EntityParent p;
private OrthographicCamera cam;
@Override
public void create () {

cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.setToOrtho(false);
cam.viewportWidth = 640;
cam.viewportHeight = 480;
world = new World(new Vector2(0, -9.81f), true);

rend = new Box2DDebugRenderer();


p = new EntityParent(world, new Vector2(100, 100), BodyType.DynamicBody);
p.initBodyVariables(1, 1, 1);
p.createCircle(50);
}


@Override
public void render () {
cam.update();
world.step(1/60f, 6, 2);
System.out.println(p.getPosition());

Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

rend.render(world, cam.combined);

}

}

这是 EntityParent 的代码:

package com.reality.entity;

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.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

public class EntityParent implements Entity{

/*
* Used for initializing the body
*/
protected Vector2 position;
protected World world;

/*
* All definitions for creating the body
*/
protected BodyType type;
protected BodyDef bodyDef;
protected Body body;
protected FixtureDef fixtureDef;
protected Fixture fixture;

protected float density, friction, restitution;

/**
* Puts body by default at (0,0)
* @param world
* @param type
*/
public EntityParent(World world, BodyType type){
this.world = world;
this.type = type;
this.position = new Vector2(0, 0);

}
/**
*
* @param world
* @param position of body
* @param type
*/
public EntityParent(World world, Vector2 position, BodyType type){
this.world = world;
this.position = position;
this.type = type;
}


/**
*
* @param world
* @param x position of body
* @param y position of body
* @param type
*/
public EntityParent(World world, float x, float y, BodyType type){
this.world = world;
this.position = new Vector2(x, y);
this.type = type;
}

public void initBodyVariables(float density, float friction, float restitution){
this.density = density;
this.friction = friction;
this.restitution = restitution;
}


@Override
public void createPolygonBody(float[] vertices) {

bodyDef = new BodyDef();
bodyDef.type = type;
bodyDef.position.set(position);

body = world.createBody(bodyDef);

PolygonShape shape = new PolygonShape();
shape.set(vertices);

fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = density;
fixtureDef.friction = friction;
fixtureDef.restitution = restitution;

fixture = body.createFixture(fixtureDef);
shape.dispose();

}

@Override
public void createRectangle(Vector2 dimensions) {

bodyDef = new BodyDef();
bodyDef.type = type;
bodyDef.position.set(position);

body = world.createBody(bodyDef);

PolygonShape shape = new PolygonShape();
shape.setAsBox(dimensions.x, dimensions.y);

fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = density;
fixtureDef.friction = friction;
fixtureDef.restitution = restitution;

fixture = body.createFixture(fixtureDef);
shape.dispose();
}

@Override
public void createCircle(float radius) {
bodyDef = new BodyDef();
bodyDef.type = type;
bodyDef.position.set(position);

body = world.createBody(bodyDef);

PolygonShape shape = new PolygonShape();
shape.setRadius(radius);

fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = density;
fixtureDef.friction = friction;
fixtureDef.restitution = restitution;

fixture = body.createFixture(fixtureDef);
shape.dispose();
}

@Override
public void update() {

}

@Override
public void render(Box2DDebugRenderer renderer) {

}




@Override
public Vector2 getPosition() {
return this.position;
}

}

那么当我运行这个时会发生什么我收到以下错误:

AL lib: (EE) alc_cleanup: 1 device not closed
Assertion failed!

Program: C:\Program Files\Java\jre1.8.0_60\bin\javaw.exe
File: /var/lib/jenkins/workspace/libgdx/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp, Line 384

Expression: m_count >= 3

如果我从核心类中删除该语句 p.initBodyVariables(1, 1, 1);

我只是得到一个空白屏幕。

我测试了一下它是否是世界,而世界说其中有一个物体,因此它正在注册,但我只是不明白为什么它会抛出此错误并且根本不渲染。非常感谢任何帮助,谢谢!

最佳答案

创建圆圈时,您使用了错误的形状类型 - PolygonShape 而不是 CircleShape:

    @Override
public void createCircle(float radius) {
bodyDef = new BodyDef();
bodyDef.type = type;
bodyDef.position.set(position);

body = world.createBody(bodyDef);

PolygonShape shape = new PolygonShape(); //here it should be CircleShape
shape.setRadius(radius);

应该是:

        CircleShape shape = new CircleShape();

关于java - Box2D 主体不渲染并崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38172435/

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