Example usage for com.badlogic.gdx.math.collision Ray Ray

List of usage examples for com.badlogic.gdx.math.collision Ray Ray

Introduction

In this page you can find the example usage for com.badlogic.gdx.math.collision Ray Ray.

Prototype

public Ray(Vector3 origin, Vector3 direction) 

Source Link

Document

Constructor, sets the starting position of the ray and the direction.

Usage

From source file:com.mygdx.game.pathfinding.NavMeshPointPath.java

License:Apache License

/**
 * Calculate the shortest path through the navigation mesh triangles.
 *
 * @param trianglePath/*w w w  . j  ava 2  s  .c o m*/
 */
public void calculateForGraphPath(NavMeshGraphPath trianglePath) {
    clear();
    nodes = trianglePath.nodes;
    this.start = new Vector3(trianglePath.start);
    this.end = new Vector3(trianglePath.end);
    this.startTri = trianglePath.startTri;

    // Check that the start point is actually inside the start triangle, if not, project it to the closest
    // triangle edge. Otherwise the funnel calculation might generate spurious path segments.
    Ray ray = new Ray(tmp1.set(V3_UP).scl(1000).add(start), tmp2.set(V3_DOWN));
    if (!Intersector.intersectRayTriangle(ray, startTri.a, startTri.b, startTri.c, null)) {
        float minDst = Float.POSITIVE_INFINITY;
        Vector3 projection = new Vector3();
        Vector3 newStart = new Vector3();
        float dst;
        // A-B
        if ((dst = GeometryUtils.nearestSegmentPointSquareDistance(projection, startTri.a, startTri.b,
                start)) < minDst) {
            minDst = dst;
            newStart.set(projection);
        }
        // B-C
        if ((dst = GeometryUtils.nearestSegmentPointSquareDistance(projection, startTri.b, startTri.c,
                start)) < minDst) {
            minDst = dst;
            newStart.set(projection);
        }
        // C-A
        if ((dst = GeometryUtils.nearestSegmentPointSquareDistance(projection, startTri.c, startTri.a,
                start)) < minDst) {
            minDst = dst;
            newStart.set(projection);
        }
        start.set(newStart);
    }
    if (nodes.size == 0) {
        addPoint(start, startTri);
        addPoint(end, startTri);
    } else {
        lastEdge = new Edge(nodes.get(nodes.size - 1).getToNode(), nodes.get(nodes.size - 1).getToNode(), end,
                end);
        calculateEdgePoints();
    }
}