Question

I loaded an object with rajawali framework... and I want to rotate it unison with camera..

Here is my code:

public Renderer(Context context) {
    super(context);
    setFrameRate(60);
    this.context = context;
    mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mAccVals = new Number3d();
}

public void initScene() {  
    DirectionalLight light = new DirectionalLight(0, 0, 1);
    light.setPower(4);     
    mCamera = new Camera();
    LoaderOBJ objParser = new LoaderOBJ(mContext.getResources(), mTextureManager, R.raw.ship_obj);
    try {
        objParser.parse();
    } catch (ParsingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mObjectGroup = objParser.getParsedObject();
    mObjectGroup.setPosition(0, 0, 0);
    mCamera.setPosition(10, 50, 150);
    addCamera(mCamera);
    addChild(mObjectGroup); 
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
}   

public void onDrawFrame(GL10 glUnused) {
    super.onDrawFrame(glUnused);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
        return; 
    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));
    double posx = mAccVals.x * .2f;
    double posy = mAccVals.y * .2f;
    mCamera.setPosition(posx, posy, 0);     
    double currentPosx = mCamera.getPosition().x;
    double currentPosy = mCamera.getPosition().y;
    mCamera.setLookAt(currentPosx, currentPosy, 0);
}

And this is my class:

public class Renderer extends RajawaliRenderer implements SensorEventListener {
}

I receive numbers posx and posy in onSensorChanged but mCamera.setPosition(posx, posy, 0) doesn't work!

Was it helpful?

Solution

I changed these:

mCamera.setPosition(posx, posy, 0); 

to =>

getCurrentCamera().setRotation(posx, posy, 0);


mCamera.setLookAt(currentPosx, currentPosy, 0); 

to =>

getCurrentCamera().setLookAt(currentPosx, currentPosy, 0);

And

mCamera.setPosition(10, 50, 150); to => mCamera.setPosition(0, 0, 0);

Now it works!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top