Example usage for com.badlogic.gdx.math Vector3 len

List of usage examples for com.badlogic.gdx.math Vector3 len

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Vector3 len.

Prototype

@Override
    public float len() 

Source Link

Usage

From source file:com.badlogic.gdx.physics.bullet.demo.BulletDemoMath.java

public static float angle(Vector3 first, Vector3 second) {
    double vDot = first.dot(second) / (first.len() * second.len());

    if (vDot < -1.0) {
        vDot = -1.0;/*from w ww .java2  s.  c  om*/
    }

    if (vDot > 1.0) {
        vDot = 1.0;
    }
    return ((float) (Math.acos(vDot)));
}

From source file:com.digitale.sim.Missile.java

License:Open Source License

public void update(float delta) {
    shotLife++;/* w  w w.  jav a 2s .c om*/

    destination = new Vector3(target.position);
    Vector3 direction = new Vector3(destination.sub(position));
    double length = direction.len();
    double npcPitch = Math.toDegrees(Math.asin((double) (direction.y / length)));
    double npcYaw;
    if (Math.abs(direction.z) < 0.00001) {
        // special case
        if (direction.x > 0) {
            npcYaw = Math.PI / 2.0;
        } else if (direction.x < 0) {
            npcYaw = -Math.PI / 2.0;
        } else {
            npcYaw = 0.0;
        }
    } else {
        npcYaw = Math.atan2(direction.x, direction.z);
    }

    yawAngle = (float) Math.toDegrees(npcYaw);
    pitchAngle = -(float) npcPitch;
    position.set(position.add(direction.nor().mul(20f)));

    if (shotTime + lifeTime < System.currentTimeMillis())
        hasLeftField = true;
}

From source file:com.digitale.sim.Simulation.java

License:Open Source License

public static void Collide(float frameDuration, Ship object1, Actor object2) {
    // Calculate the difference between the two objects.
    Vector3 difference = object1.position.sub(object2.position);
    float distanceAtFrameEnd = difference.len();
    frameDuration = frameDuration * 100000;
    // Calculate the distance that a collision would occur at.
    float collisionDistance = (object1.SHIP_RADIUS / 2f) + (object2.INVADER_RADIUS / 2f);

    // Check of the objects are closer that the collision distance.
    if (distanceAtFrameEnd < collisionDistance) {
        // Move both objects back to the exact point of collision.
        float millisecondsAfterCollision = MoveBackToCollisionPoint(frameDuration, object1, object2,
                distanceAtFrameEnd, collisionDistance);
        if (Stardust3d.DEBUG)
            System.out.println(LOG_TAG + "milli after coll: " + millisecondsAfterCollision);
        // Calculate the normal of the collision plane.
        Vector3 normalPlane = difference;
        normalPlane.nor();/*from w w w . j a  v a2 s.co  m*/

        // Calculate the collision plane.
        Vector3 collisionPlane = new Vector3(-normalPlane.x, normalPlane.y, normalPlane.z);

        // Calculate prior velocities relative the the collision plane and normal.
        float n_vel1 = (normalPlane.dot(object1.heading));
        float c_vel1 = (collisionPlane.dot(object1.heading));
        float n_vel2 = (normalPlane.dot(object2.heading));
        float c_vel2 = (collisionPlane.dot(object2.heading));

        // Calculate the scaler velocities of each object after the collision.
        float n_vel1_after = ((n_vel1 * (object1.mass - object2.mass)) + (2 * object2.mass * n_vel2))
                / (object2.mass + object1.mass);
        float n_vel2_after = ((n_vel2 * (object2.mass - object1.mass)) + (2 * object1.mass * n_vel1))
                / (object2.mass + object1.mass);
        //float velObject2Tangent_After = c_vel2;
        //float velObject1Tangent_After = c_vel1;

        // Convert the scalers to vectors by multiplying by the normalised plane vectors.
        Vector3 vec_n_vel2_after = normalPlane.mul(n_vel2_after);
        Vector3 vec_c_vel2 = collisionPlane.mul(c_vel2);
        Vector3 vec_n_vel1_after = normalPlane.mul(n_vel1_after);
        Vector3 vec_c_vel1 = collisionPlane.mul(c_vel1);

        // Combine the vectors back into a single vector in world space.
        Vector3 vel1_after = vec_n_vel1_after.add(vec_c_vel1);
        Vector3 vel2_after = vec_n_vel2_after.add(vec_c_vel2);

        // Reapply the move-back from before the collision (using the post collision velocity)
        Vector3 object1AdjustedPositionAfterCollision = object1.position
                .add(vel1_after.mul(millisecondsAfterCollision));
        Vector3 object2AdjustedPositionAfterCollision = object2.position
                .add(vel2_after.mul(millisecondsAfterCollision));

        // Set the objects new positions and velocities.
        object1.position = (object1AdjustedPositionAfterCollision);
        object1.heading = vel1_after;
        object2.position = object2AdjustedPositionAfterCollision;
        object2.heading = vel2_after;
    }
}

