3

我以 SensorManager 的加速器为例,其中画布(球)根据设备加速器的旋转来更新其位置。这是图像:

在此处输入图像描述

如图所示,有一个球和一条线。球的位置经常更新,而线的位置是静态的。

我想让球在触线时反弹回来。我从 3 天开始就尝试过,但不明白我该怎么做。

这是我的代码:

public class ballsensor extends Activity implements SensorEventListener {

    // sensor-related
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;

    // animated view
    private ShapeView mShapeView;

    // screen size
    private int mWidthScreen;
    private int mHeightScreen;

    // motion parameters
    private final float FACTOR_FRICTION = 0.5f; // imaginary friction on the
                                                // screen
    private final float GRAVITY = 9.8f; // acceleration of gravity
    private float mAx; // acceleration along x axis
    private float mAy; // acceleration along y axis
    private final float mDeltaT = 0.5f; // imaginary time interval between each
                                        // acceleration updates

    // timer
    private Timer mTimer;
    private Handler mHandler;
    private boolean isTimerStarted = false;
    private long mStart;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // set the screen always portait
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // initializing sensors
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        // obtain screen width and height
        Display display = ((WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        mWidthScreen = display.getWidth();
        mHeightScreen = display.getHeight() - 35;

        // initializing the view that renders the ball
        mShapeView = new ShapeView(this);
        mShapeView.setOvalCenter((int) (mWidthScreen * 0.6),
                (int) (mHeightScreen * 0.6));

        setContentView(mShapeView);

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // obtain the three accelerations from sensors
        mAx = event.values[0];
        mAy = event.values[1];

        float mAz = event.values[2];

        // taking into account the frictions
        mAx = Math.signum(mAx) * Math.abs(mAx)
                * (1 - FACTOR_FRICTION * Math.abs(mAz) / GRAVITY);
        mAy = Math.signum(mAy) * Math.abs(mAy)
                * (1 - FACTOR_FRICTION * Math.abs(mAz) / GRAVITY);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // start sensor sensing
        mSensorManager.registerListener(this, mAccelerometer,
                SensorManager.SENSOR_DELAY_NORMAL);

    }

    @Override
    protected void onPause() {
        super.onPause();
        // stop senser sensing
        mSensorManager.unregisterListener(this);
    }

    // the view that renders the ball
    private class ShapeView extends SurfaceView implements
            SurfaceHolder.Callback {

        private final int RADIUS = 30;
        private final float FACTOR_BOUNCEBACK = 0.50f;

        private int mXCenter;
        private int mYCenter;
        private RectF mRectF;
        private final Paint mPaint;
        private ShapeThread mThread;

        private float mVx;
        private float mVy;

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

            getHolder().addCallback(this);
            mThread = new ShapeThread(getHolder(), this);
            setFocusable(true);

            mPaint = new Paint();
            mPaint.setColor(0xFFFFFFFF);
            mPaint.setAlpha(192);
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint.setAntiAlias(true);

            mRectF = new RectF();
        }

        // set the position of the ball
        public boolean setOvalCenter(int x, int y) {
            mXCenter = x;
            mYCenter = y;
            return true;
        }

        // calculate and update the ball's position
        public boolean updateOvalCenter() {
            mVx -= mAx * mDeltaT;
            mVy += mAy * mDeltaT;

            System.out.println("mVx is ::" + mVx);
            System.out.println("mVy is ::" + mVy);

            mXCenter += (int) (mDeltaT * (mVx + 0.6 * mAx * mDeltaT));
            mYCenter += (int) (mDeltaT * (mVy + 0.6 * mAy * mDeltaT));

            if (mXCenter < RADIUS) {
                mXCenter = RADIUS;
                mVx = -mVx * FACTOR_BOUNCEBACK;
            }

            if (mYCenter < RADIUS) {
                mYCenter = RADIUS;
                mVy = -mVy * FACTOR_BOUNCEBACK;
            }
            if (mXCenter > mWidthScreen - RADIUS) {
                mXCenter = mWidthScreen - RADIUS;
                mVx = -mVx * FACTOR_BOUNCEBACK;
            }

            if (mYCenter > mHeightScreen - 2 * RADIUS) {
                mYCenter = mHeightScreen - 2 * RADIUS;
                mVy = -mVy * FACTOR_BOUNCEBACK;
            }

            return true;
        }

        // update the canvas.
        @Override
        protected void onDraw(Canvas canvas) {
            if (mRectF != null) {
                mRectF.set(mXCenter - RADIUS, mYCenter - RADIUS, mXCenter
                        + RADIUS, mYCenter + RADIUS);
                canvas.drawColor(0XFF000000);
                // canvas.drawOval(mRectF, mPaint);

                Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
                        R.drawable.stripe1);

                Bitmap ball = BitmapFactory.decodeResource(getResources(),
                        R.drawable.blackwhiteball);

                canvas.drawBitmap(ball, mXCenter - RADIUS, mYCenter - RADIUS,
                        mPaint);
                canvas.drawBitmap(kangoo, 130, 10, null);

            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            mThread.setRunning(true);
            mThread.start();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            mThread.setRunning(false);
            while (retry) {
                try {
                    mThread.join();
                    retry = false;
                } catch (InterruptedException e) {

                }
            }
        }
    }

