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

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

Introduction

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

Prototype

public Vector2(float x, float y) 

Source Link

Document

Constructs a vector with the given components

Usage

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

License:Apache License

public static Vector2 vectorMul(Vector2 v1, float scalar) {
    return new Vector2(v1.x * scalar, v1.y * scalar);
}

From source file:com.kotcrab.vis.editor.module.scene.entitymanipulator.tool.PolygonTool.java

License:Apache License

private void makeDefaultPolygon() {
    Rectangle rect = proxy.getBoundingRectangle();

    ChangePolygonAction action = new ChangePolygonAction(entityManipulator, proxy);

    component.vertices.clear();/*from  w  w w  .  ja  v a 2s .  co  m*/
    component.vertices.add(new Vector2(rect.x, rect.y));
    component.vertices.add(new Vector2(rect.x + rect.width, rect.y));
    component.vertices.add(new Vector2(rect.x + rect.width, rect.y + rect.height));
    component.vertices.add(new Vector2(rect.x, rect.y + rect.height));

    updateComponentFaces();

    action.takeSnapshot();
    undoModule.add(action);
    entityManipulator.selectedEntitiesChanged();
}

From source file:com.kotcrab.vis.editor.module.scene.entitymanipulator.tool.PolygonTool.java

License:Apache License

@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
    camera.unproject(tmpVector.set(x, y, 0));

    float worldX = tmpVector.x;
    float worldY = tmpVector.y;

    camera.project(tmpVector);/*w ww  .j  a v  a  2s  .  c  om*/

    x = tmpVector.x;
    y = tmpVector.y;

    if (button == Buttons.LEFT && component != null) {
        if (lineOverStartVertex != null) {
            int vertexIndex = component.vertices.indexOf(lineOverStartVertex, true);
            ChangePolygonAction action = new ChangePolygonAction(entityManipulator, proxy);

            Vector2 newVertex = new Vector2(worldX, worldY);
            component.vertices.insert(vertexIndex, newVertex);
            overVertex = newVertex;

            updateComponentFaces();
            action.takeSnapshot();
            undoModule.add(action);
            entityManipulator.selectedEntitiesChanged();

            changePolygonAction = new ChangePolygonAction(entityManipulator, proxy);

            return true;
        }

        if (overVertex != null) {
            changePolygonAction = new ChangePolygonAction(entityManipulator, proxy);
        }

        for (Vector2 v : component.vertices) {
            if (isInsidePoint(v, x, y, POLYGON_RECT_SIZE)) {
                return true;
            }
        }
    }

    return super.touchDown(event, x, y, pointer, button);
}

From source file:com.kotcrab.vis.editor.ui.scene.entityproperties.EntityProperties.java

License:Apache License

