简体   繁体   English

夹具的Jbox2D问题

[英]Jbox2D problems with fixture

I just started using jbox2d yesterday in my game and I'm currently having some issues and I can't find a way to fix them. 我昨天刚开始在游戏中使用jbox2d,目前遇到一些问题,我找不到解决这些问题的方法。 I have many static bodies and one dynamic body (which is the player) and I want to simulate collisions. 我有许多静态物体和一个动态物体(即玩家),我想模拟碰撞。

Basically as soon as the method world.step is called my dynamic body moves out of the original position on both the x and y axis (from 18.0 to 19.25 and from 26.0 to 70.76 . I tried to print everything else (the position of static bodies and the movement of the dynamic body) and it's correct. 基本上,一旦方法world.step被调用,我的动态物体就会同时从x和y轴上移出原始位置(从18.0到19.25,从26.​​0到70.76。我试图打印其他所有内容(静态物体的位置)以及动态物体的运动),这是正确的。

I though it had to do with the gravity (which I don't need), but after I set the world and dynamic body to 0.0f, it still gives the same problem. 尽管它与重力有关(我不需要),但是在将世界和动态物体设置为0.0f之后,仍然会遇到相同的问题。 The I figured out the problem is the fixture of my dynamic entity (If I remove it the problem doesn't occur anymore) 我发现问题出在我的动态实体的固定装置上(如果我删除它,那么问题就不会再发生了)

Also the body and the dynamic entity are moving slightly differently (in pixels, so I guess my setLinearVelocity method might be wrong) 物体和动态实体的移动也略有不同(以像素为单位,所以我想我的setLinearVelocity方法可能是错误的)

How can I fix this and what's these problems? 我该如何解决这个问题?

This is my code (I avoid useless stuffs and I keep only those regarding jbox2d): 这是我的代码(避免使用无用的东西,而只保留那些与jbox2d有关的东西):

Map class, which contains a map of the game. Map类,其中包含游戏地图。

//fields
Code:
public static final float METER_IN_PIXELS = 40.0f;
private World world;
...



//constructor
Code:
public Map(int startPosX, int startPosY)
{
   world = new World(new Vec2(0,10.0f));
   ...
}


//called every 33ms
Code:
public void update(int delta)
{
   world.step(1/60.0f, 8, 3);
   player.update(delta); // to update the player's movement
        ...
}

PhysicsMovingEntity the class used by all the moving entities (at the moment it's extended only by the player PhysicsMovingEntity所有移动实体使用的类(目前仅由播放器扩展)

//fields
Code:
private Body body;
private float physicsX;
private float physicsY;

//constructor

public PhysicsMovingEntity(float x, float y, World world)
Code:
{
   super.setX(x);
   super.setY(y);
   physicsX = x / Map.METER_IN_PIXELS;
   physicsY = y / Map.METER_IN_PIXELS;
   initPhysicalBody(world);
}


//init method

Code:
public void initPhysicalBody(World world)
{
   BodyDef bd = new BodyDef();
   bd.type = BodyType.DYNAMIC;
   bd.position.set(physicsX, physicsY);
   bd.gravityScale = 0;
   PolygonShape ps = new PolygonShape();
   ps.setAsBox(getFrameSize().width() / Map.METER_IN_PIXELS, getFrameSize().height() / Map.METER_IN_PIXELS);
   FixtureDef fd = new FixtureDef();
   fd.density = 1;
   fd.shape = ps;
   body = world.createBody(bd);
   [color=#FF4000]body.createFixture(fd);[/color] // this toggles the problem
}


//called every 33ms
Code:
public void update(int delta) 
{
   super.update(delta);
   [color=#FF4000]body.setLinearVelocity(new Vec2(getLastDirection().getX() * getSpeed(), 
         getLastDirection().getY() * getSpeed()));[/color] // probably giving the second problem
}

I really hope you guys can help me solving this problem. 我真的希望你们能帮助我解决这个问题。

Thanks a lot! 非常感谢!

Sorry guys I fixed the problem. 抱歉,我解决了这个问题。 The reason was wrong position of the walls (they were colliding with the player at the start. 原因是墙壁的位置错误(开始时它们与玩家发生碰撞。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM