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

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

Introduction

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

Prototype

public Vector3 add(float values) 

Source Link

Document

Adds the given value to all three components of the vector.

Usage

From source file:br.com.raphaelbruno.game.zombieinvaders.vr.model.ScreenBase.java

License:Apache License

public GameObject getObject(int screenX, int screenY) {
    Ray ray = camera.getPickRay(screenX, screenY);
    GameObject result = null;/*from  w  ww .j  ava 2  s  .c  o  m*/
    float distance = -1;
    for (ModelInstance item : instances) {
        if (item instanceof GameObject) {
            GameObject gameObject = (GameObject) item;
            if (gameObject.enabled) {
                Vector3 position = gameObject.transform.getTranslation(new Vector3());
                gameObject.updateBox();
                position.add(gameObject.center);
                float dist2 = ray.origin.dst2(position);
                if (distance >= 0f && dist2 > distance)
                    continue;
                if (Intersector.intersectRayBoundsFast(ray, gameObject.bounds)) {
                    result = gameObject;
                    distance = dist2;
                }
            }
        }
    }
    return result;
}

From source file:com.badlogic.gdx.tests.g3d.HeightField.java

public Vector3 getPositionAt(Vector3 out, int x, int y) {
    final float dx = (float) x / (float) (width - 1);
    final float dy = (float) y / (float) (height - 1);
    final float a = data[y * width + x];
    out.set(corner00).lerp(corner10, dx).lerp(tmpV1.set(corner01).lerp(corner11, dx), dy);
    out.add(tmpV1.set(magnitude).scl(a));
    return out;//from   w  w w.ja va  2  s  .c  o m
}

From source file:com.badlogic.gdx.tests.g3d.HeightField.java

public Vector3 getWeightedNormalAt(Vector3 out, int x, int y) {
    // This commented code is based on http://www.flipcode.com/archives/Calculating_Vertex_Normals_for_Height_Maps.shtml
    // Note that this approach only works for a heightfield on the XZ plane with a magnitude on the y axis
    // float sx = data[(x < width - 1 ? x + 1 : x) + y * width] + data[(x > 0 ? x-1 : x) + y * width];
    // if (x == 0 || x == (width - 1))
    // sx *= 2f;/*from  www . jav  a 2  s . c  o m*/
    // float sy = data[(y < height - 1 ? y + 1 : y) * width + x] + data[(y > 0 ? y-1 : y) * width + x];
    // if (y == 0 || y == (height - 1))
    // sy *= 2f;
    // float xScale = (corner11.x - corner00.x) / (width - 1f);
    // float zScale = (corner11.z - corner00.z) / (height - 1f);
    // float yScale = magnitude.len();
    // out.set(-sx * yScale, 2f * xScale, sy*yScale*xScale / zScale).nor();
    // return out;

    // The following approach weights the normal of the four triangles (half quad) surrounding the position.
    // A more accurate approach would be to weight the normal of the actual triangles.
    int faces = 0;
    out.set(0, 0, 0);

    Vector3 center = getPositionAt(tmpV2, x, y);
    Vector3 left = x > 0 ? getPositionAt(tmpV3, x - 1, y) : null;
    Vector3 right = x < (width - 1) ? getPositionAt(tmpV4, x + 1, y) : null;
    Vector3 bottom = y > 0 ? getPositionAt(tmpV5, x, y - 1) : null;
    Vector3 top = y < (height - 1) ? getPositionAt(tmpV6, x, y + 1) : null;
    if (top != null && left != null) {
        out.add(tmpV7.set(top).sub(center).nor().crs(tmpV8.set(center).sub(left).nor()).nor());
        faces++;
    }
    if (left != null && bottom != null) {
        out.add(tmpV7.set(left).sub(center).nor().crs(tmpV8.set(center).sub(bottom).nor()).nor());
        faces++;
    }
    if (bottom != null && right != null) {
        out.add(tmpV7.set(bottom).sub(center).nor().crs(tmpV8.set(center).sub(right).nor()).nor());
        faces++;
    }
    if (right != null && top != null) {
        out.add(tmpV7.set(right).sub(center).nor().crs(tmpV8.set(center).sub(top).nor()).nor());
        faces++;
    }
    if (faces != 0)
        out.scl(1f / (float) faces);
    else
        out.set(magnitude).nor();
    return out;
}

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  ww.  jav  a  2s .  c  o  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.github.fauu.helix.graphics.HelixCamera.java

