Android Open Source - SpaceRagdoll Space Renderer






From Project

Back to project page SpaceRagdoll.

License

The source code is released under:

GNU General Public License

If you think the Android project SpaceRagdoll listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright (C) 2015 Tuukka Ruhanen/*www . jav a2 s .  c  o m*/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package fi.karu.spaceragdoll;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;

/**
 * Renders everything, determines camera scaling, etc.
 */
public class SpaceRenderer {

    public static float ppc = Gdx.graphics.getPpcX(); // pixels per centimeter
    public static final float cpu = .3f; // centimeters per unit (desktop)
    // public static final float cpu = .15f; // centimeters per unit (android)
    private OrthographicCamera cam = new OrthographicCamera(); // define screen aspect ratio;
    private int screenw;
    private int screenh;
    /**view width in units*/
    private float camw;
    /**view height in units*/
    private float camh;
    private Space space;
    ShapeRenderer renderer = new ShapeRenderer();
    Box2DDebugRenderer b2renderer = new Box2DDebugRenderer();

    public SpaceRenderer(Space space) {
        this.space = space;
        b2renderer.setDrawJoints(true);
    }

    public void render() {
        // scale view if too small
        float dist = (space.getBirdPosition().len() + 1f) * ppc * cpu;
        float scaleFactor = Math.min(screenh, screenw) / 2 / dist;
        float ppu; // pixels per unit
        if (scaleFactor < 1)
            ppu = ppc * cpu * scaleFactor;
        else
            ppu = ppc * cpu;
        updateCamera(ppu);
        // render sky (background)
        renderer.setProjectionMatrix(cam.combined);
        renderer.begin(ShapeType.Point);
        renderer.setColor(new Color(.7f, .7f, .7f, 1));
        float s = 2;
        for (float i = -(int) (camw / 2 / s) * s; i < camw; i += s) {
            for (float j = -(int) (camh / 2 / s) * s; j < camh; j += s) {
                renderer.point(i, j, 0);
            }
        }
        renderer.end();
        renderer.begin(ShapeType.Filled);
        // // render rocket
        float r = Ragdoll.mainBodyLength / 3;
        float a = space.getBirdAngle();
        Vector2 pos = space.getBirdPosition();
        float xr = (float) (pos.x + r * Math.cos(a));
        float yr = (float) (pos.y + r * Math.sin(a));
        r = Ragdoll.r;
        renderer.setColor(new Color(1, 0, 0, 1));
        renderer.circle(xr, yr, r);
        // render jet
        if (space.getBirdState() == Ragdoll.State.ON) {
            float l = Ragdoll.l;
            float d = Ragdoll.d;
            float xj1 = (float) (xr - r * Math.cos(a));
            float yj1 = (float) (yr - r * Math.sin(a));
            float xj2 = (float) (xr - l * Math.cos(a) - d * Math.sin(a));
            float yj2 = (float) (yr - l * Math.sin(a) + d * Math.cos(a));
            float xj3 = (float) (xr - l * Math.cos(a) + d * Math.sin(a));
            float yj3 = (float) (yr - l * Math.sin(a) - d * Math.cos(a));
            renderer.triangle(xj1, yj1, xj2, yj2, xj3, yj3, new Color(1, 0, 0, 1), new Color(1, 1, 0, 0), new Color(1, 1, 0, 0));
        }
        renderer.end();

        b2renderer.render(space.getWorld(), cam.combined);
    }

    /**
     * @return view dimensions in units
     */
    public static Vector2 getViewDimensions() {
        float width = Gdx.graphics.getWidth() / ppc / cpu;
        float height = Gdx.graphics.getHeight() / ppc / cpu;
        return new Vector2(width, height);
    }

    public void resize(int w, int h) {
        screenw = w;
        screenh = h;
        updateCamera(ppc * cpu);
    }

    private void updateCamera(float ppu) {
        camw = screenw / ppu;
        camh = screenh / ppu;
        cam.setToOrtho(false, camw, camh);
        cam.position.set(0, 0, 0); // view to origo
        cam.update();
    }
}




Java Source Code List

fi.karu.spacebird_android.SpaceRagdollActivity.java
fi.karu.spaceragdoll.GameScreen.java
fi.karu.spaceragdoll.Planet.java
fi.karu.spaceragdoll.Ragdoll.java
fi.karu.spaceragdoll.SpaceRagdoll.java
fi.karu.spaceragdoll.SpaceRenderer.java
fi.karu.spaceragdoll.Space.java
fi.karu.spaceragdoll.controller.Controller.java
fi.karu.spaceragdoll_desktop.SpaceRagdollDesktop.java