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

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

Introduction

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

Prototype

@Override
    public Vector3 nor() 

Source Link

Usage

From source file:br.com.raphaelbruno.game.zombieinvaders.vr.tween.GameObjectAccessor.java

License:Apache License

@Override
public int getValues(GameObject target, int tweenType, float[] returnValues) {
    Vector3 position = target.transform.getTranslation(new Vector3());
    Vector3 rotation = new Vector3();

    switch (tweenType) {
    case XYZ:/*w w  w  .j  a  va 2s. c  o m*/
        returnValues[0] = position.x;
        returnValues[1] = position.y;
        returnValues[2] = position.z;
        return 3;

    case ROTATION:
        returnValues[0] = target.transform.getRotation(new Quaternion()).getAxisAngle(rotation)
                * rotation.nor().y;
        return 1;

    case ALPHA:
        returnValues[0] = target.blending.opacity;
        return 1;

    default:
        assert false;
        return -1;
    }
}

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

License:Open Source License

public void update(float delta) {
    shotLife++;//www . j  a  v a 2  s.  c  o m

    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();

        // 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;// w  ww .  j  a  v a2s .c o m
        object2.position = object2AdjustedPositionAfterCollision;
        object2.heading = vel2_after;
    }
}

From source file:com.lyeeedar.Roguelike3D.Graphics.Models.Shapes.java

License:Open Source License

public static Vector3 orthoNormalize(Vector3 vec1, Vector3 vec2) {
    vec1.nor();
    vec1.mul(vec2.dot(vec1));//from  w  w  w .  j a  va 2  s. co  m

    vec2.sub(vec1);

    vec2.nor();
    return vec2;
}

From source file:com.mbrlabs.mundus.commons.terrain.Terrain.java

License:Apache License

/**
 * Get Normal at x,y point of terrain/*from www.j  a va 2s .  co m*/
 * 
 * @param x
 *            the x coord on terrain
 * @param y
 *            the y coord on terrain( actual z)
 * @return the normal at the point of terrain
 */
public Vector3 getNormalAt(int x, int y) {
    Vector3 out = new Vector3();
    // handle edges of terrain
    int xP1 = (x + 1 >= vertexResolution) ? vertexResolution - 1 : x + 1;
    int yP1 = (y + 1 >= vertexResolution) ? vertexResolution - 1 : y + 1;
    int xM1 = (x - 1 < 0) ? 0 : x - 1;
    int yM1 = (y - 1 < 0) ? 0 : y - 1;

    float hL = heightData[y * vertexResolution + xM1];
    float hR = heightData[y * vertexResolution + xP1];
    float hD = heightData[yM1 * vertexResolution + x];
    float hU = heightData[yP1 * vertexResolution + x];
    out.x = hL - hR;
    out.y = 2;
    out.z = hD - hU;
    out.nor();
    return out;
}

From source file:com.mygdx.game.scene.GameScene.java

License:Apache License

public void setToSceneCamera(GhostCamera camera) {
    Vector3 direction = new Vector3(V3_DOWN);
    direction.rotate(Vector3.X, sceneCamera.rotation.x);
    direction.rotate(Vector3.Z, sceneCamera.rotation.z);
    direction.rotate(Vector3.Y, sceneCamera.rotation.y);
    direction.nor();

    camera.fieldOfView = sceneCamera.fov;
    camera.targetPosition.set(sceneCamera.position);
    camera.targetDirection.set(direction);
    camera.targetUp.set(Vector3.Y);
    camera.snapToTarget();//from   w  w  w . ja va  2  s .co  m
    camera.update();
}

From source file:com.mygdx.game.utilities.ModelFactory.java

License:Apache License

/**
 * Translate each vertex along its normal by specified amount.
 *
 * @param model//from   ww w  .j  a  v a  2  s . co m
 * @param amount
 */
public static void fatten(Model model, float amount) {
    Vector3 pos = new Vector3();
    Vector3 nor = new Vector3();
    for (Node node : model.nodes) {
        for (NodePart n : node.parts) {
            Mesh mesh = n.meshPart.mesh;
            FloatBuffer buf = mesh.getVerticesBuffer();
            int lastFloat = mesh.getNumVertices() * mesh.getVertexSize() / 4;
            int vertexFloats = (mesh.getVertexSize() / 4);
            VertexAttribute posAttr = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position);
            VertexAttribute norAttr = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Normal);
            if (posAttr == null || norAttr == null) {
                throw new IllegalArgumentException("Position/normal vertex attribute not found");
            }
            int pOff = posAttr.offset / 4;
            int nOff = norAttr.offset / 4;

            for (int i = 0; i < lastFloat; i += vertexFloats) {
                pos.x = buf.get(pOff + i);
                pos.y = buf.get(pOff + i + 1);
                pos.z = buf.get(pOff + i + 2);

                nor.x = buf.get(nOff + i);
                nor.y = buf.get(nOff + i + 1);
                nor.z = buf.get(nOff + i + 2);

                nor.nor().scl(amount);

                buf.put(pOff + i, pos.x + nor.x);
                buf.put(pOff + i + 1, pos.y + nor.y);
                buf.put(pOff + i + 2, pos.z + nor.z);
            }
        }
    }
}

