Search in sources :

Example 21 with Vector3

use of com.badlogic.gdx.math.Vector3 in project libgdx by libgdx.

the class BoundingSphereDirectionalAnalyzer method analyze.

@Override
public Camera analyze(DirectionalLight light, Camera out, Camera mainCamera) {
    bb.inf();
    // Create bounding box encompassing main camera frustum
    for (int i = 0; i < mainCamera.frustum.planePoints.length; i++) {
        bb.ext(mainCamera.frustum.planePoints[i]);
    }
    // Radius
    float radius = bb.getDimensions(tmpV).len() * 0.5f;
    // Center
    bb.getCenter(tmpV);
    // Move back from 1.5*radius
    tmpV2.set(light.direction);
    tmpV2.scl(radius * 1.5f);
    // Position out camera
    out.direction.set(light.direction);
    out.position.set(tmpV.sub(tmpV2));
    // Compute near and far
    out.near = 0.5f * radius;
    out.far = 2.5f * radius;
    // Compute up vector
    Vector3 d = light.direction;
    if (d.z < d.x + d.y)
        out.up.set(-light.direction.y, light.direction.x, light.direction.z);
    else
        out.up.set(light.direction.x, -light.direction.z, light.direction.y);
    // Compute viewport (orthographic camera)
    out.viewportWidth = radius;
    out.viewportHeight = radius;
    return out;
}
Also used : Vector3(com.badlogic.gdx.math.Vector3)

Example 22 with Vector3

use of com.badlogic.gdx.math.Vector3 in project libgdx-inGameConsole by StrongJoshua.

the class Box2DTest method render.

@Override
public void render() {
    if (Gdx.input.isTouched()) {
        float x = Gdx.input.getX();
        float y = Gdx.input.getY();
        if (!console.hitsConsole(x, y)) {
            Vector3 worldVector = c.unproject(new Vector3(x, y, 0));
            createExplosion(worldVector.x, worldVector.y, 2000);
            console.log(String.format("Created touch explosion at %.2f, %.2f!", worldVector.x, worldVector.y), LogLevel.SUCCESS);
        }
    }
    if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE))
        Gdx.app.exit();
    world.step(1 / 60f, 6, 2);
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    for (int i = 0; i < bodies.length; i++) {
        Vector2 pos = bodies[i].getPosition();
        sprites[i].setPosition(pos.x - sprites[i].getWidth() / 2, pos.y - sprites[i].getHeight() / 2);
        sprites[i].setRotation(MathUtils.radiansToDegrees * bodies[i].getAngle());
        sprites[i].draw(batch);
    }
    batch.end();
    debugRenderer.render(world, c.combined);
    console.draw();
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) Vector3(com.badlogic.gdx.math.Vector3)

Example 23 with Vector3

use of com.badlogic.gdx.math.Vector3 in project libgdx-inGameConsole by StrongJoshua.

the class Box2DTest method createExplosion.

/**
	 * Creates an explosion that applies forces to the bodies relative to their
	 * position and the given x and y values.
	 * 
	 * @param maxForce
	 *            The maximum force to be applied to the bodies (diminishes as
	 *            distance from touch increases).
	 */
