所以我看到一个非常奇怪的问题。
使用展示中的PlayN豌豆演示我将一个简单的应用程序放在一起 - 我有一个动态实体放到一个静态实体上,我无法弄清楚为什么它没有弹跳。
在做了一些打印输出后,我发现碰撞正在发生 - 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
这是一张图片 - 这朵花是我的测试动态实体 - 它明显停在砖头上,但我无法弄清楚为什么速度会继续上升
我不知道其他代码在这里会有什么帮助 - 我的设置几乎与展示中的Peas Demo完全相同 - 我从Showcase演示中删除了一些东西并且无法重新创建那个问题。
我100%不知所措,因为这是一个物理问题,这会阻碍我的所有进步。豌豆演示完全有效
任何帮助将不胜感激 - 我已经尝试了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);
}
}
}
}
}
答案 0 :(得分:2)
如果有人遇到这种情况 - 在整整一天半的时间里试图解决这个问题并创建一个单独的简单项目,将一个动态实体放到静态实体上并看到它做同样的事情。
如果你看到这发生在实体停止移动但速度不断增加的地方 - 你输入的碰撞形状是后退 - 我有4个顶点按0,1,2,3的顺序放入形状 - 我有把它们放进3,2,1,0就可以了。我想如果遇到同样类型的问题,请尝试颠倒创建形状的顺序,看看碰撞是否有效。