From source file:com.digitale.sim.Simulation.java

License:Open Source License

private static float MoveBackToCollisionPoint(float frameDuration, Ship object1, Actor object2,
        float distanceAtFrameEnd, float collisionDistance) {
    // Calculate the position of each object at the start of the frame.
    float object1PosAtFrameStart_X = (float) (object1.position.x - object1.heading.x * frameDuration);
    float object1PosAtFrameStart_Y = (float) (object1.position.y - object1.heading.y * frameDuration);
    float object1PosAtFrameStart_Z = (float) (object1.position.z - object1.heading.z * frameDuration);
    Vector3 object1PosAtFrameStart = new Vector3(object1PosAtFrameStart_X, object1PosAtFrameStart_Y,
            object1PosAtFrameStart_Z);//from   ww  w.j  a  va 2  s . c  o  m

    float object2PosAtFrameStart_X = (float) (object2.position.x - object2.heading.x * frameDuration);
    float object2PosAtFrameStart_Y = (float) (object2.position.y - object2.heading.y * frameDuration);
    float object2PosAtFrameStart_Z = (float) (object2.position.z - object2.heading.z * frameDuration);
    Vector3 object2PosAtFrameStart = new Vector3(object2PosAtFrameStart_X, object2PosAtFrameStart_Y,
            object2PosAtFrameStart_Z);

    // Calculate the distance between the objects at the start of the frame.
    Vector3 differenceAtFrameStart = object2PosAtFrameStart.sub(object1PosAtFrameStart);
    float distanceAtFrameStart = differenceAtFrameStart.len();

    // Calculate the total change in distance during the frame, and the required change to reach the collision.
    float distanceTotalDelta = distanceAtFrameEnd - distanceAtFrameStart;
    float distanceDeltaToCollision = collisionDistance - distanceAtFrameStart;

    // Calculate the percentage change to the collision and after the collision.
    float percentageDeltaToCollision = distanceDeltaToCollision / distanceTotalDelta;
    float percentageDeltaAfterCollision = 1 - percentageDeltaToCollision;

    // Calculte the time before and after the collision in the frame.
    double millisecondsToCollision = frameDuration * percentageDeltaToCollision;
    float millisecondsAfterCollision = (float) (frameDuration * percentageDeltaAfterCollision);

    // Calculate and move the objects to their positions at the point of collision.
    float object1PosAtCollision_X = (float) (object1PosAtFrameStart_X
            + object1.heading.x * millisecondsToCollision);
    float object1PosAtCollision_Y = (float) (object1PosAtFrameStart_Y
            + object1.heading.y * millisecondsToCollision);
    float object1PosAtCollision_Z = (float) (object1PosAtFrameStart_Z
            + object1.heading.z * millisecondsToCollision);
    Vector3 object1PosAtCollision = new Vector3(object1PosAtCollision_X, object1PosAtCollision_Y,
            object1PosAtCollision_Z);
    object1.position = (object1PosAtCollision);

    float object2PosAtCollision_X = (float) (object2PosAtFrameStart_X
            + object2.heading.x * millisecondsToCollision);
    float object2PosAtCollision_Y = (float) (object2PosAtFrameStart_Y
            + object2.heading.y * millisecondsToCollision);
    float object2PosAtCollision_Z = (float) (object2PosAtFrameStart_Y
            + object2.heading.z * millisecondsToCollision);
    Vector3 object2PosAtCollision = new Vector3(object2PosAtCollision_X, object2PosAtCollision_Y,
            object2PosAtCollision_Z);
    object2.position = (object2PosAtCollision);

    return millisecondsAfterCollision;
}

From source file:com.digitale.utils.Util.java

License:Open Source License

public static Vector3 lookAt(Vector3 target, Vector3 observer) {
    float playerx = target.x;
    float playery = target.y;
    float playerz = target.z;

    Vector3 playerVector = new Vector3(playerx, playery, playerz);
    Vector3 direction = new Vector3(playerVector.sub(observer));
    double length = direction.len();

    double npcPitch = Math.toDegrees(Math.asin((double) (direction.y / length)));
    double npcYaw;
    if (Math.abs(direction.z) < 0.00001) {
        // special case
        if (direction.x > 0) {
            npcYaw = Math.PI / 2.0;
        } else if (direction.x < 0) {
            npcYaw = -Math.PI / 2.0;
        } else {/*ww  w.  j ava  2  s . c  o m*/
            npcYaw = 0.0;
        }
    } else {
        npcYaw = Math.atan2(direction.x, direction.z);
    }

    npcYaw = Math.toDegrees(npcYaw);
    return new Vector3((float) (npcPitch), (float) (npcYaw), 0);
}

