2011-06-25 16 views
5

मैंने वेब पर पोस्ट किए गए ट्यूटोरियल्स की सहायता से एक नमूना आवेदन विकसित किया है। मेरा लक्ष्य एक्सेलेरोमीटर तक पहुंचना और फोन अभिविन्यास के अनुसार गेंद को स्थानांतरित करना है। मैं हद तक प्रमाणित करने में सफल रहा था। लेकिन मैं दो issesएंड्रॉइड एक्सेलेरोमीटर चलती गेंद

  1. गेंद स्क्रीन
  2. गेंद आंदोलन के लिए बाध्य से बाहर जा रहा है चिकनी

यहाँ मेरी है (यह गायब हो जाता है और फिर से स्क्रीन पर दिखाई देता दिखता है) नहीं है कोड। क्या गेंद में चिकनी और सटीक गति प्राप्त करने के लिए मुझे कोई बदलाव करने की ज़रूरत है क्योंकि हम बहुत सारे गेम देखते हैं।

public class Accelerometer extends Activity implements SensorEventListener{ 
    /** Called when the activity is first created. */ 
    CustomDrawableView mCustomDrawableView = null; 
    ShapeDrawable mDrawable = new ShapeDrawable(); 
    public static int x; 
    public static int y; 
     private Bitmap mBitmap; 
     private Bitmap mWood; 
    private SensorManager sensorManager = null; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState); 
     // Get a reference to a SensorManager 
     sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
     mCustomDrawableView = new CustomDrawableView(this); 
     setContentView(mCustomDrawableView); 
     // setContentView(R.layout.main); 

    } 

    // This method will update the UI on new sensor events 
    public void onSensorChanged(SensorEvent sensorEvent) 
    { 
     { 
      /* if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
       // the values you were calculating originally here were over 10000! 
       x = (int) Math.pow(sensorEvent.values[0], 2); 
       y = (int) Math.pow(sensorEvent.values[1], 2); 

      }*/ 

      if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) { 
        Display display = getWindowManager().getDefaultDisplay(); 
        int xmax = display.getWidth(); 
        int ymax = display.getHeight(); 
        x = (int) Math.pow(sensorEvent.values[1], 2); 
        y = (int) Math.pow(sensorEvent.values[2], 2); 
        if (x > xmax) { 
         x = xmax; 
        } else if (x < -xmax) { 
         x = -xmax; 
        } 
        if (y > ymax) { 
         y = ymax; 
        } else if (y < -ymax) { 
         y = -ymax; 
        } 

      } 
     } 
    } 

    // I've chosen to not implement this method 
    public void onAccuracyChanged(Sensor arg0, int arg1) 
    { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     // Register this class as a listener for the accelerometer sensor 
     sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
       SensorManager.SENSOR_DELAY_GAME); 
     // ...and the orientation sensor 
     sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
       SensorManager.SENSOR_DELAY_NORMAL); 
    } 

    @Override 
    protected void onStop() 
    { 
     // Unregister the listener 
     sensorManager.unregisterListener(this); 
     super.onStop(); 
    } 

    public class CustomDrawableView extends View 
    { 


     public CustomDrawableView(Context context) 
     { 
      super(context); 

      Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball); 
      final int dstWidth = 50; 
      final int dstHeight = 50; 
      mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true); 
      mWood = BitmapFactory.decodeResource(getResources(), R.drawable.wood); 

     } 

     protected void onDraw(Canvas canvas) 
     { 

      final Bitmap bitmap = mBitmap; 

      canvas.drawBitmap(mWood, 0, 0, null); 
      canvas.drawBitmap(bitmap, x, y, null); 

      invalidate(); 
     } 
    } 
} 
+0

.. करने के लिए उपयुक्त है जिसे आप [यहां] (http://www.appsrox.com/android/tutorials/accelerometer-golf/) है – user2230793

उत्तर

10

क्या यह आपके अंतिम प्रश्न के समान प्रश्न नहीं है here?! आपको अपना मूल प्रश्न संपादित करना/विस्तार करना चाहिए बल्कि एक नया शुरू करना चाहिए!

लेकिन मूल रूप से इसे यथार्थवादी बनाने के लिए आप स्थिति को बदलने के बजाय x/y गति का उपयोग कर गेंद को स्थानांतरित करना चाहते हैं। तो आप एक लूप रखना चाहते हैं जो गेंद को खींचता है और आप वर्तमान पाश और पिछले के बीच का समय अंतर जानना चाहते हैं, तो आप स्थिति परिवर्तन को काम करने के लिए सरल किनेमेटिक्स समीकरणों का उपयोग कर सकते हैं।

उदाहरण के लिए:

newspeed = oldSpeed + (acceleration * time) 
distance = (original speed*time) + (0.5 * acceleration * time^2). 

आप सेंसर आदानों का उपयोग कहां त्वरण स्थापित करने के लिए है, तो बस गणना की दूरी गेंदों स्थिति में जोड़ें।


संपादित करें - अनुरोध के रूप में कोड।

आपका भाग्यशाली आपको एक ऊब गया गेम प्रोग्रामर मिला! यह किसी भी माध्यम से सही नहीं है बल्कि यह आपके लिए काम कर रहा है। आपको खुद को एक गेम डेवलपमेंट बुक खरीदना चाहिए और इसके लिए ओपन जीएल का उपयोग करना चाहिए क्योंकि यह बहुत बेहतर होगा!

public class test2 extends Activity implements SensorEventListener{ 

CustomDrawableView mCustomDrawableView = null; 
ShapeDrawable mDrawable = new ShapeDrawable(); 
public float xPosition, xAcceleration,xVelocity = 0.0f; 
public float yPosition, yAcceleration,yVelocity = 0.0f; 
public float xmax,ymax; 
private Bitmap mBitmap; 
private Bitmap mWood; 
private SensorManager sensorManager = null; 
public float frameTime = 0.666f; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 

    //Set FullScreen & portrait 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    // Get a reference to a SensorManager 
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
      SensorManager.SENSOR_DELAY_GAME); 

    mCustomDrawableView = new CustomDrawableView(this); 
    setContentView(mCustomDrawableView); 
    // setContentView(R.layout.main); 

    //Calculate Boundry 
    Display display = getWindowManager().getDefaultDisplay(); 
    xmax = (float)display.getWidth() - 50; 
    ymax = (float)display.getHeight() - 50; 
} 