private void createExplosion(float x, float y, float maxForce) {
    float force;
    Vector2 touch = new Vector2(x, y);
    for (int i = 0; i < bodies.length; i++) {
        Body b = bodies[i];
        Vector2 v = b.getPosition();
        float dist = v.dst2(touch);
        if (dist == 0)
            force = maxForce;
        else
            force = MathUtils.clamp(maxForce / dist, 0, maxForce);
        float angle = v.cpy().sub(touch).angle();
        float xForce = force * MathUtils.cosDeg(angle);
        float yForce = force * MathUtils.sinDeg(angle);
        Vector3 touch3, v3, boundMin, boundMax, intersection;
        touch3 = new Vector3(touch.x, touch.y, 0);
        v3 = new Vector3(v.x, v.y, 0);
        boundMin = new Vector3(v.x - 1, v.y - 1, 0);
        boundMax = new Vector3(v.x + 1, v.y + 1, 0);
        intersection = Vector3.Zero;
        Intersector.intersectRayBounds(new Ray(touch3, v3), new BoundingBox(boundMin, boundMax), intersection);
        b.applyForce(new Vector2(xForce, yForce), new Vector2(intersection.x, intersection.y), true);
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) BoundingBox(com.badlogic.gdx.math.collision.BoundingBox) Vector3(com.badlogic.gdx.math.Vector3) Ray(com.badlogic.gdx.math.collision.Ray) Body(com.badlogic.gdx.physics.box2d.Body)

Example 24 with Vector3

use of com.badlogic.gdx.math.Vector3 in project AmazingMaze by TheVirtualMachine.

the class MazeScreen method touchDown.

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    Vector3 worldClickPos = viewport.getCamera().unproject(new Vector3(screenX, screenY, 0));
    int x = (int) worldClickPos.x;
    int y = (int) worldClickPos.y;
    TiledMapTileLayer objects = (TiledMapTileLayer) map.getLayers().get(MapFactory.OBJECT_LAYER);
    Cell gate;
    int newID;
    for (Point point : gateLocations) {
        if (point.x == x && point.y == y) {
            gate = objects.getCell(x, y);
            newID = TileIDs.computeID(TileIDs.stripElectricState(gate.getTile().getId()));
            if (button == Buttons.LEFT) {
                newID = TileIDs.computeID(newID, TileIDs.ON);
                updateWires(x, y, TileIDs.ON);
            } else if (button == Buttons.RIGHT) {
                newID = TileIDs.computeID(newID, TileIDs.OFF);
                updateWires(x, y, TileIDs.OFF);
            } else if (button == Buttons.MIDDLE) {
                newID = TileIDs.computeID(newID, TileIDs.UNKNOWN);
                updateWires(x, y, TileIDs.UNKNOWN);
            } else {
                return true;
            }
            gate.setTile(game.assets.tiles.getTile(newID));
            break;
        }
    }
    return true;
}
Also used : TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) Vector3(com.badlogic.gdx.math.Vector3) Point(java.awt.Point) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell) Point(java.awt.Point)

Example 25 with Vector3

use of com.badlogic.gdx.math.Vector3 in project nhglib by VoidZombie.

the class NhgG3dModelLoader method parseAnimations.

