gpt4 book ai didi

java - PlayN JBox2D Y 速度增加但发生碰撞

转载 作者:行者123 更新时间:2023-11-30 09:30:15 25 4
gpt4 key购买 nike

所以我看到了一个非常奇怪的问题。

使用展示柜中的 PlayN 豌 bean 演示,我组装了一个简单的应用程序 - 我有一个动态实体掉落到静态实体上,但我无法弄清楚为什么它没有反弹。

在做了一些打印输出后,我发现碰撞正在发生——y 位置保持不变,但 y 速度一直在增加

这是我在动态实体中的更新代码

  @Override
public void paint(float alpha) {
float x = (body.getPosition().x * alpha) + (previousX * (1f - alpha));
float y = (body.getPosition().y * alpha) + (previousY * (1f - alpha));
float w = (body.getAngle() * alpha) + (previousRotationAngle * (1f - alpha));
if (body.m_type == BodyType.DYNAMIC) {
System.out.println("body x = " + body.getPosition().x + ", y = " + body.getPosition().y);
System.out.println("velocity x = " + body.m_linearVelocity.x + ", y = "
+ body.m_linearVelocity.y);
}
imageLayer.setTranslation(x, y);
imageLayer.setRotation(w);
}

@Override
public void update(float delta) {
previousX = body.getPosition().x;
previousY = body.getPosition().y;
previousRotationAngle = body.getAngle();
}

这是打印输出——你可以看到速度不断上升,但 y 轴位置保持不变,就在 8.46

  [java] body x = 17.024979, y = 0.5199691
