box2d can't get listen

Anything libgdx related goes here!

box2d can't get listen

Postby TruePlayer » Fri Aug 30, 2019 11:45 pm

I create bodies and the world, I want to get a listener collisions. but my code does not work, because the conditions of a non-null object are not met:

Code: Select all
public class ActorObject extends Actor {

    protected Sprite sprite;
    protected Body body;
    private World world;

    public ActorObject(World world){
        this.world = world;
    }

    protected void createBody(Shape shape, BodyDef.BodyType type){
        BodyDef bDef = new BodyDef();
        bDef.position.set(getX(), getY());
        bDef.type = type;

        body = world.createBody(bDef);

        FixtureDef fDef = new FixtureDef();
        fDef.shape = shape;
        fDef.restitution = 1;
        fDef.density = 1000; //килограм

        body.createFixture(fDef);
    }

    @Override
    public void act(float delta) {
        setPosition(body.getPosition().x - this.getWidth()/2,
                    body.getPosition().y - this.getHeight()/2);
        super.act(delta);
    }
}


Code: Select all
public class Ball extends ActorObject {


    public Ball(World world) {
        super(world);
        setBounds(320/2 - this.getWidth()/2, 450, 32, 32);
        CircleShape shape = new CircleShape();
        shape.setRadius(16);
        createBody(shape, BodyDef.BodyType.DynamicBody);
        this.setUserObject("ball");

    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        setPosition(body.getPosition().x-16,body.getPosition().y-16);
    }
}


Code: Select all
public class Player extends ActorObject {


    public Player(World world) {
        super(world);
        setPosition(320/2f - getWidth()/2, 480/3f-getHeight()/2);
        CircleShape shape = new CircleShape();
        shape.setRadius(32);
        createBody(shape, BodyDef.BodyType.KinematicBody);
        setUserObject("player");
    }

    @Override
    public void act(float delta) {
        body.setTransform(this.getX(), this.getY(), 0);
    }
}


Code: Select all
public class MyListener implements ContactListener {
    @Override
    public void beginContact(Contact contact) {
        Fixture fixA = contact.getFixtureA();
        Fixture fixB = contact.getFixtureB();
        if(fixA.getBody().getUserData()!=null && fixB.getBody().getUserData()!=null){
            System.out.println("GOT IT");
        }
    }

    @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 beginContact(Contact contact) {
Fixture fixA = contact.getFixtureA();
Fixture fixB = contact.getFixtureB();
if(fixA.getBody().getUserData()!=null && fixB.getBody().getUserData()!=null){
System.out.println("GOT IT");
}
}

in this function, if we println above the if condition
then I see messages in the console, but if, as shown in the code, nothing happens, as if my objects return null but why?
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm

Re: box2d can't get listen

Postby QuiIIraven » Sat Aug 31, 2019 6:19 am

and where exactly do you think that you set the user data of the box2d body?

Seems again like a useless thread from you. I repeat myself from another thread.
Learn how to debug!
Set a breakpoints in begin contact and analyze your bodies.
Set another one in your Player constructor and step through it. Analyze your body again and you will see your mistake.
QuiIIraven
 
Posts: 81
Joined: Sat Jun 02, 2018 4:59 pm

Re: box2d can't get listen

Postby TruePlayer » Sat Aug 31, 2019 10:01 pm

I can debug and your advice turned out to be useless unfortunately
for the same as me: you need to use body.setUserData ("ball"); instead of setUserObject ("ball");
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm

Re: box2d can't get listen

Postby QuiIIraven » Sun Sep 01, 2019 10:45 am

TruePlayer wrote:I can debug and your advice turned out to be useless unfortunately
for the same as me: you need to use body.setUserData ("ball"); instead of setUserObject ("ball");


Since you call my advice "useless" I am not sure if you really understand what you did wrong.

By stepping through your Ball's constructor you should have noticed that your "this.setUserObject("ball")" call did not set the userData of the body.
The reason is that you call the setUserObject method of the actor class that your ball class is extending. You are not calling the box2d body setUserData method.

That is what I wanted you to find out via the debugger and I hope you understand the difference between actor and box2d body.
QuiIIraven
 
Posts: 81
Joined: Sat Jun 02, 2018 4:59 pm

Re: box2d can't get listen

Postby TruePlayer » Sun Sep 01, 2019 1:07 pm

I went through the debugger class player and saw clearly that userData was installed
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm


Return to Libgdx

Who is online

Users browsing this forum: MSN [Bot] and 1 guest