private void parseAnimations(ModelData model, JsonValue json) {
    JsonValue animations = json.get("animations");
    if (animations == null)
        return;
    model.animations.ensureCapacity(animations.size);
    for (JsonValue anim = animations.child; anim != null; anim = anim.next) {
        JsonValue nodes = anim.get("bones");
        if (nodes == null)
            continue;
        ModelAnimation animation = new ModelAnimation();
        model.animations.add(animation);
        animation.nodeAnimations.ensureCapacity(nodes.size);
        animation.id = anim.getString("id");
        for (JsonValue node = nodes.child; node != null; node = node.next) {
            ModelNodeAnimation nodeAnim = new ModelNodeAnimation();
            animation.nodeAnimations.add(nodeAnim);
            nodeAnim.nodeId = node.getString("boneId");
            // For backwards compatibility (version 0.1):
            JsonValue keyframes = node.get("keyframes");
            if (keyframes != null && keyframes.isArray()) {
                for (JsonValue keyframe = keyframes.child; keyframe != null; keyframe = keyframe.next) {
                    final float keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                    JsonValue translation = keyframe.get("translation");
                    if (translation != null && translation.size == 3) {
                        if (nodeAnim.translation == null)
                            nodeAnim.translation = new Array<ModelNodeKeyframe<Vector3>>();
                        ModelNodeKeyframe<Vector3> tkf = new ModelNodeKeyframe<Vector3>();
                        tkf.keytime = keytime;
                        tkf.value = new Vector3(translation.getFloat(0), translation.getFloat(1), translation.getFloat(2));
                        nodeAnim.translation.add(tkf);
                    }
                    JsonValue rotation = keyframe.get("rotation");
                    if (rotation != null && rotation.size == 4) {
                        if (nodeAnim.rotation == null)
                            nodeAnim.rotation = new Array<ModelNodeKeyframe<Quaternion>>();
                        ModelNodeKeyframe<Quaternion> rkf = new ModelNodeKeyframe<Quaternion>();
                        rkf.keytime = keytime;
                        rkf.value = new Quaternion(rotation.getFloat(0), rotation.getFloat(1), rotation.getFloat(2), rotation.getFloat(3));
                        nodeAnim.rotation.add(rkf);
                    }
                    JsonValue scale = keyframe.get("scale");
                    if (scale != null && scale.size == 3) {
                        if (nodeAnim.scaling == null)
                            nodeAnim.scaling = new Array<ModelNodeKeyframe<Vector3>>();
                        ModelNodeKeyframe<Vector3> skf = new ModelNodeKeyframe();
                        skf.keytime = keytime;
                        skf.value = new Vector3(scale.getFloat(0), scale.getFloat(1), scale.getFloat(2));
                        nodeAnim.scaling.add(skf);
                    }
                }
            } else {
                // Version 0.2:
                JsonValue translationKF = node.get("translation");
                if (translationKF != null && translationKF.isArray()) {
                    nodeAnim.translation = new Array<ModelNodeKeyframe<Vector3>>();
                    nodeAnim.translation.ensureCapacity(translationKF.size);
                    for (JsonValue keyframe = translationKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Vector3> kf = new ModelNodeKeyframe<Vector3>();
                        nodeAnim.translation.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue translation = keyframe.get("value");
                        if (translation != null && translation.size >= 3)
                            kf.value = new Vector3(translation.getFloat(0), translation.getFloat(1), translation.getFloat(2));
                    }
                }
                JsonValue rotationKF = node.get("rotation");
                if (rotationKF != null && rotationKF.isArray()) {
                    nodeAnim.rotation = new Array<ModelNodeKeyframe<Quaternion>>();
                    nodeAnim.rotation.ensureCapacity(rotationKF.size);
                    for (JsonValue keyframe = rotationKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Quaternion> kf = new ModelNodeKeyframe<Quaternion>();
                        nodeAnim.rotation.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue rotation = keyframe.get("value");
                        if (rotation != null && rotation.size >= 4)
                            kf.value = new Quaternion(rotation.getFloat(0), rotation.getFloat(1), rotation.getFloat(2), rotation.getFloat(3));
                    }
                }
                JsonValue scalingKF = node.get("scaling");
                if (scalingKF != null && scalingKF.isArray()) {
                    nodeAnim.scaling = new Array<ModelNodeKeyframe<Vector3>>();
                    nodeAnim.scaling.ensureCapacity(scalingKF.size);
                    for (JsonValue keyframe = scalingKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Vector3> kf = new ModelNodeKeyframe<Vector3>();
                        nodeAnim.scaling.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue scaling = keyframe.get("value");
                        if (scaling != null && scaling.size >= 3)
                            kf.value = new Vector3(scaling.getFloat(0), scaling.getFloat(1), scaling.getFloat(2));
                    }
                }
            }
        }
    }
}
Also used : Quaternion(com.badlogic.gdx.math.Quaternion) Vector3(com.badlogic.gdx.math.Vector3)

Aggregations

Vector3 (com.badlogic.gdx.math.Vector3)64 Matrix4 (com.badlogic.gdx.math.Matrix4)10 Quaternion (com.badlogic.gdx.math.Quaternion)9 Vector2 (com.badlogic.gdx.math.Vector2)6 Node (com.badlogic.gdx.graphics.g3d.model.Node)5 Texture (com.badlogic.gdx.graphics.Texture)4 Model (com.badlogic.gdx.graphics.g3d.Model)4 NodeKeyframe (com.badlogic.gdx.graphics.g3d.model.NodeKeyframe)4 BoundingBox (com.badlogic.gdx.math.collision.BoundingBox)4 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)3 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)3 Material (com.badlogic.gdx.graphics.g3d.Material)3 ModelNode (com.badlogic.gdx.graphics.g3d.model.data.ModelNode)3 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)3 Mesh (com.badlogic.gdx.graphics.Mesh)2 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)2 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)2 Animation (com.badlogic.gdx.graphics.g3d.model.Animation)2 MeshPart (com.badlogic.gdx.graphics.g3d.model.MeshPart)2 NodeAnimation (com.badlogic.gdx.graphics.g3d.model.NodeAnimation)2