[java] velocity x = 0.0, y = 1.2983631
[java] body x = 17.024979, y = 0.79601336
[java] velocity x = 0.0, y = 2.562787
[java] body x = 17.024979, y = 0.8908453
[java] velocity x = 0.0, y = 2.8736944
[java] body x = 17.024979, y = 0.99586946
[java] velocity x = 0.0, y = 3.18255
[java] body x = 17.024979, y = 0.99586946
[java] velocity x = 0.0, y = 3.18255
[java] body x = 17.024979, y = 1.1110185
[java] velocity x = 0.0, y = 3.489367
[java] body x = 17.024979, y = 1.2362258
[java] velocity x = 0.0, y = 3.794159
[java] body x = 17.024979, y = 1.2362258
[java] velocity x = 0.0, y = 3.794159
[java] body x = 17.024979, y = 1.3714248
[java] velocity x = 0.0, y = 4.096939
[java] body x = 17.024979, y = 1.5165496
[java] velocity x = 0.0, y = 4.3977213
[java] body x = 17.024979, y = 1.6715348
[java] velocity x = 0.0, y = 4.696518
[java] body x = 17.024979, y = 1.6715348
[java] velocity x = 0.0, y = 4.696518
[java] body x = 17.024979, y = 1.8363149
[java] velocity x = 0.0, y = 4.993343
[java] body x = 17.024979, y = 2.0108259
[java] velocity x = 0.0, y = 5.2882085
[java] body x = 17.024979, y = 2.195003
[java] velocity x = 0.0, y = 5.581128
[java] body x = 17.024979, y = 2.388783
[java] velocity x = 0.0, y = 5.8721147
[java] body x = 17.024979, y = 2.388783
[java] velocity x = 0.0, y = 5.8721147
[java] body x = 17.024979, y = 2.5921018
[java] velocity x = 0.0, y = 6.1611805
[java] body x = 17.024979, y = 2.5921018
[java] velocity x = 0.0, y = 6.1611805
[java] body x = 17.024979, y = 2.804897
[java] velocity x = 0.0, y = 6.4483385
[java] body x = 17.024979, y = 3.0271058
[java] velocity x = 0.0, y = 6.733601
[java] body x = 17.024979, y = 3.0271058
[java] velocity x = 0.0, y = 6.733601
[java] body x = 17.024979, y = 3.2586663
[java] velocity x = 0.0, y = 7.016981
[java] body x = 17.024979, y = 3.4995165
[java] velocity x = 0.0, y = 7.298491
[java] body x = 17.024979, y = 3.4995165
[java] velocity x = 0.0, y = 7.298491
[java] body x = 17.024979, y = 3.7495952
[java] velocity x = 0.0, y = 7.5781426
[java] body x = 17.024979, y = 4.0088415
[java] velocity x = 0.0, y = 7.8559484
[java] body x = 17.024979, y = 4.277195
[java] velocity x = 0.0, y = 8.131921
[java] body x = 17.024979, y = 4.5545955
[java] velocity x = 0.0, y = 8.406072
[java] body x = 17.024979, y = 4.5545955
[java] velocity x = 0.0, y = 8.406072
[java] body x = 17.024979, y = 4.840983
[java] velocity x = 0.0, y = 8.678413
[java] body x = 17.024979, y = 4.840983
[java] velocity x = 0.0, y = 8.678413
[java] body x = 17.024979, y = 5.1362987
[java] velocity x = 0.0, y = 8.948957
[java] body x = 17.024979, y = 5.440483
[java] velocity x = 0.0, y = 9.217716
[java] body x = 17.024979, y = 5.440483
[java] velocity x = 0.0, y = 9.217716
[java] body x = 17.024979, y = 5.753478
[java] velocity x = 0.0, y = 9.484701
[java] body x = 17.024979, y = 6.0752254
[java] velocity x = 0.0, y = 9.749924
[java] body x = 17.024979, y = 6.0752254
[java] velocity x = 0.0, y = 9.749924
[java] body x = 17.024979, y = 6.4056673
[java] velocity x = 0.0, y = 10.013396
[java] body x = 17.024979, y = 6.7447467
[java] velocity x = 0.0, y = 10.275129
[java] body x = 17.024979, y = 7.0924063
[java] velocity x = 0.0, y = 10.535135
[java] body x = 17.024979, y = 7.0924063
[java] velocity x = 0.0, y = 10.535135
[java] body x = 17.024979, y = 7.4485893
[java] velocity x = 0.0, y = 10.793425
[java] body x = 17.024979, y = 7.8132396
[java] velocity x = 0.0, y = 11.05001
[java] body x = 17.024979, y = 7.8132396
[java] velocity x = 0.0, y = 11.05001
[java] body x = 17.024979, y = 8.186301
[java] velocity x = 0.0, y = 11.304901
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 11.55811
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 11.55811
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 11.8096485
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 12.059526
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 12.059526
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 12.307755
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 12.554346
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 12.554346
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 12.799309
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 13.042655
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 13.042655
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 13.284395
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 13.52454
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 13.7631
[java] body x = 17.024979, y = 8.465522
[java] velocity x = 0.0, y = 13.7631
[java] body x = 17.024979, y = 8.465522

这是一张图片——这朵花是我的测试动态实体——它显然停在了砖 block 上,但我不明白为什么速度会继续上升

enter image description here


我不知道还有什么其他代码在这里有用 - 我的设置几乎与展示柜中的豌 bean 演示相同 - 以至于我从展示柜演示中删除了一些东西并且无法重新创建问题在于。

我在这里 100% 不知所措,因为这是一个物理问题,所以它阻碍了我所有的进步。豌 bean 演示完全有效

任何帮助将不胜感激 - 我已经尝试了 4 个小时,现在尝试了所有我能想到的东西,但没有任何效果


编辑添加了主要世界类(这绝对是丑陋的 - 请不要评判我)

