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

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

Introduction

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

Prototype

@Override
    public Vector2 nor() 

Source Link

Usage

From source file:com.badlogic.gdx.tests.box2d.Prismatic.java

License:Apache License

@Override
protected void createWorld(World world) {
    Body ground;//from  w  w  w  .j a  v  a 2  s. com

    {
        BodyDef bd = new BodyDef();
        ground = world.createBody(bd);
        EdgeShape shape = new EdgeShape();
        shape.set(new Vector2(-40, 0), new Vector2(40, 0));
        ground.createFixture(shape, 0);
        shape.dispose();
    }

    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(2, 5);

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(-10, 10);
        bd.angle = 0.5f * (float) Math.PI;
        bd.allowSleep = false;

        Body body = world.createBody(bd);
        body.createFixture(shape, 5.0f);

        PrismaticJointDef pjd = new PrismaticJointDef();

        Vector2 axis = new Vector2(2, 1);
        axis.nor();
        pjd.initialize(ground, body, new Vector2(0, 0), axis);

        pjd.motorSpeed = 10.0f;
        pjd.maxMotorForce = 10000.0f;
        pjd.enableMotor = true;
        pjd.lowerTranslation = 0;
        pjd.upperTranslation = 20.0f;
        pjd.enableLimit = true;

        m_joint = (PrismaticJoint) world.createJoint(pjd);
    }
}

From source file:com.dongbat.game.util.MovementUtil.java

public static void newMovementMechanism(World world, Entity e) {
    UnitMovement unitMovement = EntityUtil.getComponent(world, e, UnitMovement.class);

    Physics physicsComponent = EntityUtil.getComponent(world, e, Physics.class);
    Body body = physicsComponent.getBody();
    float mass = body.getMass();
    if (unitMovement.getDirectionVelocity() == null) {
        //      body.setLinearVelocity(new Vector2(0, 0));
        return;//from ww  w  .jav a2  s  .c o m
    }

    Vector2 directionVel = unitMovement.getDirectionVelocity().cpy();

    //heading
    float angleRad = body.getLinearVelocity().angleRad();
    body.setTransform(body.getPosition(), angleRad);

    // TODO: calculate based on radius, use fixed mass
    // or make a steering-like behavior with real mass
    float desiredSpd = calculalteDesiredSpeed(world, e);
    Vector2 currentVector = body.getLinearVelocity();
    directionVel.nor().scl(desiredSpd).sub(currentVector);

    Vector2 impulse = directionVel.scl(mass);

    PhysicsUtil.applyImpulse(world, e, impulse);
}

From source file:com.dongbat.invasion.system.EnemyMovementSystem.java

@Override
protected void process(Entity entity) {
    Delay delay = delayMapper.get(entity);
    if (delay != null && delay.getDuration() > 0) {
        long timePassed = TimeUtil.getGameTimeSince(delay.getStartTime());
        if (timePassed <= delay.getDuration()) {
            return;
        }//from  w  w  w  .ja va2 s.c  o m
    }

    //    Vector2 prevVelocity = PhysicsUtil.getVelocity(entity).cpy();
    Vector2 position = PhysicsUtil.getPosition(entity);
    EnemyMovement movement = EntityUtil.getComponent(entity, EnemyMovement.class);
    PhysicsUtil.setVelocity(entity, new Vector2());
    Vector2 intent = movement.getIntent().cpy();
    Vector2 travel;
    MovementUtil.moveEnemyTo(entity, intent);
    // TODO check free enemy
    intent.y = position.y;
    // TODO free movement unit use moveTo
    Vector2 distance = intent.cpy().sub(position);
    travel = PhysicsUtil.getVelocity(entity).cpy().scl(world.delta);
    if (travel.dot(distance) > 0 && travel.len2() - distance.len2() > 0) {
        PhysicsUtil.setPosition(entity, intent);
        PhysicsUtil.setVelocity(entity, new Vector2());
    }

    if (TimeUtil.getGameTimeSince(movement.getStartDrag()) >= movement.getDragDuration()) {
        movement.setDraggedTo(null);
    }

    if (movement.getDraggedTo() == null) {
        return;
    }

    float max = movement.getDraggedTo().cpy().sub(movement.getDraggedFrom()).len2();
    float actual = position.cpy().sub(movement.getDraggedFrom()).len2();
    if (actual >= max) {
        return;
    }

    Vector2 drag = movement.getDraggedTo().cpy().sub(position);
    float dragLen2 = drag.len2();
    if (dragLen2 > movement.getDragForce() * movement.getDragForce()) {
        drag.nor().scl(movement.getDragForce());
    }
    PhysicsUtil.applyImpulseOnEnemy(entity, drag);

    // TODO fix this similar to above
    travel = PhysicsUtil.getVelocity(entity).cpy().scl(world.delta);
    if (travel.sub(drag).len2() <= Constants.GAME.EPSILON_SQUARED) {
        PhysicsUtil.setPosition(entity, movement.getDraggedTo());
        PhysicsUtil.applyImpulseOnEnemy(entity, drag.scl(-1));
    }
}

