我用rajawali框架加载了一个对象......我想用相机旋转它......
这是我的代码:
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);
}
这是我的班级:
public class Renderer extends RajawaliRenderer implements SensorEventListener {
}
我在posx
收到了数字posy
和onSensorChanged
,但mCamera.setPosition(posx, posy, 0)
无效!
答案 0 :(得分:0)
我改变了这些:
mCamera.setPosition(posx, posy, 0);
to =>
getCurrentCamera().setRotation(posx, posy, 0);
mCamera.setLookAt(currentPosx, currentPosy, 0);
to =>
getCurrentCamera().setLookAt(currentPosx, currentPosy, 0);
和
mCamera.setPosition(10, 50, 150); to => mCamera.setPosition(0, 0, 0);
现在有效!