    class ShapeThread extends Thread {
        private SurfaceHolder mSurfaceHolder;
        private ShapeView mShapeView;
        private boolean mRun = false;

        public ShapeThread(SurfaceHolder surfaceHolder, ShapeView shapeView) {
            mSurfaceHolder = surfaceHolder;
            mShapeView = shapeView;
        }

        public void setRunning(boolean run) {
            mRun = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return mSurfaceHolder;
        }

        @Override
        public void run() {
            Canvas c;
            while (mRun) {
                mShapeView.updateOvalCenter();
                c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        mShapeView.onDraw(c);
                    }
                } finally {
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }
}
4

3 回答 3

13

与其尝试修复您的代码,不如在设计级别工作,开发一个包含两个组件的软件架构:物理模型和显示。关键是将问题的物理特性与显示分开。与显示分开进行时,对物理场进行建模变得容易得多。同样,显示也变得更容易。有两个单独的包——一个用于物理,一个用于显示。

从一个简单版本的问题开始,其中物理世界只有一个点和一条线。对线外反射的点进行建模。你有一些代码可以做到这一点。只需将其从当前代码中删除即可。确保物理性能符合您的预期,而不必担心显示。

为球设计一个类。球具有速度和位置属性。它有一个 move 方法,可以根据一次点击的速度更新位置。move 方法检查它是否与墙壁发生交互(碰撞),并根据您希望世界拥有的物理特性改变速度。碰撞检测是通过询问墙壁来完成的。物理学可以是入射角等于反射角,或者你可以在球上设置一个旋转属性来改变球的反弹方式。关键是所有物理建模都与显示分开完成。同样,您为墙创建一个类。最初墙壁是固定的,但您可以为其添加运动。好消息是,如果您正确设计了球类,则更改墙壁以使其移动不会' t 影响球类的设计。此外,这些物理变化都不会影响显示的完成方式。

制作一个显示器,简单地将物理转换为屏幕上的演示文稿。

从那里您可以增加模型的复杂性。把这个点画成一个圆圈。重做物理,使其适用于这种新的复杂性。显示不会有太大变化,但将它们分开。

我有我的 CS1 课程做同样问题的版本。两年前,我让他们打乒乓球。去年的蜈蚣版本。在即将到来的这个学期,他们将突破作为一个项目。当他们将物理与显示器分开建模时,他们就可以工作了。当他们不这样做时,通常是一团糟。

于 2012-01-07T06:14:16.233 回答
1

物理模块应在单独的线程中运行,并使用最佳可用时间分辨率进行位置更新。(毫秒应该足够了)这就是我设计游戏循环的方式:

    lastFrameTime = System.currentTimeMillis();