public class LevelWorld implements ContactListener, Paintable, Updatable, Destroyable {

private static final float PHYSICS_UNITS_PER_PIXEL = PhysicsScale.PHYSICS_UNITS_PER_PIXEL
.getScaleFactor();

private final playn.core.GroupLayer worldLayer;

private final Context context;
private final World world;
private final float frameRate;

private final List<playn.core.GroupLayer> groupLayers;
private final List<Entity> entities;
private final Stack<Contact> contacts;
private final Map<Body, PhysicsEntity> bodies;

public LevelWorld(Level level, Context context) {
worldLayer = graphics().createGroupLayer();
worldLayer.setScale(1.F / PHYSICS_UNITS_PER_PIXEL);
this.context = context;
final Integer fps = (Integer) context.get(FPS);
this.frameRate = 1.F / fps.floatValue();
final Vec2 gravity = new Vec2(0.F, 10.0F);
world = new World(gravity, true);
world.setWarmStarting(true);
world.setAutoClearForces(true);
groupLayers = new ArrayList<playn.core.GroupLayer>();
entities = new ArrayList<Entity>();
contacts = new Stack<Contact>();
bodies = new HashMap<Body, PhysicsEntity>();
for (final GroupLayer groupLayer : level) {
final playn.core.GroupLayer gl = graphics().createGroupLayer();
gl.setDepth(groupLayer.getDepth());
gl.setScale(groupLayer.getScaleFactor());
groupLayers.add(gl);
for (final Entity entity : groupLayer.getEntities()) {
if (entity instanceof PhysicsEntity) {
final FixtureDef fixtureDef = new FixtureDef();
final BodyDef bodyDef = new BodyDef();
if (PhysicsType.DYNAMIC.equals(entity.getEntityMetadata().getPhysicsType())) {
bodyDef.type = BodyType.DYNAMIC;
} else if (PhysicsType.STATIC.equals(entity.getEntityMetadata().getPhysicsType())) {
bodyDef.type = BodyType.STATIC;
}
final Body body = world.createBody(bodyDef);
fixtureDef.shape = entity.getEntityMetadata().getShape();
fixtureDef.density = 0.4f;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.5f;
body.setLinearDamping(0.2f);
body.createFixture(fixtureDef);
body.setTransform(new Vec2(entity.getPosition().x, entity.getPosition().y),
entity.getRotationAngle());
((PhysicsEntity) entity).setBody(body);
bodies.put(body, (PhysicsEntity) entity);
}
gl.add(entity.getImageLayer());
}
entities.addAll(groupLayer.getEntities());
worldLayer.add(gl);
}
graphics().rootLayer().add(worldLayer);
world.setContactListener(this); // kinda sketchy
}

@Override
public void beginContact(Contact contact) {
System.out.println("BEGIN CONTACT HAPPENED");
contacts.push(contact);
}

@Override
public void endContact(Contact contact) {

}

@Override
public void preSolve(Contact contact, Manifold oldManifold) {

}

@Override
public void postSolve(Contact contact, ContactImpulse impulse) {

}

@Override
public void destroy() {
worldLayer.destroy();
}

@Override
public void update(float delta) {
// worldLayer.setOrigin(worldLayer.originX() + (delta / 250.F),
// worldLayer.originY());
for (final Entity entity : entities) {
entity.update(delta);
}
world.step(0.033F, 10, 10);
// processContacts();
}

@Override
public void paint(float alpha) {
for (final Entity entity : entities) {
entity.paint(alpha);
}
}

private void processContacts() {
while (!contacts.isEmpty()) {
final Contact contact = contacts.pop();
final PhysicsEntity a = bodies.get(contact.m_fixtureA.m_body);
final PhysicsEntity b = bodies.get(contact.m_fixtureB.m_body);

if (a != null && b != null) {
if (a instanceof PhysicsEntity.IsContactListener) {
((PhysicsEntity.IsContactListener) a).contact(b);
}
if (b instanceof PhysicsEntity.IsContactListener) {
((PhysicsEntity.IsContactListener) b).contact(a);
}
}
}
}
}

最佳答案

以防万一有人遇到这个问题 - 经过一整天半的时间试图解决这个问题并创建一个单独的简单项目,将一个动态实体放到一个静态实体上并看到它做同样的事情。

如果你看到这种情况发生在实体停止移动但速度不断增加的地方——你输入的碰撞形状是向后的——我有 4 个顶点按 0、1、2、3 的顺序放入形状中——如果我将它们放入 3,2,1,0 中会很好用。我想,如果您遇到同样类型的问题,请尝试颠倒创建形状的顺序,看看碰撞是否有效。

关于java - PlayN JBox2D Y 速度增加但发生碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13329906/

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