License:Open Source License

public HelixCamera(float fieldOfView, Vector3 targetPosition, float near, float far) {
    super(fieldOfView, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    this.targetPosition = new Vector3();
    this.near = near;
    this.far = far;
    this.translate(0, -13, 17);
    this.lookAt(0, 0, 0);

    targetPositionDelta = new Vector3();

    translateTargetPosition(targetPosition.add(DEFAULT_TARGET_DISPLACEMENT));
}

From source file:com.lyeeedar.Roguelike3D.Game.Level.Level.java

License:Open Source License

public float getDescription(Ray ray, float view, StringBuilder sB, boolean longDesc) {
    Vector3 pos = Pools.obtain(Vector3.class).set(ray.origin);
    Vector3 step = Pools.obtain(Vector3.class).set(ray.direction).mul(VIEW_STEP);

    float dist = 0;

    for (int i = 0; i < view; i += VIEW_STEP) {
        dist += VIEW_STEP;/*from   w w w .j  a v a  2  s . c  o m*/

        if (dist * dist > view)
            break;

        pos.add(step);

        Tile t = getTile(pos.x, pos.z);
        if (t == null) {
            dist = view;
            break;
        }

        if (pos.y < t.height) {
            sB.delete(0, sB.length());
            if (longDesc) {
                sB.append(longDescs.get(t.character));
            } else {
                sB.append(shortDescs.get(t.character));
            }
            break;
        } else if (pos.y > t.roof) {
            if (hasRoof) {
                sB.delete(0, sB.length());
                if (longDesc) {
                    sB.append(longDescs.get('R'));
                } else {
                    sB.append(shortDescs.get('R'));
                }
            } else {

            }
            break;
        }
    }

    Pools.free(pos);
    Pools.free(step);

    return dist * dist;
}

From source file:com.lyeeedar.Roguelike3D.Game.Level.Level.java

License:Open Source License

public boolean collideRayLevel(Ray ray, float view) {
    Vector3 pos = Pools.obtain(Vector3.class).set(ray.origin);
    Vector3 step = Pools.obtain(Vector3.class).set(ray.direction).mul(VIEW_STEP);

    float dist = 0;
    boolean collide = false;

    for (int i = 0; i < view; i += VIEW_STEP) {
        dist += VIEW_STEP;//from   w  w  w .  j  a  va  2 s  .c  o m

        if (dist * dist > view)
            break;

        pos.add(step);

        Tile t = getTile(pos.x, pos.z);
        if (t == null) {
            dist = view;
            break;
        }

        if (pos.y < t.height) {
            collide = true;
            break;
        } else if (hasRoof && pos.y > t.roof) {
            collide = true;
            break;
        }
    }

    Pools.free(pos);
    Pools.free(step);
    return collide;
}

From source file:com.mbrlabs.mundus.ui.modules.Outline.java

License:Apache License

private void setupDragAndDrop() {
    dragAndDrop = new DragAndDrop();

    // source/*www . j  a va 2s . co m*/
    dragAndDrop.addSource(new DragAndDrop.Source(tree) {
        @Override
        public DragAndDrop.Payload dragStart(InputEvent event, float x, float y, int pointer) {
            DragAndDrop.Payload payload = new DragAndDrop.Payload();
            Tree.Node node = tree.getNodeAt(y);
            if (node != null) {
                payload.setObject(node);
                return payload;
            }

            return null;
        }
    });

    // target
    dragAndDrop.addTarget(new DragAndDrop.Target(tree) {
        @Override
        public boolean drag(DragAndDrop.Source source, DragAndDrop.Payload payload, float x, float y,
                int pointer) {
            // Select node under mouse if not over the selection.
            Tree.Node overNode = tree.getNodeAt(y);
            if (overNode == null && tree.getSelection().isEmpty()) {
                return true;
            }
            if (overNode != null && !tree.getSelection().contains(overNode)) {
                tree.getSelection().set(overNode);
            }
            return true;
        }

        @Override
        public void drop(DragAndDrop.Source source, DragAndDrop.Payload payload, float x, float y,
                int pointer) {
            Tree.Node node = (Tree.Node) payload.getObject();

            if (node != null) {
                GameObject draggedGo = (GameObject) node.getObject();
                Tree.Node newParent = tree.getNodeAt(y);

                // check if a go is dragged in one of its' children or
                // itself
                if (newParent != null) {
                    GameObject parentGo = (GameObject) newParent.getObject();
                    if (parentGo.isChildOf(draggedGo)) {
                        return;
                    }
                }
                GameObject oldParent = draggedGo.getParent();

                // remove child from old parent
                draggedGo.remove();

                // add to new parent
                if (newParent == null) {
                    // recalculate position for root layer
                    Vector3 newPos;
                    Vector3 draggedPos = new Vector3();
                    draggedGo.getPosition(draggedPos);
                    // if moved from old parent
                    if (oldParent != null) {
                        // new position = oldParentPos + draggedPos
                        Vector3 parentPos = new Vector3();
                        oldParent.getPosition(parentPos);
                        newPos = parentPos.add(draggedPos);
                    } else {
                        // new local position = World position
                        newPos = draggedPos;
                    }
                    projectContext.currScene.sceneGraph.addGameObject(draggedGo);
                    draggedGo.setLocalPosition(newPos.x, newPos.y, newPos.z);
                } else {
                    GameObject parentGo = (GameObject) newParent.getObject();
                    // recalculate position
                    Vector3 parentPos = new Vector3();
                    Vector3 draggedPos = new Vector3();
                    // World coorinates
                    draggedGo.getPosition(draggedPos);
                    parentGo.getPosition(parentPos);

                    // if gameObject came from old parent
                    if (oldParent != null) {
                        // calculate oldParentPos + draggedPos
                        Vector3 oldParentPos = new Vector3();
                        oldParent.getPosition(oldParentPos);
                        draggedPos = oldParentPos.add(draggedPos);
                    }

                    // Local in releation to new parent
                    Vector3 newPos = draggedPos.sub(parentPos);
                    // add
                    parentGo.addChild(draggedGo);
                    draggedGo.setLocalPosition(newPos.x, newPos.y, newPos.z);
                }

                // update tree
                buildTree(sceneGraph);
            }
        }
    });
}

From source file:com.mygdx.game.debugdrawers.ArmatureDebugDrawer.java

License:Apache License

private void drawArmatureNodes(Node currentNode, Vector3 modelPos, Quaternion modelRot, Vector3 parentNodePos,
        Vector3 currentNodePos) {
    currentNode.globalTransform.getTranslation(currentNodePos);
    modelRot.transform(currentNodePos);//w w w  .  jav  a  2  s.  co  m
    currentNodePos.add(modelPos);
    drawVertex(currentNodePos, 0.02f, Color.GREEN);
    shapeRenderer.setColor(Color.YELLOW);
    if (currentNode.hasParent()) {
        shapeRenderer.line(parentNodePos, currentNodePos);
    }
    if (currentNode.hasChildren()) {
        float x = currentNodePos.x;
        float y = currentNodePos.y;
        float z = currentNodePos.z;
        for (Node child : currentNode.getChildren()) {
            drawArmatureNodes(child, modelPos, modelRot, currentNodePos, parentNodePos);
            currentNodePos.set(x, y, z);
        }
    }
}

From source file:interpolationtests.CatmullRomSpline.java

License:Apache License

/** Returns a path, between every two control points numPoints are generated and the control points themselves are added too.
 * The first and the last controlpoint are omitted. if there's less than 4 controlpoints an empty path is returned.
 * //from  w w w  .ja v a 2s.co m
 * @param numPoints number of points returned for a segment
 * @return the path */
public List<Vector3> getPath(int numPoints) {
    ArrayList<Vector3> points = new ArrayList<Vector3>();

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

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

    for (int i = 1; i <= controlPoints.size() - 3; i++) {
        points.add(controlPoints.get(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);

        for (int j = 0; j < numPoints; j++) {
            float h1 = 2 * t * t * t - 3 * t * t + 1; // calculate basis
            // function 1
            float h2 = -2 * t * t * t + 3 * t * t; // calculate basis
            // function 2
            float h3 = t * t * t - 2 * t * t + t; // calculate basis
            // function 3
            float h4 = t * t * t - t * 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));
            points.add(point);
            t += increment;
        }
    }

    if (controlPoints.size() >= 4)
        points.add(controlPoints.get(controlPoints.size() - 2));

    return points;
}