From source file:com.lyeeedar.Roguelike3D.Game.Actor.AI_Enemy_VFFG.java

License:Open Source License

public double angle(Vector3 v1, Vector3 v2) {
    Vector3 referenceForward = v1;
    Vector3 referenceRight = up.tmp2().crs(referenceForward);
    Vector3 newDirection = v2;/*from  w ww  .j  a v a2  s  .c om*/
    float angle = (float) Math.toDegrees(Math.acos(v1.dot(v2) / (v1.len() * v2.len())));
    float sign = (newDirection.dot(referenceRight) > 0.0f) ? 1.0f : -1.0f;
    float finalAngle = sign * angle;
    return finalAngle;
}

From source file:com.lyeeedar.Roguelike3D.Graphics.Lights.LightManager.java

License:Open Source License

private Color calculateLight(Vector3 l_vector, Color l_colour, float l_attenuation, float l_power,
        Vector3 n_dir) {// w ww  .  j  a  v a 2s.co  m
    float distance = l_vector.len();
    Vector3 l_dir = l_vector.cpy().div(distance);

    float NdotL = n_dir.dot(l_dir);
    float intensity = MathUtils.clamp(NdotL, 0.0f, 1.0f);

    float attenuation = 1.0f;
    if (l_attenuation != 0)
        attenuation /= (l_attenuation * distance + l_attenuation / 10 * distance * distance);

    return l_colour.mul(intensity).mul(l_power).mul(attenuation);
}

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

License:Apache License

/**
 * Calculates the angle in radians between a reference vector and the (plane) normal of the triangle.
 *
 * @param reference/*from w w  w.j  a  v a2  s  .  c o  m*/
 * @return
 */
public float getAngle(Vector3 reference) {
    float x = reference.x;
    float y = reference.y;
    float z = reference.z;
    Vector3 normal = reference;
    normal.set(a).sub(b).crs(b.x - c.x, b.y - c.y, b.z - c.z).nor();
    float angle = (float) Math.acos(normal.dot(x, y, z) / (normal.len() * Math.sqrt(x * x + y * y + z * z)));
    reference.set(x, y, z);
    return angle;
}

From source file:connex.Main.java

License:Open Source License

/**
 * @param timer// ww  w  .  j  a  v  a  2 s.  c  o  m
 */
