Example usage for com.badlogic.gdx.physics.box2d Body applyLinearImpulse

List of usage examples for com.badlogic.gdx.physics.box2d Body applyLinearImpulse

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d Body applyLinearImpulse.

Prototype

public void applyLinearImpulse(float impulseX, float impulseY, float pointX, float pointY, boolean wake) 

Source Link

Document

Apply an impulse at a point.

Usage

From source file:com.uwsoft.editor.gdx.ui.components.ItemPhysicsEditor.java

License:Apache License

private void setListeners() {
    addListener(new InputListener() {

        public float touchDownX;
        public float touchDownY;

        @Override//from   w ww. j av a  2s.  c  om
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            if (currentMode == EditMode.Test) {
                touchDownX = x;
                touchDownY = y;
                return true;
            }
            stage.setKeyboardFocus(ItemPhysicsEditor.this);

            localToStageCoordinates(tmpvector.set(x, y));

            if (vertices.length == 0) {
                verticesList.add(tmpvector.cpy());
                System.out.println("ADD POINT " + tmpvector.toString());
            }
            selectedPoint.unSet();
            if (currentMode == EditMode.Edit && vertices.length > 1) {
                for (int i = 0; i < vertices.length; i++) {
                    //shapeRenderer.rect(vertices[i-1]-POINT_WIDTH/2, vertices[i]-POINT_WIDTH/2, POINT_WIDTH, POINT_WIDTH);
                    if (tmpvector.x >= (vertices[i].x - POINT_WIDTH / 2)
                            && tmpvector.x <= (vertices[i].x + POINT_WIDTH / 2)
                            && tmpvector.y >= (vertices[i].y - POINT_WIDTH / 2)
                            && tmpvector.y <= (vertices[i].y + POINT_WIDTH / 2)) {
                        selectedPoint.set(vertices[i], i);
                        //System.out.println("waaaaaaaaaaaaaaaaaaaaaaaaa");
                        break;
                    }
                }

                if (currentMode == EditMode.Edit && lineIndex >= 0 && !selectedPoint.isSet) {
                    //                  if(lineIndex == vertices.length-1){
                    //                     verticesList.add(tmpvector.cpy());
                    //                  }else{
                    verticesList.add(lineIndex, tmpvector.cpy());
                    selectedPoint.set(vertices[lineIndex], lineIndex);
                    vertices = verticesList.toArray(new Vector2[0]);
                    //}
                    finish();
                }
            }

            //                else if (currentMode==EditMode.Test){

            //                }
            isDragging = false;
            return true;
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            if (currentMode == EditMode.Test) {
                BodyDef bodyDef = new BodyDef();
                Vector2 vec = new Vector2(touchDownX, touchDownY);
                ItemPhysicsEditor.this.localToStageCoordinates(vec);
                vec.scl(PhysicsBodyLoader.SCALE);
                bodyDef.position.set(vec);
                bodyDef.type = BodyDef.BodyType.DynamicBody;
                Body body = physicsEditorWorld.createBody(bodyDef);
                FixtureDef fixtureDef = new FixtureDef();
                fixtureDef.shape = new CircleShape();
                fixtureDef.shape.setRadius(10 * PhysicsBodyLoader.SCALE);
                body.createFixture(fixtureDef);
                body.applyLinearImpulse((x - touchDownX) * PhysicsBodyLoader.SCALE * 5,
                        (y - touchDownY) * PhysicsBodyLoader.SCALE * 5, 0, 0, false);
                return;
            }

            System.out.println("waaat up");
            isDragging = true;
            localToStageCoordinates(tmpvector.set(x, y));

            if (currentMode == EditMode.Edit) {
                finish();
                //selectedPoint.unSet();
                nearestPoint.unSet();
                return;
            }
            if (nearestPoint.isSet && nearestPoint.index == 0 && currentMode == EditMode.Create) {
                finish();
                selectedPoint.unSet();
                nearestPoint.unSet();
                return;
            }

            if (vertices.length > 0 && !checkForIntersection(vertices[vertices.length - 1], tmpvector)
                    && currentMode == EditMode.Create) {
                if (!verticesList.contains(tmpvector)) {
                    verticesList.add(tmpvector.cpy());
                    System.out.println("ADD POINT " + tmpvector.toString());
                }

            }
            nextPoint.set(tmpvector);
            vertices = verticesList.toArray(new Vector2[0]);
        }

        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {
            if (selectedPoint.isSet) {
                localToStageCoordinates(tmpvector.set(x, y));
                vertices[selectedPoint.index].set(tmpvector);
                verticesList.get(selectedPoint.index).set(tmpvector);
                finish();
            }
            super.touchDragged(event, x, y, pointer);
        }

        @Override
        public boolean mouseMoved(InputEvent event, float x, float y) {
            localToStageCoordinates(tmpvector.set(x, y));
            if (vertices.length > 0 && isDragging && currentMode == EditMode.Create) {
                nextPoint.set(tmpvector);
                lastLineColor = goodColor;
                if (checkForIntersection(vertices[vertices.length - 1], nextPoint)) {
                    lastLineColor = badColor;
                }
            }
            nearestPoint.unSet();
            for (int i = 0; i < vertices.length; i++) {
                if (vertices.length > 1 && tmpvector.x >= (vertices[i].x - POINT_WIDTH / 2)
                        && tmpvector.x <= (vertices[i].x + POINT_WIDTH / 2)
                        && tmpvector.y >= (vertices[i].y - POINT_WIDTH / 2)
                        && tmpvector.y <= (vertices[i].y + POINT_WIDTH / 2)) {
                    nearestPoint.set(vertices[i], i);
                    //System.out.println("PTUK SET " +i);
                    //System.out.println(tmpvector.x+">="+(vertices[i].x-POINT_WIDTH/2)+" && " +tmpvector.x+"<="+(vertices[i].x+POINT_WIDTH/2)+" && "+tmpvector.y+">="+(vertices[i].y-POINT_WIDTH/2) +" && "+ tmpvector.y+"<="+(vertices[i].y+POINT_WIDTH/2));
                    break;
                }
            }

            if (currentMode == EditMode.Edit && !nearestPoint.isSet) {
                lineIndex = -1;
                for (int i = 1; i < vertices.length; i++) {
                    if (Intersector.intersectSegmentCircle(vertices[i - 1], vertices[i], tmpvector,
                            CIRCLE_RADIUS)) {
                        lineIndex = i;
                        break;
                    }

                }
                if (vertices.length > 0 && Intersector.intersectSegmentCircle(vertices[vertices.length - 1],
                        vertices[0], tmpvector, CIRCLE_RADIUS)) {
                    lineIndex = 0;
                }
            }

            return super.mouseMoved(event, x, y);
        }

        @Override
        public boolean scrolled(InputEvent event, float x, float y, int amount) {
            if (currentMode == EditMode.Test) {
                return false;
            }
            float oldzoomfactor = zoomFactor;
            zoomFactor += amount / 10f;
            if (zoomFactor <= 0.2f) {
                zoomFactor = 0.2f;
            }

            currentActor.setScale(zoomFactor);
            currentActor.setX((getWidth() - currentActor.getWidth() * zoomFactor) / 2);
            currentActor.setY((getHeight() - currentActor.getHeight() * zoomFactor) / 2);

            float mul = zoomFactor / oldzoomfactor;
            Vector2 diff = new Vector2(getWidth() / 2, getHeight() / 2);
            localToStageCoordinates(diff);

            for (int i = 0; i < verticesList.size(); i++) {
                verticesList.get(i).sub(diff);
                verticesList.get(i).scl(mul);
                verticesList.get(i).add(diff);
            }
            if (currentMode == EditMode.Edit) {
                finish();
            }
            return super.scrolled(event, x, y, amount);
        }

        @Override
        public boolean keyDown(InputEvent event, int keycode) {
            System.out.println("DELETE DELE TE DELETE");
            if (keycode == 67 && currentMode == EditMode.Edit && selectedPoint.isSet) {

                verticesList.remove(selectedPoint.index);
                selectedPoint.unSet();
                finish();
            }
            return super.keyDown(event, keycode);
        }

        private boolean checkForIntersection(Vector2 v1, Vector2 v2) {
            if (vertices.length < 3) {
                return false;
            }

            Vector2 intersect = new Vector2();
            for (int i = 1; i < vertices.length - 1; i++) {
                if (Intersector.intersectSegments(v1, v2, vertices[i - 1], vertices[i], intersect)) {
                    return true;
                }
            }
            return false;
        }

    });
}

From source file:us.notsoserio.ninja.physics.EntityPhysics.java

License:Apache License

public static void MoveCharacter(Body body) {

    Entity entity = (Entity) body.getUserData();

    boolean Top = false;
    boolean Bottom = false;
    boolean Left = false;
    boolean Right = false;

    if (entity.Top.collisionCount > 0)
        Top = true;/* w w  w  .j  a v a 2  s .c om*/
    else if (entity.Bottom.collisionCount > 0)
        Bottom = true;
    if (entity.Left.collisionCount > 0)
        Left = true;
    else if (entity.Right.collisionCount > 0)
        Right = true;

    if (!Top && !Left && !Right)
        body.setGravityScale(1);

    if (Left || Right || Top)
        body.setGravityScale(0);

    if (entity.isJumping) {
        boolean doJump = true;

        if (Left && entity.JumpX < 0)
            doJump = false;

        if (Right && entity.JumpX > 0)
            doJump = false;

        if (Top && entity.JumpY > 0)
            doJump = false;

        if (Bottom && entity.JumpY < 0)
            doJump = false;

        if (doJump) {
            body.applyLinearImpulse(entity.JumpX, entity.JumpY, body.getPosition().x, body.getPosition().y,
                    true);
            entity.isInControl = false;
            body.setGravityScale(1);
            return;
        }
    }

    if (entity.isWalkingLeft && Top) {
        body.setLinearVelocity(-entity.speed, 0);
        body.setGravityScale(0);
    } else if (entity.isWalkingLeft && Bottom) {
        body.setLinearVelocity(-entity.speed, body.getLinearVelocity().y);
        body.setGravityScale(1);
    }

    else if (entity.isWalkingRight && Top) {
        body.setLinearVelocity(entity.speed, 0);
        body.setGravityScale(0);
    } else if (entity.isWalkingRight && Bottom) {
        body.setLinearVelocity(entity.speed, body.getLinearVelocity().y);
        body.setGravityScale(1);
    }

    else if (entity.isWalkingUp && Left) {
        body.setLinearVelocity(body.getLinearVelocity().x, entity.speed);
        body.setGravityScale(0);
    } else if (entity.isWalkingUp && Right) {
        body.setLinearVelocity(body.getLinearVelocity().x, entity.speed);
        body.setGravityScale(0);
    }

    else if (entity.isWalkingDown && Left) {
        body.setLinearVelocity(body.getLinearVelocity().x, -entity.speed);
        body.setGravityScale(0);
    } else if (entity.isWalkingDown && Right) {
        body.setLinearVelocity(body.getLinearVelocity().x, -entity.speed);
        body.setGravityScale(0);
    }

    else if (entity.isInControl) {
        body.setLinearVelocity(body.getLinearVelocity().x *= 0.1f, body.getLinearVelocity().y *= 0.1f);
    }

}