public EntityProperties(SceneModuleContainer sceneMC, Tab parentSceneTab) {
    super(true);/*from  w w  w.  jav  a 2s.  c  o  m*/
    sceneMC.injectModules(this);

    this.sceneMC = sceneMC;
    this.parentTab = parentSceneTab;

    setBackground(VisUI.getSkin().getDrawable("window-bg"));
    setTouchable(Touchable.enabled);
    setVisible(false);

    sharedChangeListener = new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            if (actor instanceof VisCheckBox)
                throw new IllegalStateException(
                        "sharedChangeListener cannot be used for checkboxes, use sharedCheckBoxChangeListener instead");
            if (actor instanceof VisSelectBox)
                throw new IllegalStateException(
                        "sharedChangeListener cannot be used for selectBoxes, use sharedSelectBoxChangeListener instead");

            setValuesToEntities();
            parentTab.dirty();
        }
    };

    sharedChckAndSelectBoxChangeListener = new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            beginSnapshot();
            setValuesToEntities();
            parentTab.dirty();
            endSnapshot();
        }
    };

    sharedFocusListener = new FocusListener() {
        @Override
        public void keyboardFocusChanged(FocusEvent event, Actor actor, boolean focused) {
            if (focused) {
                beginSnapshot();
            } else {
                endSnapshot();
            }
        }
    };

    sharedInputListener = new InputListener() {
        @Override
        public boolean keyDown(InputEvent event, int keycode) {
            if (keycode == Keys.ENTER) {
                if (snapshotInProgress == false)
                    beginSnapshot();
                setValuesToEntities();
                parentTab.dirty();
                endSnapshot();
                return true;
            }

            return false;
        }
    };

    basicProperties = new BasicEntityPropertiesTable(this, colorPickerModule.getPicker());
    groupProperties = new GroupPropertiesTable(this);

    componentSelectDialog = new ComponentSelectDialog(sceneMC, this, clazz -> {
        try {
            ImmutableArray<EntityProxy> entities = getSelectedEntities();

            if (entities.size() == 0)
                return; //nothing is selected
            undoModule.execute(new ComponentAddAction(sceneMC, entities, clazz));
        } catch (ReflectiveOperationException e) {
            Log.exception(e);
            toastModule.show(new DetailsToast("Component creation failed!", e));
        }
    });

    addComponentButton = new VisTextButton("Add Component");
    addComponentButton.addListener(new VisChangeListener((event, actor) -> {
        boolean anyComponentAvailable = componentSelectDialog.build();
        if (anyComponentAvailable == false) {
            statusBarModule.setText("There isn't any available component");
            return;
        }
        getStage().addActor(componentSelectDialog);
        Vector2 pos = getStage().screenToStageCoordinates(
                new Vector2(Gdx.input.getX(), Gdx.input.getY() + componentSelectDialog.getHeight()));
        componentSelectDialog.setPosition(pos.x, pos.y);
        ActorUtils.keepWithinStage(getStage(), componentSelectDialog);
    }));

    reloadComponentTables();

    propertiesTable = new VisTable(true);

    VisScrollPane scrollPane = new VisScrollPane(propertiesTable);
    scrollPane.setScrollingDisabled(true, false);
    scrollPane.setFadeScrollBars(false);
    scrollPane.setFlickScroll(false);

    top();
    add(new VisLabel("Entity Properties")).row();
    add(scrollPane).fillX().expandX().padLeft(3).padRight(3);

    pack();
}

From source file:com.kotcrab.vis.runtime.system.physics.PhysicsBodyManager.java

License:Apache License

@Override
public void inserted(Entity entity) {
    PhysicsProperties physicsProperties = physicsPropCm.get(entity);
    VisPolygon polygon = polygonCm.get(entity);
    Transform transform = transformCm.get(entity);

    if (physicsProperties.adjustOrigin && originCm.has(entity))
        originCm.get(entity).setOrigin(0, 0);

    Vector2 worldPos = new Vector2(transform.getX(), transform.getY());

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(worldPos);//from   ww w  .  j ava2  s .c o  m

    Body body = world.createBody(bodyDef);
    body.setType(physicsProperties.bodyType);
    body.setUserData(entity);

    body.setGravityScale(physicsProperties.gravityScale);
    body.setLinearDamping(physicsProperties.linearDamping);
    body.setAngularDamping(physicsProperties.angularDamping);

    body.setBullet(physicsProperties.bullet);
    body.setFixedRotation(physicsProperties.fixedRotation);
    body.setSleepingAllowed(physicsProperties.sleepingAllowed);
    body.setActive(physicsProperties.active);

    for (Vector2[] vs : polygon.faces) {
        for (Vector2 v : vs) { //polygon component stores data in world cords, we need to convert it to local cords
            v.sub(worldPos);
        }

        PolygonShape shape = new PolygonShape();
        shape.set(vs);

        FixtureDef fd = new FixtureDef();
        fd.density = physicsProperties.density;
        fd.friction = physicsProperties.friction;
        fd.restitution = physicsProperties.restitution;
        fd.isSensor = physicsProperties.sensor;
        fd.shape = shape;
        fd.filter.maskBits = physicsProperties.maskBits;
        fd.filter.categoryBits = physicsProperties.categoryBits;

        body.createFixture(fd);
        shape.dispose();
    }

    entity.edit().add(new PhysicsBody(body)).add(new OriginalRotation(transform.getRotation()));
}

