2016-09-14 76 views
0

拉賈瓦利版本或分公司 拉賈瓦利使用MotionEvent當發生MotionEvent(觸摸)時,使相機移動。我試過https://github.com/Rajawali/Rajawali/issues/428的代碼,但它對我沒有用。我不能拉賈瓦利

我的代碼

public class Renderer extends RajawaliRenderer { 

public Context context; 
private DirectionalLight directionalLight; 
public Renderer(Context context) { 
    super(context); 
    this.context = context; 
    setFrameRate(60); 
} 

private Object3D object, star; 
public void initScene(){ 

    getCurrentScene().setBackgroundColor(Color.rgb(5,104,255)); 
    directionalLight = new DirectionalLight(1f, .2f, -1.0f); 
    directionalLight.setColor(1.0f, 1.0f, 1.0f); 
    directionalLight.setPower(2); 
    getCurrentScene().addLight(directionalLight); 

    //LoaderOBJ objParser = new LoaderOBJ(this,"Load/1c_obj"); 
    LoaderOBJ objParser = new LoaderOBJ(mContext.getResources(),mTextureManager, R.raw.primo_obj); 
    LoaderOBJ starParser = new LoaderOBJ(mContext.getResources(),mTextureManager,R.raw.star_obj); 
    try { 
     objParser.parse(); 
     starParser.parse(); 
     object = objParser.getParsedObject(); 
     star = starParser.getParsedObject(); 
     getCurrentScene().addChild(star); 
     getCurrentScene().addChild(object); 

    } catch (ParsingException e) { 
     e.printStackTrace(); 
    } 


    getCurrentCamera().setLookAt(object.getWorldPosition()); 
    Log.d("->",getCurrentCamera().getX()+","+getCurrentCamera().getY()+","+getCurrentCamera().getZ()); 

    getCurrentCamera().setZ(40); 
} 


@Override 
public void onRender(final long elapsedTime, final double deltaTime) { 
    super.onRender(elapsedTime, deltaTime); 

    Camera cam = getCurrentCamera(); 
    Vector3 s = cam.getPosition(); 
    if (touchTurn != 0) { 

     if (flagMulti) { 
      Double r = Math.sqrt(s.x * s.x + s.z * s.z); 
      angle += touchTurn; 
      angle %= 360; 
      cam.setPosition((float) (r * Math.cos(angle)), s.y, (float) (r * Math.sin(angle))); 
      cam.setLookAt(object.getLookAt()); 
     } 
     else{ 
      s.x+=touchTurn*5; 
      cam.setPosition(s); 
     } 
     touchTurn = 0; 
    } 

    if (touchTurnUp != 0) { 
     if (!flagMulti) { 
      s.z += touchTurnUp * 5; 
      cam.setPosition(s); 
     } 
     touchTurnUp = 0; 
    } 

} 



@Override 
public void onTouchEvent(MotionEvent me){ 
    Log.d("touch ","Log touch"); 
    int pointerCount = me.getPointerCount(); 
    if(pointerCount == 2) 
     flagMulti = true; 


    if (me.getAction() == MotionEvent.ACTION_DOWN) { 
     xpos = me.getX(); 
     ypos = me.getY(); 
    } 

    if (me.getAction() == MotionEvent.ACTION_UP) { 
     xpos = -1; 
     ypos = -1; 
     touchTurn = 0; 
     touchTurnUp = 0; 
    } 

    if (me.getAction() == MotionEvent.ACTION_MOVE) { 
     float xd = me.getX() - xpos; 
     float yd = me.getY() - ypos; 

     xpos = me.getX(); 
     ypos = me.getY(); 

     touchTurn = xd/-100f; 
     touchTurnUp = yd/-100f; 

     Log.d("touchTurn ", touchTurn+" - "+touchTurnUp); 
    } 



    try { 
     Thread.sleep(15); 
    } catch (Exception e) { 
    } 

} 

public void onOffsetsChanged(float x, float y, float z, float w, int i, int j){ 

}} 

回答

0

你可以考慮arcballCamera和目標,以你的3D對象。然後,您可以旋轉3D對象並使用arcballCamera進行放大/縮小。