Android Accelerometer moving ball?

Is this not the same question as your last question here?! You should just edit/expand your original question rather that starting a new one! Please accept my answer to your previous question since I can see you used my answer in your new code But basically to make it realistic you want to move the ball by using x/y speed rather than just changing the position.

So you want to have a loop which draws the ball and you want to know the time difference between the current loop and the previous, then you can use simple kinematics equations to work out the position change for example: newspeed = oldSpeed + (acceleration * time) distance = (original speed*time) + (0.5 * acceleration * time^2) Where you use sensor inputs to set the acceleration, then just add the calculated distance to the balls position. Hope this helps EDIT - Code as requested Your lucky you found a bored games programmer! Its not perfect by anymeans but its working for you.

You should buy yourself a game development book and have a look at using open GL for this as it would be alot better! 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. Values1; xAcceleration = sensorEvent. Values2; updateBall(); } } } private void updateBall() { //Calculate new speed float xSpeed = xVelocity + (xAcceleration * frameTime); float ySpeed = yVelocity + (yAcceleration * frameTime); //Calc distance travelled in that time float xS = ((xVelocity + xSpeed)/2)*frameTime; float yS = ((yVelocity + ySpeed)/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 ymax) { yPosition = ymax; } else if (yPosition 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); } }.

Is this not the same question as your last question here?! You should just edit/expand your original question rather that starting a new one! Please accept my answer to your previous question since I can see you used my answer in your new code.

But basically to make it realistic you want to move the ball by using x/y speed rather than just changing the position. So you want to have a loop which draws the ball and you want to know the time difference between the current loop and the previous, then you can use simple kinematics equations to work out the position change. For example: newspeed = oldSpeed + (acceleration * time) distance = (original speed*time) + (0.5 * acceleration * time^2).

Where you use sensor inputs to set the acceleration, then just add the calculated distance to the balls position. Hope this helps. EDIT - Code as requested.

Your lucky you found a bored games programmer! Its not perfect by anymeans but its working for you. You should buy yourself a game development book and have a look at using open GL for this as it would be alot better!

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.

Values1; xAcceleration = sensorEvent. Values2; updateBall(); } } } private void updateBall() { //Calculate new speed float xSpeed = xVelocity + (xAcceleration * frameTime); float ySpeed = yVelocity + (yAcceleration * frameTime); //Calc distance travelled in that time float xS = ((xVelocity + xSpeed)/2)*frameTime; float yS = ((yVelocity + ySpeed)/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 ymax) { yPosition = ymax; } else if (yPosition 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); } }.

I am very much new to this site. I wanted close the earlier question but could not see the option anywhere. May be I am not looking at the right place.

How do I accept the answer? – sankara rao bhatta Jun 25 at 18:51 hi I got how to accept the answer. I have accepted your previous answer.

Thanks – sankara rao bhatta Jun 25 at 19:04 hi kenny, sounds good. But I am new to java as well. Can you change my code and put the logic explained by you – sankara rao bhatta Jun 25 at 19:08 There you go, i've added it to the answer - your lucky I was bored and had nothing better to be doing!

Lol - enjoy. – Kenny Jun 25 at 20:42 thanks kenny I will try the code given by you and also explore the open GL. – sankara rao bhatta Jun 257 at 8:52.

I would set SENSOR_DELAY constant to SENSOR_DELAY_FASTEST See if that fixes it. This should help the stutter problem. Also I think your boundary checks are wrong since xmax and ymax values are given display.

GetWidth/getHeight Maybe you should feed it your BitMap width and height boundaries to the xmax and ymax values.

Changing it to SENSOR_DELAY_FASTEST did not solve issue, infact it made the movement too fast also I boundary issue is not solved – sankara rao bhatta Jun 25 at 18:47.

But basically to make it realistic you want to move the ball by using x/y speed rather than just changing the position. So you want to have a loop which draws the ball and you want to know the time difference between the current loop and the previous, then you can use simple kinematics equations to work out the position change. Where you use sensor inputs to set the acceleration, then just add the calculated distance to the balls position.

Hope this helps. EDIT - Code as requested.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions