, ?! / , !
, , , x/y, . , , , , .
:
newspeed = oldSpeed + (acceleration * time)
distance = (original speed*time) + (0.5 * acceleration * time^2).
, .
EDIT - .
, -! , . , Open GL , !
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;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
mCustomDrawableView = new CustomDrawableView(this);
setContentView(mCustomDrawableView);
Display display = getWindowManager().getDefaultDisplay();
xmax = (float)display.getWidth() - 50;
ymax = (float)display.getHeight() - 50;
}
public void onSensorChanged(SensorEvent sensorEvent)
{
{
if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
yAcceleration = sensorEvent.values[1];
xAcceleration = sensorEvent.values[2];
updateBall();
}
}
}
private void updateBall() {
xVelocity += (xAcceleration * frameTime);
yVelocity += (yAcceleration * frameTime);
float xS = (xVelocity/2)*frameTime;
float yS = (yVelocity/2)*frameTime;
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;
}
}
public void onAccuracyChanged(Sensor arg0, int arg1)
{
}
@Override
protected void onResume()
{
super.onResume();
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop()
{
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) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}