Example usage for com.badlogic.gdx.math MathUtils degreesToRadians

List of usage examples for com.badlogic.gdx.math MathUtils degreesToRadians

Introduction

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

Prototype

float degreesToRadians

To view the source code for com.badlogic.gdx.math MathUtils degreesToRadians.

Click Source Link

Document

multiply by this to convert from degrees to radians

Usage

From source file:Tabox2D.java

License:Open Source License

private Tabody generateRegularPoly(String name, String type, float x, float y, float rad) {
    // Scale proportions:
    x /= meterSize;/* w w  w  . j  a va  2s .c o  m*/
    y /= meterSize;
    rad /= meterSize;

    PolygonShape polygonShape;
    BodyDef defPoly = new BodyDef();

    setType(defPoly, type);

    // Generate points:
    List<Vector2> pts = new ArrayList<Vector2>();
    Vector2 p0 = new Vector2(0, rad);

    float conv = MathUtils.degreesToRadians;
    float angleInDeg = polyInfo.get(name + "_angle");
    float cos = MathUtils.cos(conv * angleInDeg);
    float sin = MathUtils.sin(conv * angleInDeg);

    for (int i = 0; i < polyInfo.get(name); i++) {
        pts.add(new Vector2(p0.x, p0.y));
        p0.set(p0.x, p0.y);

        float newX = p0.x * cos - p0.y * sin;
        float newY = p0.x * sin + p0.y * cos;

        p0.x = newX;
        p0.y = newY;
    }

    // Get bounding box:

    float[] rawPoints = new float[pts.size() * 2];
    int pointIndex = 0;
    for (int i = 0; i < rawPoints.length - 1; i += 2) {
        rawPoints[i] = pts.get(pointIndex).x;
        rawPoints[i + 1] = pts.get(pointIndex).y;
        pointIndex++;
    }

    Polygon polyForBox = new Polygon();
    polyForBox.setVertices(rawPoints);

    Rectangle boundingRect = polyForBox.getBoundingRectangle();
    float boxX = boundingRect.x;
    float boxY = boundingRect.y;
    float boxW = boundingRect.getWidth();
    float boxH = boundingRect.getHeight();

    Vector2 aabbCenter = new Vector2(boxX + boxW / 2, boxY + boxH / 2);
    defPoly.position.set(x, y);

    Tabody regularPoly = new Tabody();
    regularPoly.body = world.createBody(defPoly);
    //regularPoly.body.setFixedRotation(true);
    polygonShape = new PolygonShape();

    //polygonShape.setAsBox(w / 2, h / 2);
    for (int i = 0; i < rawPoints.length - 1; i += 2) {
        rawPoints[i] -= aabbCenter.x;
        rawPoints[i + 1] -= aabbCenter.y;
    }
    //rawPoints[0] += 0.5;
    polygonShape.set(rawPoints);

    FixtureDef fixtureBox = new FixtureDef();
    fixtureBox.shape = polygonShape;
    fixtureBox.density = 1;
    fixtureBox.friction = 1;
    fixtureBox.restitution = 0;

    ////////////////////////////////////////
    regularPoly.w = boxW * meterSize;//radius * 2 * meterSize;
    regularPoly.h = boxH * meterSize;//radius * 2 * meterSize;
    regularPoly.fixture = fixtureBox;
    regularPoly.bodyType = "poly";
    ////////////////////////////////////////

    regularPoly.body.createFixture(fixtureBox);
    polygonShape.dispose();
    tabodies.add(regularPoly);
    return regularPoly;
}

From source file:CB_UI_Base.graphics.Geometry.CircularSegment.java

License:Open Source License

/**
 * Calculate the vertices of this circle with a minimum segment length of 10. <br>
 * OR a minimum segment count of 18. <br>
 * <br>//from  ww  w  . ja  v  a  2 s.c om
 * For every segment are compute a triangle from the segment start, end and the center of this circle.
 */