From source file:connex.Main.java

License:Open Source License

/**
 * @param timer//www  .j av  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:interpolationtests.CatmullRomSpline.java

License:Apache License

/** Returns all tangents for the points in a path. Same semantics as getPath.
 * //from ww  w .  j  ava2 s . c  om
 * @param numPoints number of points returned for a segment
 * @return the tangents of the points in the path */
public List<Vector3> getTangents(int numPoints) {
    ArrayList<Vector3> tangents = new ArrayList<Vector3>();

    if (controlPoints.size() < 4)
        return tangents;

    Vector3 T1 = new Vector3();
    Vector3 T2 = new Vector3();

    for (int i = 1; i <= controlPoints.size() - 3; i++) {
        float increment = 1.0f / (numPoints + 1);
        float t = increment;

        T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i - 1)).mul(0.5f);
        T2.set(controlPoints.get(i + 2)).sub(controlPoints.get(i)).mul(0.5f);

        tangents.add(new Vector3(T1).nor());

        for (int j = 0; j < numPoints; j++) {
            float h1 = 6 * t * t - 6 * t; // calculate basis function 1
            float h2 = -6 * t * t + 6 * t; // calculate basis function 2
            float h3 = 3 * t * t - 4 * t + 1; // calculate basis function 3
            float h4 = 3 * t * t - 2 * t; // calculate basis function 4

            Vector3 point = new Vector3(controlPoints.get(i)).mul(h1);
            point.add(controlPoints.get(i + 1).tmp().mul(h2));
            point.add(T1.tmp().mul(h3));
            point.add(T2.tmp().mul(h4));
            tangents.add(point.nor());
            t += increment;
        }
    }

    if (controlPoints.size() >= 4)
        tangents.add(T1.set(controlPoints.get(controlPoints.size() - 1))
                .sub(controlPoints.get(controlPoints.size() - 3)).mul(0.5f).cpy().nor());

    return tangents;
}

From source file:interpolationtests.CatmullRomSpline.java

License:Apache License

/** Returns all tangent's normals in 2D space for the points in a path. The controlpoints have to lie in the x/y plane for this
 * to work. Same semantics as getPath.//  w  w  w.  j av a 2 s.  c o m
 * 
 * @param numPoints number of points returned for a segment
 * @return the tangents of the points in the path */
public List<Vector3> getTangentNormals2D(int numPoints) {
    ArrayList<Vector3> tangents = new ArrayList<Vector3>();

    if (controlPoints.size() < 4)
        return tangents;

    Vector3 T1 = new Vector3();
    Vector3 T2 = new Vector3();

    for (int i = 1; i <= controlPoints.size() - 3; i++) {
        float increment = 1.0f / (numPoints + 1);
        float t = increment;

        T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i - 1)).mul(0.5f);
        T2.set(controlPoints.get(i + 2)).sub(controlPoints.get(i)).mul(0.5f);

        Vector3 normal = new Vector3(T1).nor();
        float x = normal.x;
        normal.x = normal.y;
        normal.y = -x;
        tangents.add(normal);

        for (int j = 0; j < numPoints; j++) {
            float h1 = 6 * t * t - 6 * t; // calculate basis function 1
            float h2 = -6 * t * t + 6 * t; // calculate basis function 2
            float h3 = 3 * t * t - 4 * t + 1; // calculate basis function 3
            float h4 = 3 * t * t - 2 * t; // calculate basis function 4

            Vector3 point = new Vector3(controlPoints.get(i)).mul(h1);
            point.add(controlPoints.get(i + 1).tmp().mul(h2));
            point.add(T1.tmp().mul(h3));
            point.add(T2.tmp().mul(h4));
            point.nor();
            x = point.x;
            point.x = point.y;
            point.y = -x;
            tangents.add(point);
            t += increment;
        }
    }

    return tangents;
}