Using box2d to detect collisions but ignore forces

I use libgdx and box2d to detect collisions, but I want some kind of collision to be detected, but no forces are played out.

For example, I want to determine when a character collides with a coin, but does not want the coin to affect the player’s movements.

Is this possible with box2d? If so, how can I do this?

+4
source share
2 answers

I'm not sure what you want, but if I understand what you want to do, you can create a type of gauge device, this pseudo example:

FixtureDef fd1 = new FixtureDef();
//fd1.friction = 0.5f;

PolygonShape pSTest = new PolygonShape();

//size you want for example:
pSTest.setAsBox(4f / PIXEL_POR_METRO, 
                1f / PIXEL_POR_METRO, 
                new Vector2(22f / PIXEL_POR_METRO, -1f / PIXEL_POR_METRO),
                0f);

fd1.shape = pSTest;
fd1.isSensor = true;

yourBody.createFixture(fd1).setUserData("yourId");

the ContactListener interface looks like preSolve

Please note that the sensor is not intended for sensors.

Information

setAsBox(float hx, float hy, Vector2 center, float angle)

Parameters:
hx the half-width.
hy the half-height.
center the center of the box in local coordinates.
angle the rotation in radians of the box in local coordinates.
+3

: https://gamedev.stackexchange.com/a/22542

Box2D:

. , , , . , , , ( ). Box2D .

Box2D 16 . , . , , . , , ,

. ( ) ,

+1

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


All Articles