@Override
public void Compute() {
    if (!isDirty)
        return; // Nothing todo

    chkStartEnd();

    // calculate segment count
    double alpha = (360 * MIN_CIRCLE_SEGMENTH_LENGTH) / (MathUtils.PI2 * radius);
    int segmente = Math.max(MIN_CIRCLE_SEGMENTH_COUNT, (int) (360 / alpha));

    // calculate beginn and end
    float length = end - start;
    segmente = (int) ((segmente * (Math.abs(length) / 360) + 0.5));
    float thetaBeginn = start;
    float thetaEnd = end;

    // calculate theta step
    double thetaStep = length / segmente;

    segmente++;

    // initialize arrays
    vertices = new float[(segmente + 1) * 2];
    triangleIndices = new short[(segmente) * 3];

    int index = 0;

    // first point is the center point
    vertices[index++] = centerX;
    vertices[index++] = centerY;

    int triangleIndex = 0;
    short verticeIdex = 1;
    boolean beginnTriangles = false;

    for (float i = thetaBeginn; !(i > thetaEnd); i += thetaStep) {

        float rad = MathUtils.degreesToRadians * i;

        vertices[index++] = centerX + radius * MathUtils.cos(rad);
        vertices[index++] = centerY + radius * MathUtils.sin(rad);

        if (!beginnTriangles) {
            if (index % 6 == 0)
                beginnTriangles = true;
        }

        if (beginnTriangles) {
            triangleIndices[triangleIndex++] = 0;
            triangleIndices[triangleIndex++] = verticeIdex++;
            triangleIndices[triangleIndex++] = verticeIdex;
        }

    }

    // last triangle
    // triangleIndices[triangleIndex++] = 0;
    // triangleIndices[triangleIndex++] = verticeIdex;
    // triangleIndices[triangleIndex++] = 1;

    isDirty = false;
}

From source file:CB_UI_Base.graphics.Images.VectorDrawable.java

License:Open Source License

public void draw(Batch batch, float x, float y, final float width, final float height, float rotated) {

    if (isDisposed.get()) {
        return;/*from  w w w. j a va 2  s  .  c  om*/
    }

    final Matrix4 oriMatrix = GL.batch.getProjectionMatrix().cpy();
    Matrix4 thisDrawMatrix = oriMatrix.cpy();
    thisDrawMatrix.translate(x, y, 0);

    drawFbo(batch, x, y, width, height, oriMatrix, thisDrawMatrix);

    for (MatrixDrawable drw : rotateDrawableList) {

        Matrix4 matrix = thisDrawMatrix.cpy();
        ext_Matrix drwMatrix = new GL_Matrix(drw.matrix);
        matrix.mul(drwMatrix.getMatrix4().cpy());
        GL.batch.setProjectionMatrix(matrix);
        drw.drawable.draw(GL.batch, 0, 0, width, height, -rotated * MathUtils.degreesToRadians);
    }
    GL.batch.setProjectionMatrix(oriMatrix);
}

From source file:coder5560.gdxai.SteeringActor.java

License:Apache License

@Override
public float getOrientation() {
    return getRotation() * MathUtils.degreesToRadians;
}

From source file:coder5560.gdxai.SteeringActor.java

License:Apache License

private void applySteering(SteeringAcceleration<Vector2> steering, float time) {
    // Update position and linear velocity. Velocity is trimmed to maximum
    // speed/*  w w  w  . j av  a2 s . co m*/
    position.mulAdd(linearVelocity, time);
    linearVelocity.mulAdd(steering.linear, time).limit(getMaxLinearSpeed());

    // Update orientation and angular velocity
    if (independentFacing) {
        setRotation(getRotation() + (angularVelocity * time) * MathUtils.radiansToDegrees);
        angularVelocity += steering.angular * time;
    } else {
        // If we haven't got any velocity, then we can do nothing.
        if (!linearVelocity.isZero(MathUtils.FLOAT_ROUNDING_ERROR)) {
            float newOrientation = vectorToAngle(linearVelocity);
            angularVelocity = (newOrientation - getRotation() * MathUtils.degreesToRadians) * time; // this is superfluous if independentFacing is
                                                                                                    // always true
            setRotation(newOrientation * MathUtils.radiansToDegrees);
        }
    }
}

From source file:com.agateau.pixelwheels.bonus.BonusSpot.java

License:Open Source License

public BonusSpot(Assets assets, AudioManager audioManager, GameWorld gameWorld, float x, float y) {
    final float U = Constants.UNIT_FOR_PIXEL;
    mAudioManager = audioManager;/*from ww  w  .j  a  v a  2  s . c  om*/
    mX = x;
    mY = y;

    mRegion = assets.gift;
    mSound = assets.soundAtlas.get("bonus");

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(U * mRegion.getRegionWidth() / 2, U * mRegion.getRegionHeight() / 2);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(mX, mY);

    mBody = gameWorld.getBox2DWorld().createBody(bodyDef);
    Fixture fixture = mBody.createFixture(shape, 1f);
    fixture.setSensor(true);
    mBody.setUserData(this);

    mBody.setAngularVelocity(240 * MathUtils.degreesToRadians);

    shape.dispose();
}

From source file:com.agateau.pixelwheels.bonus.Bullet.java

License:Open Source License

