Min3D for Android: Using The Accelerometer To Control The Camera

NOTE: This OpenGL ES 1.1 framework isn’t maintained anymore. Check out the OpenGL ES 2.0 Rajawali framework which also supports live wallpapers.

I haven’t done anything with Min3D for while. I know people have logged some bugs on Google Code but I haven’t had the time to look at them yet. Shame on me.

Seeing the awesomeness of OpenGL ES 2.0 and GLSL I’m not sure if I want to keep maintaining Min3D. I might start a new engine from scratch. Then again, Molehill (hardware accelerated 3D Flash) is coming to Android so why bother. Well, we’ll see. I’d love to hear your comments on this.

I did find time to create a new demo that uses the accelerometer to control the camera. It creates a very nice depth effect. Have a look at the video:

Here’s the code. Should explain itself :-). You can download the Eclipse project archive here.


package com.rozengain.min3d.accelerometer;

import min3d.core.Object3dContainer;
import min3d.core.RendererActivity;
import min3d.objectPrimitives.SkyBox;
import min3d.parser.IParser;
import min3d.parser.Parser;
import min3d.vos.Light;
import min3d.vos.Number3d;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class Min3DAccelerometerActivity extends RendererActivity implements SensorEventListener {
	private final float FILTERING_FACTOR = .3f;

	private SkyBox mSkyBox;
	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	private Number3d mAccVals;
	private Object3dContainer mMonster;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mAccVals = new Number3d();
	}

	public void initScene()
	{
		scene.lights().add(new Light());

		mSkyBox = new SkyBox(5.0f, 2);
		mSkyBox.addTexture(SkyBox.Face.North, 	R.drawable.wood_back, 	"north");
		mSkyBox.addTexture(SkyBox.Face.East, 	R.drawable.wood_right, 	"east");
		mSkyBox.addTexture(SkyBox.Face.South, 	R.drawable.wood_back, 	"south");
		mSkyBox.addTexture(SkyBox.Face.West, 	R.drawable.wood_left, 	"west");
		mSkyBox.addTexture(SkyBox.Face.Up,		R.drawable.ceiling, 	"up");
		mSkyBox.addTexture(SkyBox.Face.Down, 	R.drawable.floor, 		"down");
		mSkyBox.scale().y = 0.8f;
		mSkyBox.scale().z = 2.0f;
		scene.addChild(mSkyBox);

		IParser parser = Parser.createParser(Parser.Type.MAX_3DS,
				getResources(), "com.rozengain.min3d.accelerometer:raw/monster_high", true);
		parser.parse();

		mMonster = parser.getParsedObject();
		mMonster.scale().x = mMonster.scale().y = mMonster.scale().z  = .1f;
		mMonster.position().y = -2.5f;
		mMonster.position().z = -3;
		scene.addChild(mMonster);

		mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;

		// low-pass filter to make the movement more stable
		mAccVals.x = (float) (-event.values[1] * FILTERING_FACTOR + mAccVals.x * (1.0 - FILTERING_FACTOR));
		mAccVals.y = (float) (event.values[0] * FILTERING_FACTOR + mAccVals.y * (1.0 - FILTERING_FACTOR));

		scene.camera().position.x = mAccVals.x * .2f;
        scene.camera().position.y = mAccVals.y * .2f;

        scene.camera().target.x = -scene.camera().position.x;
        scene.camera().target.y = -scene.camera().position.y;
	}
}
  • Delicious
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter

22 thoughts on “Min3D for Android: Using The Accelerometer To Control The Camera

  1. Nebojsa says:

    Min3d looks great, but I don’t know how to use it in my project… do you have a compiled jar library or something similar? Or maybe just upload sources to a zip file so we can include in projects.

  2. Doug Snead says:

    re: “I’d love to hear your comments on this.”

    I was looking for a simple skybox for a minimal android java opengl animated model I have, and the next thing I knew, I was reading your api, source code and how you did things. (Nicely done!!)

    To use my own model in/with a min3d scene, I was wondering, would the easiest way to do that be to draw my model (in the proper position) at the start of my updateScene method – doing bad things with gl snagged from the Renderer (and get Renderer from Shared)?

    Or might things be better (if not easier) if I made my model coexist with Object3D – then it could integrate with the min3d world in ways it can’t otherwise. (My model has a number of bones-like moving parts.)

    The ability to export models from blender is really nice.

  3. Nate says:

    When are OpenGL ES 2.0 and Molehill supposed to come to android? Where can I read about those? Thanks!

  4. Ivica says:

    Hi, Dennis.

    I’m so excited about this engine/tutorial of yours that I can’t even begin to tell you how grateful I am.
    I’m a developer for many years now but never did anything noteworthy with OpenGL or in 3D at all.
    I got myself an Android phone these days and wanted to play with it and try 3D on the phone. I knew almost nothing about OpenGL but with your help, in a few hours I had a pretty functional Android application running OpenGL with loaded 3D object previously designed in AutoCAD, textured, with functional lights, shadows, zoom, rotate, etc… When I finish pinch-zoom functionality I will be very happy.

    Thank you for all the effort you’ve put into this project, it’s greatly appreciated.

    Cheers

    Ivica

  5. ScimitarM says:

    Min3D has proven invaluable to my projects. It has helped me learn a bit about using OpenGL with Android and will continue to prove useful long after OpenGL ES 2.0 emerges in Android. As such,if you’re thinking of moving to 2.0 and abandoning 1.0/fixed functions,why not make a branch of Min3D that uses that. In any event,I think Min3D is awesome. Thanks!

  6. andre says:

    Hello , great job with the project .. I want to ask you how i can rotate the accelerometer to see the whole box? I cant see the back pic .
    Thank you in advance.

  7. asher says:

    Hey Dennis, Awesome job on this project. I pulled down the latest code base from SVN and had no problems building the project. What are your plans for collision detection? That would be way cool to integrate into this. Also, keyboard/dpad support would be a nice additional input type along side touch/accelerometer.

  8. Hi Asher,
    I’m actually working on a new engine that uses the power of OpenGL ES 2.0. So this means that I stopped working on Min3D. More news on this new engine soon. Probably next week :-)
    Cheers,
    Dennis

  9. Hi ScimitarM,
    I’ve actually started building a new OpenGL ES 2.0 based engine. It’s not Min3D but something brand new. The Min3D core wasn’t built by me so I wanted to start afresh with this new engine. The materials in this new engine will be much nicer because of the power of GLSL shaders!
    Cheers
    Dennis

  10. @Robert: this should be possible. There’s an example in the demo project. You might want to compare the .md2 files to see if there are any differences there.

  11. Meeks says:

    Dennis, Do you know if it is possible to implement a pinch-zoom function within the load obj or 3ds model examples? I have been trying to figure this out for my senior design project but can’t really find any examples where ppl use 3D models instead of images to be zoomed in/out of.

  12. Vishwanath says:

    Min3d has a really great feature to render but what about TouchListner..
    Rather than accelerometer how to move it using TouchListner?

  13. Arun Goyal says:

    Hi dennis

    can u tell me if i change images instead of these white blank space appear.
    how can i solve this problem and what to do if i have to show interior of house or car etc.

    thanx

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>