Collision in Libgdx box2D, not suitable for some bodies

I am working on my first game using libgdx with box2d. I am using a debugging tool to validate objects. I created several similar cars. Each car has a main body, which is a large polygon of 6 points (about 1 meter in length, 0.7 meters in height) and has 2 wheels connected by revolving joints.

The main car has a cannon and a machine gun, also through revolving units.

The problem I am facing is that most of the car does not collide as intended. When 2 cars fall into each other, they overlap, for example:

Bodies colliding

Some notes:

  • Wheels and guns (smaller figures) do a great job with a collision. When the wheels touch the bodies stop.
  • If I detect a collision using code, the collision actually occurs
  • I tried this with a smaller object (a bullet fired from a machine gun), and I set the "isBullet" property of a true object, as I saw in another post ( not a correct collision in box2d ), but had the same result (the bullet is surrounded in red):

Bullet object colliding

Here is the code I use to create the bodies:

protected Body createBody(Material material, Shape shape, BodyType type, WorldWrapper world) { BodyDef bodyDef = new BodyDef(); bodyDef.type = type; bodyDef.position.set(initialPosition); Body body = world.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = this.material.getDensity(); fixtureDef.friction = this.material.getFriction(); fixtureDef.restitution = this.material.getRestitution(); fixtureDef.shape = shape; body.createFixture(fixtureDef); return body; } 

The car moves forward using engines on the steep joints of the wheels, constructed as follows:

 public void goForward() { for (RevoluteJoint joint : wheelJoints) { joint.enableMotor(true); joint.setMotorSpeed(-this.engineSpeed); joint.setMaxMotorTorque(this.engineTorque); } } 

I use the following values:

 Density = 2500; Restitution = 0; Friction = 0.1; BodyType = Dynamic; 

I use a world step of 1/60 second with an Iteration = 6 and positionIterations = 2

Any idea what I'm missing here?

+6
source share
1 answer

The main part of your car (polygon) is concave. Box2D has problems with concave polygons or if the vertices are in clockwise order. Try building these cars with two rectangles that should work.

Btw: It seems you are using an older version of libgdx. The current night clock libgdx has an additional algorithm in the function polygon.set, which automatically calculates the convex hull in the counterclockwise order of the given vertices. Therefore, using this version should also fix your problem.

+5
source

Source: https://habr.com/ru/post/945714/


All Articles