From source file:com.kotcrab.vis.runtime.system.physics.PhysicsSystem.java

License:Apache License

public PhysicsSystem(PhysicsSettings physicsSettings) {
    box2dWorld = new World(new Vector2(physicsSettings.gravityX, physicsSettings.gravityY),
            physicsSettings.allowSleep);
}

From source file:com.kotcrab.vis.ui.widget.Menu.java

License:Apache License

private void showMenu() {
    Vector2 pos = openButton.localToStageCoordinates(new Vector2(0, 0));
    setPosition(pos.x, pos.y - getHeight());
    openButton.getStage().addActor(this);
    menuBar.setCurrentMenu(this);
}

From source file:com.laex.cg2d.entityeditor.pages.CollisionFormPage.java

License:Open Source License

/**
 * Calculate vertices of shape./* w  w w. java  2  s . c  o  m*/
 * 
 * @return the list
 */
private List<Vector2> calculateVerticesOfShape() {
    switch (collisionShape) {
    case BOX:
    case CIRCLE:
        Rectangle r = shapeTypeBoundingBox();

        int widthVal = r.x + r.width;
        int heightVal = r.y + r.height;

        Vector2 v1 = new Vector2(r.x, r.y);
        Vector2 v2 = new Vector2(widthVal, r.y);
        Vector2 v3 = new Vector2(widthVal, heightVal);
        Vector2 v4 = new Vector2(r.x, heightVal);

        List<Vector2> vlist = new ArrayList<Vector2>();
        vlist.add(v1);
        vlist.add(v2);
        vlist.add(v3);
        vlist.add(v4);

        return vlist;
    case CUSTOM:
        break;
    case NONE:
        break;
    default:
        break;
    }

    return null;
}

From source file:com.laex.cg2d.model.adapter.EntityAdapter.java

License:Open Source License

/**
 * As entity.//  w w w  .j ava 2  s. c o m
 * 
 * @param cge
 *          the cge
 * @return the entity
 */
public static Entity asEntity(CGEntity cge) {
    Entity entityModel = new Entity();

    for (CGEntityAnimation cgEa : cge.getAnimationsList()) {
        EntityAnimation ea = new EntityAnimation();
        ea.setAnimationDuration(cgEa.getAnimationDuration());
        ea.setAnimationName(cgEa.getAnimationName());
        ea.setDefaultAnimation(cgEa.getDefaultAnimation());
        ea.setFixtureResourceFile(ResourceFileAdapter.asResourceFile(cgEa.getFixtureFile()));
        ea.setSpritesheetFile(ResourceFileAdapter.asResourceFile(cgEa.getSpritesheetFile()));
        ea.setShapeType(toEntityCollisionType(cgEa.getCollisionType()));

        for (CGVector2 cv : cgEa.getVerticesList()) {
            ea.getVertices().add(new Vector2(cv.getX(), cv.getY()));
        }

        ea.setSpritesheetMapperFile(ResourceFileAdapter.asResourceFile(cgEa.getSpritesheetJsonFile()));

        for (CGEntitySpritesheetItem cesi : cgEa.getSpritesheetItemsList()) {

            EntitySpritesheetItem esi = new EntitySpritesheetItem();

            esi.setExtractBounds(RectAdapter.gdxRect(cesi.getExtractBounds()));
            esi.setFrameIndex(cesi.getFrameIndex());
            ea.getSpritesheetItems().add(esi);
        }

        entityModel.addEntityAnimation(ea);
    }

    Image defaultFrame = EntitiesUtil.getDefaultFrame(entityModel, 1);

    entityModel.setDefaultFrame(defaultFrame);

    return entityModel;
}

From source file:com.laex.cg2d.model.adapter.Vector2Adapter.java

License:Open Source License

/**
 * As vector2./*  w ww . jav  a2 s. c om*/
 * 
 * @param v
 *          the v
 * @return the vector2
 */
public static Vector2 asVector2(CGVector2 v) {
    return new Vector2(v.getX(), v.getY());
}