summaryrefslogtreecommitdiff
path: root/core/src/org/snoopdesigns/endless/physics/Box2DDebugRenderer.java
blob: 9d7d4dd9f7052037be1669496504d446d37a20be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.snoopdesigns.endless.physics;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.utils.Array;
import org.snoopdesigns.endless.context.Context;
import org.snoopdesigns.endless.renderer.Renderer;

public class Box2DDebugRenderer implements Renderer {

    private com.badlogic.gdx.physics.box2d.Box2DDebugRenderer debugRenderer;
    private BitmapFont font;
    private SpriteBatch batch;

    @Override
    public void create() {
        debugRenderer = new com.badlogic.gdx.physics.box2d.Box2DDebugRenderer();
        debugRenderer.setDrawBodies(true);
        debugRenderer.setDrawVelocities(true);
        debugRenderer.setDrawAABBs(true);

        font = new BitmapFont(Gdx.files.internal("calibri.fnt"), false);
        font.setColor(Color.CYAN);
        font.getData().setScale(0.15f);

        batch = new SpriteBatch();
    }

    @Override
    public void render() {
        debugRenderer.render(
                Context.getInstance().getWorldContext().getWorld(),
                Context.getInstance().getCameraContext().getCameraProjection());

        final Array<Body> bodies = new Array<>();
        Context.getInstance().getWorldContext().getWorld().getBodies(bodies);

        batch.setProjectionMatrix(Context.getInstance().getCameraContext().getCameraProjection());
        batch.begin();
        bodies.forEach(body -> {
            final String debugText = String.format("""
                p %.1f %.1f
                a %.1f
                s %.1f m/s
                v %.1f %.1f
                """,
                    body.getPosition().x,
                    body.getPosition().y,
                    body.getAngle(),
                    body.getLinearVelocity().len(),
                    body.getLinearVelocity().x,
                    body.getLinearVelocity().y);
            font.draw(batch, debugText, body.getPosition().x + 5f, body.getPosition().y - 5f);
        });
        batch.end();
    }

    @Override
    public void dispose() {
        batch.dispose();
    }
}