private static void aitask(Timer timer) {

    timer.scheduleAtFixedRate(new TimerTask() {
        private int ticks;

        public void run() {

            try {
                //Class.forName("com.mysql.jdbc.Driver");

                System.out.println("start NPC ai");

                final Statement stmt;
                final Statement updatestmt;
                final Statement qrystmt;
                String query;

                //con = DriverManager.getConnection(MYSQLURL, serviceUser,
                //      servicePass);
                updatestmt = con.createStatement();
                stmt = con.createStatement();
                qrystmt = con.createStatement();
                // surnameUid=1000000 =hive ai scout
                ResultSet rs = stmt
                        .executeQuery("SELECT * FROM `npc` WHERE (surnames_uid=1000000)and hitpoints >0");
                // for each Hive ai scout
                while (rs.next()) {

                    if (rs.getString("uid") != null) {
                        float npcx = rs.getFloat("sysx");
                        float npcy = rs.getFloat("sysy");
                        float npcz = rs.getFloat("sysz");
                        BigInteger npcUID = BigInteger.valueOf(Long.valueOf(rs.getString("uid")));
                        int locx = rs.getInt("x");
                        int locy = rs.getInt("y");
                        int locz = rs.getInt("z");
                        // find nearest player in range
                        query = "select uid,firstname, surname, sysx, sysy, sysz," + " MIN(ABS(sysx-" + npcx
                                + ")) +MIN(ABS(sysy-" + npcy + ")) +MIN(ABS(sysz-" + npcz + ")) as dist"
                                + " from player_char" + " where (x=" + locx + " and y=" + locy + " and z="
                                + locz + " and status=1" + ")" + " group by sysx,sysy,sysz"
                                + " order by dist limit 1";

                        //if (DEBUG)   System.out.println(query);
                        ResultSet playerdata = qrystmt.executeQuery(query);

                        Vector3 npcVector = new Vector3(npcx, npcy, npcz);
                        while (playerdata.next()) {
                            float playerx = playerdata.getFloat("sysx");
                            float playery = playerdata.getFloat("sysy");
                            float playerz = playerdata.getFloat("sysz");
                            int playerUID = playerdata.getInt("uid");
                            Vector3 playerVector = new Vector3(playerx, playery, playerz);
                            Vector3 direction = new Vector3(playerVector.sub(npcVector));
                            double length = direction.len();
                            if (DEBUG)
                                System.out.println(length);
                            if (length < 2000) {
                                if (DEBUG)
                                    System.out.println("moving to player");
                                double npcPitch = Math.toDegrees(Math.asin((double) (direction.y / length)));
                                double npcYaw;
                                if (Math.abs(direction.z) < 0.00001) {
                                    // special case
                                    if (direction.x > 0) {
                                        npcYaw = Math.PI / 2.0;
                                    } else if (direction.x < 0) {
                                        npcYaw = -Math.PI / 2.0;
                                    } else {
                                        npcYaw = 0.0;
                                    }
                                } else {
                                    npcYaw = Math.atan2(direction.x, direction.z);
                                }

                                npcYaw = Math.toDegrees(npcYaw);
                                direction = (direction.nor().mul(10f));
                                query = "update npc set pitchangle=" + -npcPitch + ", yawangle=" + npcYaw
                                        + ",lastupdate=(select Now() from landscape limit 1)" + ",sysx=sysx+"
                                        + direction.x + ",sysy=sysy+" + direction.y + ",sysz=sysz+"
                                        + direction.z + "where uid=" + npcUID;
                                if (DEBUG)
                                    System.out.println(query);
                                int affected = updatestmt.executeUpdate(query);
                                //attack players in range
                                if (length < 200 && ticks == 1) {
                                    query = "update player_char set hitpoints=hitpoints-50, lastattackeruid=-"
                                            + npcUID
                                            + ",lastupdate=(select Now() from landscape limit 1) where uid="
                                            + playerUID;
                                    if (DEBUG)
                                        System.out.println(query);
                                    int newaffected = updatestmt.executeUpdate(query);
                                }
                            }

                        }

                    }
                }

                ticks++;
                if (ticks == 10)
                    ticks = 0;
                //   con.close();
                stmt.close();
                updatestmt.close();
                qrystmt.close();

            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }, 0, 200);

}

From source file:connex.NPCtasks.java

License:Open Source License

/**
 * @param stmt/*w  w w  . java 2  s. c  o m*/
 * @param updatestmt
 * @throws SQLException
 */
private static void respawnShieldDisruptor(Statement stmt, Statement updatestmt) throws SQLException {
    String query;
    ResultSet rs;
    // surname id 1 is a shield disruptor hive ai
    rs = stmt.executeQuery("SELECT count(*)as count FROM `npc` WHERE (surnames_uid=1)and shipname!='dead'");
    while (rs.next()) {
        int count = Integer.valueOf(rs.getString("count"));

        System.out.println("count " + count);
        if (count <= 20) {
            double s = Math.random();
            double t = Math.random();
            double u = s * Math.PI * 2;
            double z = t * 2 - 1;
            double r = Math.sqrt(1 - z * z);
            double x = r * Math.cos(u);
            double y = r * Math.sin(u);
            //point disruptor at station
            Vector3 npcVector = new Vector3((float) (x), (float) (y), (float) (z));
            Vector3 targetVector = new Vector3(0, 0, 0);
            Vector3 direction = new Vector3(targetVector.sub(npcVector));
            double length = direction.len();
            System.out.println(length);

            double npcPitch = -Math.toDegrees(Math.asin((double) (direction.y / length)));
            double npcYaw;
            if (Math.abs(direction.z) < 0.00001) {
                // special case
                if (direction.x > 0) {
                    npcYaw = Math.PI / 2.0;
                } else if (direction.x < 0) {
                    npcYaw = -Math.PI / 2.0;
                } else {
                    npcYaw = 0.0;
                }
            } else {
                npcYaw = Math.atan2(direction.x, direction.z);
            }
            npcYaw = Math.toDegrees(npcYaw);

            query = "INSERT INTO npc (`UID`, `race`, `x`, `y`, `z`, `is_static`, `firstnames_uid`, `surnames_uid`,"
                    + " `sysx`, `sysy`, `sysz`, `hitpoints`, `faction`, `stamina`, `intelligence`, `social`, `dexterity`, `leadership`, `recuperation`, `exp`,"
                    + "`firstattackeruid`,`lastattackeruid`,`expvalue`,`creditvalue`,`status`,`yawangle`,`pitchangle`,`shipname`,`lastupdate`)"
                    + " VALUES (NULL, 'aih', '1', '1', '10', '1', '7168', '1', '" + x * 1500 + "', '" + y * 1500
                    + "', '" + z * 1500
                    + "', '500', 'aihive', '1', '1', '1', '1', '1', '1', '0','0','0','100','1000','1','"
                    + npcYaw + "','" + npcPitch + "','shielddisruptor',(select NOW() from landscape limit 1))";
            System.out.println(query);
            int affected = updatestmt.executeUpdate(query);

        }
    }
}