Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / views / LineRenderer.java
package gameEngine.views;
import org.joml.Vector3f;

public class LineRenderer {
    private final Sprite lineSprite;

    public LineRenderer(String spritePath) {
        this.lineSprite = new Sprite(spritePath);
    }

    public void updateLine(Vector3f start, Vector3f end, float thickness) {
        Vector3f linePosition = calculatePosition(start, end);
        float lineLength = calculateDistance(start, end);
        float angle = calculateAngle(start, end);

        linePosition.x -= (float) (lineLength / 2 * Math.cos(Math.toRadians(angle)));
        linePosition.y -= (float) (lineLength / 2 * Math.sin(Math.toRadians(angle)));

        lineSprite.setPosition(linePosition.x + 8, linePosition.y + 8);
        lineSprite.setRotation(new Vector3f(0, 0, angle));
        lineSprite.setScale(new Vector3f(lineLength, thickness, 1));
        lineSprite.update();
    }

    public void render() {
        lineSprite.render();
    }

    public Vector3f calculatePosition(Vector3f posA, Vector3f posB) {
        return new Vector3f((posA.x + posB.x) / 2, (posA.y + posB.y) / 2, 0);
    }

    private float calculateDistance(Vector3f posA, Vector3f posB) {
        return (float) Math.sqrt(Math.pow(posB.x - posA.x, 2) + Math.pow(posB.y - posA.y, 2));
    }

    private float calculateAngle(Vector3f posA, Vector3f posB) {
        return (float) Math.toDegrees(Math.atan2(posB.y - posA.y, posB.x - posA.x));
    }
}