// This method will update the UI on new sensor events 
public void onSensorChanged(SensorEvent sensorEvent) 
{ 
    { 
     if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) { 
      //Set sensor values as acceleration 
      yAcceleration = sensorEvent.values[1]; 
      xAcceleration = sensorEvent.values[2]; 
      updateBall(); 
     } 
    } 
} 

private void updateBall() { 


    //Calculate new speed 
    xVelocity += (xAcceleration * frameTime); 
    yVelocity += (yAcceleration * frameTime); 

    //Calc distance travelled in that time 
    float xS = (xVelocity/2)*frameTime; 
    float yS = (yVelocity/2)*frameTime; 

    //Add to position negative due to sensor 
    //readings being opposite to what we want! 
    xPosition -= xS; 
    yPosition -= yS; 

    if (xPosition > xmax) { 
     xPosition = xmax; 
    } else if (xPosition < 0) { 
     xPosition = 0; 
    } 
    if (yPosition > ymax) { 
     yPosition = ymax; 
    } else if (yPosition < 0) { 
     yPosition = 0; 
    } 
} 

// I've chosen to not implement this method 
public void onAccuracyChanged(Sensor arg0, int arg1) 
{ 
    // TODO Auto-generated method stub 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
      SensorManager.SENSOR_DELAY_GAME); 
} 

@Override 
protected void onStop() 
{ 
    // Unregister the listener 
    sensorManager.unregisterListener(this); 
    super.onStop(); 
} 

public class CustomDrawableView extends View 
{ 
    public CustomDrawableView(Context context) 
    { 
     super(context); 
     Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball); 
     final int dstWidth = 50; 
     final int dstHeight = 50; 
     mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true); 
     mWood = BitmapFactory.decodeResource(getResources(), R.drawable.wood); 

    } 

    protected void onDraw(Canvas canvas) 
    { 
     final Bitmap bitmap = mBitmap; 
     canvas.drawBitmap(mWood, 0, 0, null); 
     canvas.drawBitmap(bitmap, xPosition, yPosition, null); 
     invalidate(); 
    } 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    // TODO Auto-generated method stub 
    super.onConfigurationChanged(newConfig); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
} 
} 
+0

हे, मैं इस साइट पर बहुत नया हूं। मैं पहले के सवाल को बंद करना चाहता था लेकिन कहीं भी विकल्प नहीं देख सका। हो सकता है कि मैं सही जगह पर नहीं देख रहा हूं। मैं जवाब कैसे स्वीकार करूं? –

+0

हाय मुझे जवाब स्वीकार करने का तरीका मिला। मैंने आपके पिछले उत्तर को स्वीकार कर लिया है। धन्यवाद –

+0

हाय केनी, अच्छा लगता है। लेकिन मैं जावा के लिए भी नया हूं। क्या आप अपना कोड बदल सकते हैं और –

2

मैं SENSOR_DELAY निरंतर कायम होगी

SENSOR_DELAY_FASTEST 

को देखें कि अगर यह ठीक करता है। इससे स्टटर समस्या में मदद मिलनी चाहिए। मुझे लगता है कि आपकी सीमा जांच गलत है क्योंकि xmax और ymax मान प्रदर्शित किए जाते हैं .getWidth/getHeight शायद आपको इसे अपने बिटमैप चौड़ाई और ऊंचाई सीमाओं को xmax और ymax मानों में फ़ीड करना चाहिए।

+0

हाय बॉब के लिए देख रहे हैं, इसे बदलने SENSOR_DELAY_FASTEST के लिए नहीं किया था समस्या हल करें, वास्तव में यह आंदोलन को बहुत तेज बना दिया है, मैं सीमा मुद्दे हल नहीं किया गया है –

संबंधित मुद्दे