Android Open Source - RubeLoader Simple Spatial






From Project

Back to project page RubeLoader.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUC...

If you think the Android project RubeLoader 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

package com.gushikustudios.rube;
/*  w  w  w . jav a  2  s . c  o  m*/
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

/**
 * Simple class for spatial (image) rendering.  If a body reference is included, it's position will serve as the local coordinate system.
 * 
 * @author tescott
 *
 */
public class SimpleSpatial {
  private Sprite mSprite;
  private Body mBody;
  private final Vector2 mCenter = new Vector2();
  private final Vector2 mHalfSize = new Vector2();
  private float mRotation;
  private static final Vector2 mTmp = new Vector2();

  public SimpleSpatial(Texture texture, boolean flip, Body body, Color color, Vector2 size,
      Vector2 center, float rotationInDegrees) {
    mSprite = new Sprite(texture);
    defineSpatial(flip,body,color,size,center,rotationInDegrees);
  }

  public SimpleSpatial(TextureRegion region, boolean flip, Body body, Color color,
      Vector2 size, Vector2 center, float rotationInDegrees) {
    mSprite = new Sprite(region);
    defineSpatial(flip,body,color,size,center,rotationInDegrees);
  }
  
  public void defineSpatial(boolean flip, Body body, Color color, Vector2 size,
      Vector2 center, float rotationInDegrees)
  {
    mBody = body;
    mSprite.flip(flip, false);
    mSprite.setColor(color);
    mRotation = rotationInDegrees;
    mSprite.setSize(size.x, size.y);
    mSprite.setOrigin(size.x / 2, size.y / 2);
    mHalfSize.set(size.x / 2, size.y / 2);
    mCenter.set(center);

    if (body != null) {
      mTmp.set(body.getPosition());
      mSprite.setPosition(mTmp.x - size.x / 2, mTmp.y - size.y / 2);

      float angle = mBody.getAngle() * MathUtils.radiansToDegrees;
      mTmp.set(mCenter).rotate(angle).add(mBody.getPosition())
          .sub(mHalfSize);
      mSprite.setRotation(mRotation + angle);
    } else {
      mTmp.set(center.x - size.x / 2, center.y - size.y / 2);
      mSprite.setRotation(rotationInDegrees);
    }

    mSprite.setPosition(mTmp.x, mTmp.y);
  }

  public void render(SpriteBatch batch, float delta) {
    // if this is a dynamic spatial...
    if (mBody != null) {
      // use body information to render it...
      float angle = mBody.getAngle() * MathUtils.radiansToDegrees;
      mTmp.set(mCenter).rotate(angle).add(mBody.getPosition())
          .sub(mHalfSize);
      mSprite.setPosition(mTmp.x, mTmp.y);
      mSprite.setRotation(mRotation + angle);
      mSprite.draw(batch);
    } else {
      // else just draw it wherever it was defined at initialization
      mSprite.draw(batch);
    }
  }
}




Java Source Code List

com.gushikustudios.rube.MainActivity.java
com.gushikustudios.rube.PolySpatial.java
com.gushikustudios.rube.PolySpatial.java
com.gushikustudios.rube.RubeDefaults.java
com.gushikustudios.rube.RubeLoaderTestDesktop.java
com.gushikustudios.rube.RubeLoaderTestDesktop.java
com.gushikustudios.rube.RubeLoaderTest.java
com.gushikustudios.rube.RubeLoaderTest.java
com.gushikustudios.rube.RubeScene.java
com.gushikustudios.rube.SimpleSpatial.java
com.gushikustudios.rube.SimpleSpatial.java
com.gushikustudios.rube.loader.RubeSceneAsyncLoader.java
com.gushikustudios.rube.loader.RubeSceneLoader.java
com.gushikustudios.rube.loader.RubeSceneSyncLoader.java
com.gushikustudios.rube.loader.serializers.BodySerializer.java
com.gushikustudios.rube.loader.serializers.FixtureSerializer.java
com.gushikustudios.rube.loader.serializers.ImageSerializer.java
com.gushikustudios.rube.loader.serializers.JointSerializer.java
com.gushikustudios.rube.loader.serializers.RubeWorldSerializer.java
com.gushikustudios.rube.loader.serializers.Vector2Serializer.java
com.gushikustudios.rube.loader.serializers.WorldSerializer.java
com.gushikustudios.rube.loader.serializers.utils.RubeImage.java
com.gushikustudios.rube.loader.serializers.utils.RubeVertexArray.java