Example usage for org.apache.commons.math3.geometry.euclidean.twod Vector2D normalize

List of usage examples for org.apache.commons.math3.geometry.euclidean.twod Vector2D normalize

Introduction

In this page you can find the example usage for org.apache.commons.math3.geometry.euclidean.twod Vector2D normalize.

Prototype

public Vector2D normalize() throws MathArithmeticException 

Source Link

Usage

From source file:haxball.networking.ServerMainLoop.java

@Override
public void run() {

    resetField();/*from  w w  w.  j  a  v  a2s .co  m*/
    byte score0 = 0, score1 = 0;

    while (!stopped) {
        // Move players
        for (Player player : players) {

            byte input = player.getLastInput();

            float vx = 0;
            float vy = 0;

            if ((input & 0b00_00_00_01) != 0) {
                vy += speed;
            }
            if ((input & 0b00_00_00_10) != 0) {
                vx -= speed;
            }
            if ((input & 0b00_00_01_00) != 0) {
                vy -= speed;
            }
            if ((input & 0b00_00_10_00) != 0) {
                vx += speed;
            }
            if ((input & 0b00_01_00_00) != 0) {
                player.setShooting(true);
            } else {
                player.setShooting(false);
            }

            Vector2D v = new Vector2D(vx, vy);
            if (v.distance(Vector2D.ZERO) > 0) {
                v = v.normalize().scalarMultiply(speed);
            }

            player.velocity = player.velocity.add(v.subtract(player.velocity).scalarMultiply(acceleration));
            player.position = player.position.add(player.velocity);
        }

        // Move ball
        ball.velocity = ball.velocity.scalarMultiply(1 - friction);
        ball.position = ball.position.add(ball.velocity);

        // Check for collisions
        for (Player p : players) {
            // collisions between players
            for (Player p0 : players) {
                if (!p.equals(p0)) {
                    p.uncollide(p0);
                }
            }
            p.uncollide(ball);
            p.setInsideMap(true);

            if (p.isShooting()
                    && p.position.distance(ball.position) < (p.getRadius() + ball.getRadius()) * 1.4f) {
                Vector2D norm = ball.position.subtract(p.position).normalize();
                ball.velocity = ball.velocity.add(norm.scalarMultiply(shootingPower));
            }

        }
        int result;
        if ((result = ball.setInsideMap(false)) >= 0) {
            if (result == 0) {
                score0++;
            } else {
                score1++;
            }
            resetField();
        }

        // send position to every connection
        for (ConnectionHandler handler : connectionHandlers) {
            handler.writeState(ball, score0, score1, players);
        }
        // sleep
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    } // while !stopped
}

From source file:edu.unc.cs.gamma.rvo.Agent.java

/**
 * Solves a two-dimensional linear program subject to linear constraints
 * defined by lines and a circular constraint.
 *
 * @param lines                Lines defining the linear constraints.
 * @param optimizationVelocity The optimization velocity.
 * @param optimizeDirection    True if the direction should be optimized.
 * @return The number of the line on which it fails, or the number of lines
 * if successful.//from ww w  .  j  a  v  a 2 s  .  c  o  m
 */
private int linearProgram2(List<Line> lines, Vector2D optimizationVelocity, boolean optimizeDirection) {
    if (optimizeDirection) {
        // Optimize direction. Note that the optimization velocity is of unit length in this case.
        newVelocity = optimizationVelocity.scalarMultiply(maxSpeed);
    } else if (optimizationVelocity.getNormSq() > maxSpeed * maxSpeed) {
        // Optimize closest point and outside circle.
        newVelocity = optimizationVelocity.normalize().scalarMultiply(maxSpeed);
    } else {
        // Optimize closest point and inside circle.
        newVelocity = optimizationVelocity;
    }

    for (int lineNo = 0; lineNo < lines.size(); lineNo++) {
        if (RVOMath.det(lines.get(lineNo).direction, lines.get(lineNo).point.subtract(newVelocity)) > 0.0) {
            // Result does not satisfy constraint i. Compute new optimal
            // result.
            final Vector2D tempResult = newVelocity;
            if (!linearProgram1(lines, lineNo, optimizationVelocity, optimizeDirection)) {
                newVelocity = tempResult;

                return lineNo;
            }
        }
    }

    return lines.size();
}

From source file:org.kalypso.model.wspm.ewawi.utils.profiles.EwawiProfilePoint.java

private Vector2D getNormalizedProfileAxis() {
    final double xLeft = m_left.getRechtswert().doubleValue();
    final double yLeft = m_left.getHochwert().doubleValue();

    final double xRight = m_right.getRechtswert().doubleValue();
    final double yRight = m_right.getHochwert().doubleValue();

    final Vector2D profileAxis = new Vector2D(xRight - xLeft, yRight - yLeft);

    return profileAxis.normalize();
}