Package engine.geometry

Examples of engine.geometry.Vector


    @Override
    protected void onBeforeMove(Keyboard keyboard, Mouse mouse, Clock clock) {
        if (state.getHorizontalState() == HorizontalState.RUN) {
            if (state.getFacingDirection() == FacingDirection.LEFT) {
                dynamic.applyImpulse(new Vector(-RUN_IMPULSE, 0));
            } else {
                dynamic.applyImpulse(new Vector(RUN_IMPULSE, 0));
            }
            if (dynamic.getVelocity().getX() > MAX_RUN_SPEED) {
                dynamic.getVelocity().setX(MAX_RUN_SPEED);
            }
            if (dynamic.getVelocity().getX() < -MAX_RUN_SPEED) {
                dynamic.getVelocity().setX(-MAX_RUN_SPEED);
            }
        } else if (state.getHorizontalState() == HorizontalState.WALK) {
            if (state.getFacingDirection() == FacingDirection.LEFT) {
                dynamic.applyImpulse(new Vector(-WALK_IMPULSE, 0));
            } else {
                dynamic.applyImpulse(new Vector(WALK_IMPULSE, 0));
            }
            if (dynamic.getVelocity().getX() > MAX_WALK_SPEED) {
                dynamic.getVelocity().setX(MAX_WALK_SPEED);
            }
            if (dynamic.getVelocity().getX() < -MAX_WALK_SPEED) {
                dynamic.getVelocity().setX(-MAX_WALK_SPEED);
            }
        } else {
            double x = dynamic.getVelocity().getX();
            if (x - SKID_IMPULSE / dynamic.getMass() > 0) {
                dynamic.applyImpulse(new Vector(-SKID_IMPULSE, 0));
            } else if (x + SKID_IMPULSE / dynamic.getMass() < 0) {
                dynamic.applyImpulse(new Vector(SKID_IMPULSE, 0));
            } else {
                dynamic.getVelocity().setX(0);
            }
        }

        if (dynamic.onGround()) {
            if (state.getVerticalState() == VerticalState.JUMP) {
                dynamic.applyImpulse(new Vector(0, -JUMP_IMPULSE));
                if (dynamic.getVelocity().getY() > MAX_JUMP_SPEED) {
                    dynamic.getVelocity().setY(MAX_JUMP_SPEED);
                }
                if (dynamic.getVelocity().getY() < -MAX_FALL_SPEED) {
                    dynamic.getVelocity().setY(-MAX_FALL_SPEED);
View Full Code Here


    private List<Scene> activeScenes;

    @Override
    public void onAdd() {
        text = "";
        position = new Vector(2, 476);
        rectangle = new Rectangle(0, 464, 640, 16);
        textColour = new Colour(255, 255, 255);
        rectColour = new Colour(0, 0, 0, 128);
        font = getStage().getLibrary().findFont("Debug");
View Full Code Here

    private Font font;

    @Override
    public void onAdd() {
        textColour = new Colour(255, 255, 255);
        position = new Vector(2, 12);
        font = getStage().getLibrary().findFont("Debug");
        rectangle = new Rectangle(0, 0, 640, 16);
        rectColour = new Colour(0, 0, 0, 128);
    }
View Full Code Here

   
    public enum State {ATTRACT, REPEL, OFF}
   
    public MagRayHabit(DynamicHabit dynamic) {
        this.dynamic = dynamic;
        centre = new Vector();
        mousePos = new Vector();
        direction = new Vector();
       
        startPoint = new Point();
        endCircle = new Circle();
        bounds = new Rectangle();
    }
View Full Code Here

        double c = Math.sqrt(cSqr); // TODO: cache this for entire shape
        double b = distance;
        double a = Math.sqrt(aSqr);

        // find first intersect with axis anti-clockwise of normal
        Vector start = new Vector (-b*normal.getX() + a*normal.getY(), -b*normal.getY() - a*normal.getX());
        // find first intersect with axis clockwise of normal
        Vector end = new Vector (-b*normal.getX() - a*normal.getY(), -b*normal.getY() + a*normal.getX());
       
        // normalise
        start.divide(c);
        end.divide(c);
       
        return new Collision(start, end, distance < 0);
    }
View Full Code Here

    }

    @Override
    protected void onBeforeMove(Keyboard keyboard, Mouse mouse, Clock clock) {
        mousePos.set(mouse.getX(), mouse.getY());
        mousePos.add(new Vector(((LevelScene) getScene()).getViewX(), ((LevelScene) getScene()).getViewY()));
       
        // calculate MagRay direction
        dynamic.getPolygon().getMid(centre);
        direction.set(mousePos);
        direction.subtract(centre);
        direction.normalise();
       
        // calculate state
        if (mouse.isDown(Button.MB_LEFT)) {
            state = State.ATTRACT;
            colour = new Colour(0, 255, 0, 128);
        } else if (mouse.isDown(Button.MB_RIGHT)) {
            state = State.REPEL;
            colour = new Colour(255, 0, 0, 128);
        } else {
            if (keyboard.isDown(Key.VK_SPACE)) {
                if (power < 30) {
                    power += 0.5;
                }
            } else {
                power = 0;
            }
            state = State.OFF;
            colour = new Colour(0, 0, 255, 128);
            return; // don't do MagRay calculations if it is unactivated
        }
       
        // calculate bounding box
        direction.multiply(RADIUS);
        startPoint.set(centre);
        endCircle.set(startPoint.getPosition().add(direction), RADIUS*BEAM_WIDTH);
        bounds = endCircle.getBounds();
        bounds.add(startPoint);
        direction.divide(RADIUS); // re-normalise direction
       
        // find all Metal object in this triangle
        Set<MetalHabit> set = ((LevelScene) getScene()).getSpace().findObjects(bounds.toPolygon(), MetalHabit.class);
        double maxAccelDynamic = 0;
        for (MetalHabit other : set) {
            double time = MagrayPhysics.computeTime(other.getCircle(), centre, direction, BEAM_WIDTH);
            if (!(time < RADIUS && time > 0)) {
                continue;
            }
           
            Vector normal = MagrayPhysics.computeNormal(other.getCircle(), centre, direction, time);
           
            double strength = timeToStrength(time, power);
            double accelOther = strength / (1 + other.getMass()/dynamic.getMass());
            other.addVelocity(normal.multiply(accelOther));
           
            double accelDynamic = strength / (1 + dynamic.getMass()/other.getMass());
            // TODO: find a better way of combining forces
            if (Math.abs(accelDynamic) > Math.abs(maxAccelDynamic)) {
                maxAccelDynamic = accelDynamic;
            }
        }
       
        dynamic.addVelocity(new Vector(direction).normalise().multiply(maxAccelDynamic));
        power = 0; // reset power once released
    }
View Full Code Here

        return new Collision(start, end, distance < 0);
    }
   
    /* Collision with single shape */
    public static Collision shapeCollision (Polygon s, Polygon d, Vector velocity) {
        Vector normal = new Vector(); // used to temporarily store normals in loop
        ArrayList<Collision> collisions = new ArrayList<Collision>();
       
        // SAT (Separating Axis Theorem) axes for dynamic shape
        int dSize = d.getSize();
        for (int i=0; i<dSize; i++) {
            d.getNormal(i, normal);
            double distance = Physics.minProjection(s, normal) - Physics.maxProjection(d, normal);
            collisions.add(axisCollision (normal, distance, velocity));
        }
       
        // SAT axes for static shape
        int sSize = s.getSize();
        for (int i=0; i<sSize; i++) {
            s.getNormal(i, normal);
            normal.flip();
            double distance = Physics.minProjection(s, normal) - Physics.maxProjection(d, normal);
            collisions.add(axisCollision (normal, distance, velocity));
        }
       
        return Collision.merge(collisions, false); // collision if overlapping all axes
View Full Code Here

    @Override
    public void onAdd() {
        getStage().getSceneBelow(this).setActive(false);
        image = getStage().getLibrary().findImage(imageName);
        position = new Vector(0, 0);
    }
View Full Code Here

            }
        }
    }

    public Collision getCollision(Polygon relStatic, Polygon relDynamic, Vector relVelocity) {
        Vector backVelocity = new Vector(relVelocity).flip().normalise();

        List<Collision> frontFaces = new LinkedList<Collision>();
        List<Collision> backFaces = new LinkedList<Collision>();
        List<Collision> zeroFaces = new LinkedList<Collision>();

        Vector n = new Vector();
        final int size1 = relStatic.getSize();
        for (int i = 0; i < size1; i++) {
            n = relStatic.getNormal(i);

            double nDistance = relStatic.getMaxProjection(i) - Physics.minProjection(relDynamic, n);
            double nVelocity = relVelocity.dot(n);
            double nTime = nDistance / nVelocity;

            if (Rough.equal(nDistance, 0) && Rough.equal(nVelocity, 0)) {
                // Sliding along this face
                zeroFaces.add(new Collision(Double.NaN, new Vector(n), backVelocity));
            } else if (Double.compare(nVelocity, 0) < 0) { // -0.0 and below
                // This is a front face
                frontFaces.add(new Collision(nTime, new Vector(n), backVelocity));
            } else if (Double.compare(nVelocity, 0) >= 0) { // +0.0 and above
                // This is a back face
                backFaces.add(new Collision(nTime, new Vector(n), backVelocity));
            }
        }

        final int size2 = relDynamic.getSize();
        for (int i = 0; i < size2; i++) {
            n = relDynamic.getNormal(i);

            double nDistance = relDynamic.getMaxProjection(i) - Physics.minProjection(relStatic, n);
            n.flip();
            double nVelocity = relVelocity.dot(n);
            double nTime = nDistance / nVelocity;

            if (Rough.equal(nDistance, 0) && Rough.equal(nVelocity, 0)) {
                // Sliding along this face
                zeroFaces.add(new Collision(Double.NaN, new Vector(n), backVelocity));
            } else if (Double.compare(nVelocity, 0) < 0) { // -0.0 and below
                // This is a front face
                frontFaces.add(new Collision(nTime, new Vector(n), backVelocity));
            } else if (Double.compare(nVelocity, 0) >= 0) { // +0.0 and above
                // This is a back face
                backFaces.add(new Collision(nTime, new Vector(n), backVelocity));
            }
        }

        // Find the last entry normal
        double lastEntry = Double.NEGATIVE_INFINITY;
View Full Code Here

                    }
                }
            }

            // Default collision
            Vector vel = new Vector(velocity).normalise();
            Vector velNorm = new Vector(vel).normal();
            Vector velAnti = new Vector(vel).antiNormal();
            Collision result = new Collision(Double.POSITIVE_INFINITY, velNorm, velAnti);

            // Find earliest collision
            for (Collision c : collisions) {
                if (c.time < result.time) {
                    result = c;
                }
            }

            // Find all collisions which happened at that time
            for (Collision c : collisions) {
                if (Rough.equal(c.time, result.time)) {
                    result.intersection(c);
                }
            }

            // Check if the collision was in this step
            if (Rough.lessEqual(0, result.time) && Rough.lessEqual(result.time, timeToGo)) {
                // Move to collision point
                Vector move = new Vector(velocity).multiply(result.time);
                Transform.translate(position, move);

                // Calculate new velocity
                Vector normal = new Vector(result.getBestNormal(velocity));
                velocity.project(normal.antiNormal());

                // Time used up
                timeToGo -= result.time;
                if (Rough.lessEqual(timeToGo, 0)) {
                    // Done!
                    break;
                }
            } else {
                // No collision. Done!
                Vector velScaled = new Vector(velocity).multiply(timeToGo);
                Transform.translate(position, velScaled);
                break;
            }
        }
        // TODO: return correct normal
View Full Code Here

TOP

Related Classes of engine.geometry.Vector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.