From source file:com.github.unluckyninja.defenseofhuman.model.entity.Hook.java

License:Open Source License

public Hook(GameWorld gameWorld, Player.HookShooter shooter, Vector2 direction, float launchSpeed) {
    this.gameWorld = gameWorld;
    this.shooter = shooter;

    Vector2 dir = direction.cpy();

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(shooter.getPlayer().getLaunchPosition());
    bodyDef.angle = (float) Math.toRadians(dir.angle());
    bodyDef.bullet = true;/*w  w w.j a  v a  2s  .c  om*/
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.linearVelocity.set(dir.nor().scl(launchSpeed));

    body = shooter.getPlayer().getWorld().createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(0.15f, 0.1f, new Vector2(0.15f, 0), 0);

    body.createFixture(shape, 0.01f).setSensor(true);

    shape.dispose();

    body.setUserData(this);

    // maybe we can move all the definitions to fields?
}

From source file:com.github.unluckyninja.defenseofhuman.model.entity.Rope.java

License:Open Source License

public Rope(GameWorld gameWorld, Body body, Vector2 direction) {
    this.gameWorld = gameWorld;
    world = body.getWorld();//from  w ww  .ja  v  a  2s .  c  om
    bodyA = body;

    // ???
    Vector2 oriPosition = new Vector2(body.getPosition());
    if (direction.equals(Vector2.Zero)) {
        direction = Vector2.X;
    }
    Vector2 dir = new Vector2(direction);
    dir.nor();
    Vector2 distance = dir.scl(width / 2 * 1.25f); // ,?1/4,?

    // ?body
    bodyDef.angle = (float) Math.toRadians(dir.angle());
    bodyDef.position.set(distance.add(oriPosition));
    bodyDef.linearDamping = 0.3f;
    bodyDef.type = BodyDef.BodyType.DynamicBody;

    Body piece = body.getWorld().createBody(bodyDef);
    piece.setGravityScale(0.1f);

    // ?
    // ??,????
    fixtureDef.isSensor = true;
    fixtureDef.density = 0.001f;

    polygonShape.setAsBox(width / 2, height / 2);

    fixtureDef.shape = polygonShape;

    piece.createFixture(fixtureDef);

    // ?
    RevoluteJointDef rjDef = new RevoluteJointDef();
    rjDef.bodyA = body;
    rjDef.bodyB = piece;

    rjDef.localAnchorA.x = 0;
    rjDef.localAnchorA.y = 0;
    rjDef.localAnchorB.x = -width / 2 * 1.25f;
    rjDef.localAnchorB.y = 0;

    joints.add(body.getWorld().createJoint(rjDef));
    pieces.add(piece);

    piece.setUserData(this);

}

From source file:com.hajnar.GravityShip.GameWorld.java

License:Apache License

public void updateBlackHoles(float delta) {
    int len = blackHoles.size();
    for (int i = 0; i < len; i++) {
        blackHoles.get(i).update(delta);
        Vector2 playerPosition = player.getBody().getPosition();
        Vector2 holePosition = blackHoles.get(i).getBody().getPosition();
        Vector2 gravityVector = (holePosition.sub(playerPosition));
        float gravityForce = blackHoles.get(i).getStrength() / gravityVector.len2();
        gravityVector.nor();
        gravityVector.mul(gravityForce);
        player.getBody().applyForceToCenter(gravityVector);
    }/*  ww w. j  av  a 2  s  .  com*/
}

From source file:com.jmolina.orb.var.Utils.java

License:Open Source License

/**
 * Calcula la normal a un vector/* w  w w.ja  v  a2s.  c  om*/
 *
 * @param vector Vector
 * @return Vector normal
 */
public static Vector2 normal(Vector2 vector) {
    Vector2 normal = new Vector2(vector.y, -vector.x);
    normal.nor();

    return normal;
}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java

License:Apache License

private static boolean SplitPolygonEdge(Array<Vector2> polygon, EdgeAlignment edgeAlign,
        Vector2 coordInsideThePolygon, Reference<Integer> vertex1IndexRef, Reference<Integer> vertex2IndexRef)
        throws Exception {
    Array<CrossingEdgeInfo> edges;
    Vector2 slope = new Vector2();
    int nearestEdgeVertex1Index = 0;
    int nearestEdgeVertex2Index = 0;
    boolean edgeFound = false;
    float shortestDistance = Float.MAX_VALUE;
    boolean edgeCoordFound = false;
    Vector2 foundEdgeCoord = new Vector2();
    vertex1IndexRef.v = 0;//from   w  w w. j  a  v  a  2 s  .  com
    vertex2IndexRef.v = 0;
    switch (edgeAlign) {
    case Vertical:
        edges = GetCrossingEdges(polygon, EdgeAlignment.Vertical, (int) coordInsideThePolygon.y);
        foundEdgeCoord.y = coordInsideThePolygon.y;
        if (edges != null && edges.size > 1 && edges.size % 2 == 0) {
            float distance;
            for (int i = 0; i < edges.size; i++) {
                if (edges.get(i).CrossingPoint.x < coordInsideThePolygon.x) {
                    distance = coordInsideThePolygon.x - edges.get(i).CrossingPoint.x;
                    if (distance < shortestDistance) {
                        shortestDistance = distance;
                        foundEdgeCoord.x = edges.get(i).CrossingPoint.x;
                        edgeCoordFound = true;
                    }
                }
            }
            if (edgeCoordFound) {
                shortestDistance = Float.MAX_VALUE;
                int edgeVertex2Index = polygon.size - 1;
                int edgeVertex1Index;
                for (edgeVertex1Index = 0; edgeVertex1Index < polygon.size; edgeVertex1Index++) {
                    Vector2 tempVector1 = polygon.get(edgeVertex1Index).cpy();
                    Vector2 tempVector2 = polygon.get(edgeVertex2Index).cpy();
                    distance = LineTools.DistanceBetweenPointAndLineSegment(foundEdgeCoord, tempVector1,
                            tempVector2);
                    if (distance < shortestDistance) {
                        shortestDistance = distance;
                        nearestEdgeVertex1Index = edgeVertex1Index;
                        nearestEdgeVertex2Index = edgeVertex2Index;
                        edgeFound = true;
                    }
                    edgeVertex2Index = edgeVertex1Index;
                }
                if (edgeFound) {
                    slope.set(vectorSub(polygon.get(nearestEdgeVertex2Index),
                            polygon.get(nearestEdgeVertex1Index)));
                    slope.nor();
                    Vector2 tempVector = polygon.get(nearestEdgeVertex1Index).cpy();
                    distance = LineTools.DistanceBetweenPointAndPoint(tempVector, foundEdgeCoord);
                    vertex1IndexRef.v = nearestEdgeVertex1Index;
                    vertex2IndexRef.v = nearestEdgeVertex1Index + 1;
                    // distance * slope + polygon[vertex1Index]
                    polygon.insert(nearestEdgeVertex1Index,
                            vectorAdd(vectorMul(slope, distance), polygon.get(vertex1IndexRef.v)));
                    polygon.insert(nearestEdgeVertex1Index,
                            vectorAdd(vectorMul(slope, distance), polygon.get(vertex2IndexRef.v)));
                    return true;
                }
            }
        }
        break;
    case Horizontal:
        throw new Exception("EdgeAlignment.Horizontal isn't implemented yet. Sorry.");
    }
    return false;
}

From source file:com.tehforce.sofa.logic.Character.java

License:Open Source License

/**
 * // w  w w.  j  a  va  2 s. com
 * @param c Target character for operation.
 * @return The direction vector to target from this Character instance.
 */
public Vector2 direction(Character c) {
    Vector2 direction = new Vector2(0, 0);

    if (c.getPosition().y > getPosition().y)
        direction.add(0, 1);
    if (c.getPosition().y < getPosition().y)
        direction.add(0, -1);
    if (c.getPosition().x > getPosition().x)
        direction.add(1, 0);
    if (c.getPosition().x < getPosition().x)
        direction.add(-1, 0);

    return direction.nor();
}

From source file:com.tnf.ptm.entities.planet.PlanetManager.java

License:Apache License

private void applyGrav(PtmGame game, PtmSystem nearestSys) {
    float npGh = myNearestPlanet.getGroundHeight();
    float npFh = myNearestPlanet.getFullHeight();
    float npMinH = myNearestPlanet.getMinGroundHeight();
    Vector2 npPos = myNearestPlanet.getPos();
    Vector2 sysPos = nearestSys.getPos();
    float npGravConst = myNearestPlanet.getGravConst();

    List<PtmObject> objs = game.getObjMan().getObjs();
    for (PtmObject obj : objs) {
        if (!obj.receivesGravity()) {
            continue;
        }//from w  ww .  j a  v a  2s  .c om

        Vector2 objPos = obj.getPosition();
        float minDist;
        Vector2 srcPos;
        float gravConst;
        boolean onPlanet;
        float toNp = npPos.dst(objPos);
        float toSys = sysPos.dst(objPos);
        if (toNp < npFh) {
            if (recoverObj(obj, toNp, npMinH)) {
                continue;
            }
            minDist = npGh;
            srcPos = npPos;
            gravConst = npGravConst;
            onPlanet = true;
        } else if (toSys < Const.SUN_RADIUS) {
            minDist = SunSingleton.SUN_HOT_RAD;
            srcPos = sysPos;
            gravConst = SunSingleton.GRAV_CONST;
            onPlanet = false;
        } else {
            continue;
        }

        Vector2 grav = PtmMath.getVec(srcPos);
        grav.sub(objPos);
        float len = grav.len();
        grav.nor();
        if (len < minDist) {
            len = minDist;
        }
        float g = gravConst / len / len;
        grav.scl(g);
        obj.receiveForce(grav, game, true);
        PtmMath.free(grav);
        if (!onPlanet) {
            mySunSingleton.doDmg(game, obj, toSys);
        }
    }

}