    // as long as we run we move
    while (state == GameState.RUNNING) {
        currentFrame++;
        timeNow = System.currentTimeMillis();

        // sleep until this frame is scheduled
        long l = lastFrameTime + FRAME_DELAY - timeNow;
        updatePositions();
        redraw();       
        if (l > 0L) {
            try {
                Thread.sleep(l);
            }
            catch (Exception exception) {
            }
        } else {
            // something long kept us from updating, reset delays
            lastFrameTime = timeNow;
            l = FRAME_DELAY;
        }

        lastFrameTime = timeNow + l;
        // be polite, let others play
        Thread.yield();
    }

对于将处理事件和配置单元命令到物理引擎的 UI 任务,放弃对线程的控制非常重要。

至于碰撞检测 - 这是非常简单的数学。你的线是垂直的,zou 必须检查线和中心的 x 坐标的差异是否小于半径 - 然后反转速度的 x 分量

于 2012-01-07T16:22:30.073 回答
0

您可以使用 Rect.intersects(Rect, Rect) 来检测碰撞。使用您的位图宽度和高度来设置新的矩形。

这是一个肮脏的例子:

import java.util.Timer;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class ballsensor extends Activity implements SensorEventListener {

// sensor-related
private SensorManager mSensorManager;
private Sensor mAccelerometer;

// animated view
private ShapeView mShapeView;

// screen size
private int mWidthScreen;
private int mHeightScreen;

// motion parameters
private final float FACTOR_FRICTION = 0.5f; // imaginary friction on the
                                            // screen
private final float GRAVITY = 9.8f; // acceleration of gravity
private float mAx; // acceleration along x axis
private float mAy; // acceleration along y axis
private final float mDeltaT = 0.5f; // imaginary time interval between each
                                    // acceleration updates

// timer
private Timer mTimer;
private Handler mHandler;
private final boolean isTimerStarted = false;
private long mStart;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set the screen always portait
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // initializing sensors
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    // obtain screen width and height
    final Display display = ((WindowManager) this
            .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    mWidthScreen = display.getWidth();
    mHeightScreen = display.getHeight() - 35;

    // initializing the view that renders the ball
    mShapeView = new ShapeView(this);
    mShapeView.setOvalCenter((int) (mWidthScreen * 0.6),
            (int) (mHeightScreen * 0.6));

    setContentView(mShapeView);

}

@Override
public void onAccuracyChanged(final Sensor sensor, final int accuracy) {

}

@Override
public void onSensorChanged(final SensorEvent event) {
    // obtain the three accelerations from sensors
    mAx = event.values[0];
    mAy = event.values[1];

    final float mAz = event.values[2];

    // taking into account the frictions
    mAx = Math.signum(mAx) * Math.abs(mAx)
            * (1 - FACTOR_FRICTION * Math.abs(mAz) / GRAVITY);
    mAy = Math.signum(mAy) * Math.abs(mAy)
            * (1 - FACTOR_FRICTION * Math.abs(mAz) / GRAVITY);
}

@Override
protected void onResume() {
    super.onResume();
    // start sensor sensing
    mSensorManager.registerListener(this, mAccelerometer,
            SensorManager.SENSOR_DELAY_NORMAL);

}

@Override
protected void onPause() {
    super.onPause();
    // stop senser sensing
    mSensorManager.unregisterListener(this);
}

// the view that renders the ball
private class ShapeView extends SurfaceView implements
        SurfaceHolder.Callback {

    private final int RADIUS = 30;
    private final float FACTOR_BOUNCEBACK = 0.50f;

    private int mXCenter;
    private int mYCenter;
    private final RectF mRectF;
    private final Paint mPaint;
    private final ShapeThread mThread;

    private float mVx;
    private float mVy;
    private final Rect lineRect = new Rect();
    private final Rect ballRect = new Rect();

    public ShapeView(final Context context) {
        super(context);

        getHolder().addCallback(this);
        mThread = new ShapeThread(getHolder(), this);
        setFocusable(true);

        mPaint = new Paint();
        mPaint.setColor(0xFFFFFFFF);
        mPaint.setAlpha(192);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setAntiAlias(true);

        mRectF = new RectF();
    }

    // set the position of the ball
    public boolean setOvalCenter(final int x, final int y) {
        mXCenter = x;
        mYCenter = y;
        return true;
    }

    // calculate and update the ball's position
    public boolean updateOvalCenter() {
        mVx -= mAx * mDeltaT;
        mVy += mAy * mDeltaT;

        System.out.println("mVx is ::" + mVx);
        System.out.println("mVy is ::" + mVy);

        mXCenter += (int) (mDeltaT * (mVx + 0.6 * mAx * mDeltaT));
        mYCenter += (int) (mDeltaT * (mVy + 0.6 * mAy * mDeltaT));

        if (mXCenter < RADIUS) {
            mXCenter = RADIUS;
            mVx = -mVx * FACTOR_BOUNCEBACK;
        }

        if (mYCenter < RADIUS) {
            mYCenter = RADIUS;
            mVy = -mVy * FACTOR_BOUNCEBACK;
        }
        if (mXCenter > mWidthScreen - RADIUS) {
            mXCenter = mWidthScreen - RADIUS;
            mVx = -mVx * FACTOR_BOUNCEBACK;
        }

        if (mYCenter > mHeightScreen - 2 * RADIUS) {
            mYCenter = mHeightScreen - 2 * RADIUS;
            mVy = -mVy * FACTOR_BOUNCEBACK;
        }

        if(Rect.intersects(lineRect, ballRect)){
            mVx = -mVx * FACTOR_BOUNCEBACK;
            mVy = -mVy * FACTOR_BOUNCEBACK;
             mXCenter += (int) (mDeltaT * (mVx + 0.6 * mAx * mDeltaT)) * 5;
             mYCenter += (int) (mDeltaT * (mVy + 0.6 * mAy * mDeltaT)) * 5;
        }

        return true;
    }

    // update the canvas.
    @Override
    protected void onDraw(final Canvas canvas) {
        if (mRectF != null) {
            mRectF.set(mXCenter - RADIUS, mYCenter - RADIUS, mXCenter
                    + RADIUS, mYCenter + RADIUS);
            canvas.drawColor(0XFF000000);
            // canvas.drawOval(mRectF, mPaint);

            final Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
                    R.drawable.blankcard);
            lineRect.set(130, 10, 130 + kangoo.getWidth(), 10 + kangoo.getHeight());

            final Bitmap ball = BitmapFactory.decodeResource(getResources(),
                    R.drawable.blankcard);
            ballRect.set(mXCenter - RADIUS,  mYCenter - RADIUS, mXCenter - RADIUS + ball.getWidth(),  mYCenter - RADIUS + ball.getHeight());

            canvas.drawBitmap(ball, mXCenter - RADIUS, mYCenter - RADIUS,
                    mPaint);
            canvas.drawBitmap(kangoo, 130, 10, null);

        }
    }

    @Override
    public void surfaceChanged(final SurfaceHolder holder, final int format, final int width,
            final int height) {
    }

    @Override
    public void surfaceCreated(final SurfaceHolder holder) {
        mThread.setRunning(true);
        mThread.start();
    }

    @Override
    public void surfaceDestroyed(final SurfaceHolder holder) {
        boolean retry = true;
        mThread.setRunning(false);
        while (retry) {
            try {
                mThread.join();
                retry = false;
            } catch (final InterruptedException e) {

            }
        }
    }
}

class ShapeThread extends Thread {
    private final SurfaceHolder mSurfaceHolder;
    private final ShapeView mShapeView;
    private boolean mRun = false;

    public ShapeThread(final SurfaceHolder surfaceHolder, final ShapeView shapeView) {
        mSurfaceHolder = surfaceHolder;
        mShapeView = shapeView;
    }

    public void setRunning(final boolean run) {
        mRun = run;
    }

    public SurfaceHolder getSurfaceHolder() {
        return mSurfaceHolder;
    }

    @Override
    public void run() {
        Canvas c;
        while (mRun) {
            mShapeView.updateOvalCenter();
            c = null;
            try {
                c = mSurfaceHolder.lockCanvas(null);
                synchronized (mSurfaceHolder) {
                    mShapeView.onDraw(c);
                }
            } finally {
                if (c != null) {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}
}

需要改进,但可能会让你走上正轨。

于 2012-01-14T01:21:00.957 回答