public static Bullet create(Assets assets, GameWorld gameWorld, AudioManager audioManager, Racer shooter,
        float originX, float originY, float angle) {
    Bullet object = sPool.obtain();//from ww  w.j a v a2s. co  m
    if (object.mBodyDef == null) {
        object.firstInit(assets);
    }
    object.mShooter = shooter;
    object.mGameWorld = gameWorld;
    object.mAudioManager = audioManager;
    object.setFinished(false);
    object.mJustShot = true;
    object.mBodyDef.position.set(originX, originY);
    object.mBodyDef.angle = angle * MathUtils.degreesToRadians;

    object.mBody = gameWorld.getBox2DWorld().createBody(object.mBodyDef);
    object.mBody.createFixture(object.mShape, 0f);
    object.mBody.setUserData(object);
    object.mBody.applyLinearImpulse(IMPULSE * MathUtils.cosDeg(angle), IMPULSE * MathUtils.sinDeg(angle),
            originX, originY, true);

    Box2DUtils.setCollisionInfo(object.mBody, CollisionCategories.RACER_BULLET,
            CollisionCategories.WALL | CollisionCategories.RACER | CollisionCategories.EXPLOSABLE);
    return object;
}

From source file:com.agateau.pixelwheels.racer.GroundCollisionHandlerComponent.java

License:Open Source License

private void actRecovering(float delta) {
    final float POSITION_TOLERANCE = 0.1f;
    final float ANGLE_TOLERANCE = MathUtils.degreesToRadians;

    mVelocity.set(mDropPoint.x, mDropPoint.y).sub(mVehicle.getBody().getPosition()).scl(1 / delta);
    float speed = mVelocity.len();
    if (speed > MAX_RECOVERING_SPEED) {
        mVelocity.scl(MAX_RECOVERING_SPEED / speed);
    }/*from w  w w  .  j av  a 2  s.c om*/

    float angularVelocity = MathUtils.clamp((mDropPoint.angle - mVehicle.getAngle()) / delta,
            -MAX_RECOVERING_ROTATION_SPEED, MAX_RECOVERING_ROTATION_SPEED) * MathUtils.degreesToRadians;

    boolean posOK = MathUtils.isZero(speed * delta, POSITION_TOLERANCE);
    boolean angleOK = MathUtils.isZero(angularVelocity * delta, ANGLE_TOLERANCE);

    if (posOK) {
        mVehicle.getBody().setLinearVelocity(0, 0);
        mVehicle.getBody().setAngularVelocity(0);
        mState = State.DROPPING;
        mTime = 0;
    } else {
        mVehicle.getBody().setLinearVelocity(mVelocity);
        mVehicle.getBody().setAngularVelocity(angleOK ? 0 : angularVelocity);
        mHelicopter.setPosition(mVehicle.getPosition());
        mHelicopter.setAngle(mVehicle.getAngle());
    }
}

From source file:com.agateau.pixelwheels.racer.Vehicle.java

License:Open Source License

public Vehicle(TextureRegion region, GameWorld gameWorld, float originX, float originY, Array<Shape2D> shapes,
        float angle) {
    mGameWorld = gameWorld;/*  ww w. j  a  v a2s .  c  o m*/

    // Main
    mRegion = region;

    // Body
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(originX, originY);
    bodyDef.angle = angle * MathUtils.degreesToRadians;
    mBody = mGameWorld.getBox2DWorld().createBody(bodyDef);

    // Body fixtures
    for (Shape2D shape : shapes) {
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = Box2DUtils.createBox2DShape(shape, Constants.UNIT_FOR_PIXEL);
        fixtureDef.density = GamePlay.instance.vehicleDensity / 10.0f;
        fixtureDef.friction = 0.2f;
        fixtureDef.restitution = GamePlay.instance.vehicleRestitution / 10.0f;
        mBody.createFixture(fixtureDef);
        fixtureDef.shape.dispose();
    }
}

From source file:com.agateau.pixelwheels.racer.Wheel.java

License:Open Source License

public Wheel(GameWorld gameWorld, Vehicle vehicle, TextureRegion region, float posX, float posY, float angle) {
    mGameWorld = gameWorld;/*w ww .  j  a  va  2  s  . co  m*/
    mVehicle = vehicle;
    mRegion = region;

    float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth();
    float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight();

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(posX, posY);
    bodyDef.angle = angle * MathUtils.degreesToRadians;
    mBody = mGameWorld.getBox2DWorld().createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.set(Box2DUtils.createOctogon(w, h, w / 4, w / 4));
    mBody.createFixture(shape, 2f);
    shape.dispose();
}