我想模擬兩個球之間的碰撞,一個由加速度計控制,另一個隨機移動。 如何實現這些碰撞?我設法設定了球和碰撞的條件,但除此之外,我不知道如何表示碰撞向量。算法或提示將不勝感激:)我有兩個球存儲在變量中的x和y速度和位置。我還設法使用了一些基本的(但錯誤的)碰撞機制,隨機運動球反轉了它的x和y速度。但我也有問題,如果球移動得太快,它們會重疊。我該怎麼辦?兩個球之間的碰撞
package perseus.gfx.test;
import everything;
public class Ball extends View {
RectF lol;
Paint paint, lpaint;
Bitmap bitmap;
Canvas canvas;
private float ballx = 150;
private float bally = 140;
private double speedx = 0;
private double speedy = 0; //ignore
private double accx, accy=0;
private float rad = 15;
private float mult = 0.5f;
private float friction = -0.001f;
private double xv, yv, xS, yS;
private long ltm = -1;
int width, height;
int xmax, ymax;
int xmin, ymin;
public Ball(Context context) {
super(context);
lol = new RectF();
paint = new Paint();
paint.setColor(Color.CYAN);
lpaint = new Paint();
lpaint.setColor(Color.GRAY);
}
public double getSpeedX()
{
return this.speedx;
}
public double getSpeedY()
{
return this.speedy;
}
public void setSpeedX(double x)
{
this.speedx = x;
}
public void setSpeedY(double y)
{
this.speedy = y;
}
public void setColor(int c)
{
paint.setColor(c);
}
public float getRad()
{
return this.rad;
}
public void setRad(float rad)
{
this.rad = rad;
}
public void setAX(double accx)
{
this.accx = accx;
}
public void setAY(double accy)
{
this.accy = accy;
}
public float getX()
{
return this.ballx;
}
public void setX(float ballx)
{
this.ballx = ballx;
}
public float getY()
{
return this.bally;
}
public void setY(float bally)
{
this.bally = bally;
}
public void moveBall() {
xv = accx * mult;
yv = accy * mult;
ballx -= xv * mult;
bally -= yv * mult;
ballx +=speedx;
bally +=speedy;
/*ballx +=speedx;
bally +=speedy;*/
// Collision detection
if (ballx + rad > xmax) {
speedx = -speedx;
ballx = xmax-rad;
}
else if (ballx - rad < 0) {
speedx = -speedx;
ballx = rad;
}
if (bally + rad > 2*ymax/3) {
speedy = -speedy;
bally = 2*ymax/3 - rad;
}
else if (bally - rad < 0) {
speedy = -speedy;
bally = rad;
}
try {
Thread.sleep(20);
} catch(InterruptedException e) {}
invalidate();
}
@Override
public void onMeasure(int widthM, int heightM)
{
width = View.MeasureSpec.getSize(widthM);
height = View.MeasureSpec.getSize(heightM);
xmax = width-1;
ymax = height-1;
xmin = 0;
ymin = 0;
setMeasuredDimension(width, height);
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
}
@Override
public void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmap, 0, 0, paint);
lol.set(ballx - rad, bally-rad, ballx + rad, bally+rad);
canvas.drawLine(0, 2*height/3, width, 2*height/3, lpaint);
/*canvas.drawOval(lol, paint);*/
canvas.drawCircle(ballx, bally, rad, paint);
canvas.drawText(xv + " " + yv, 0, height/2, lpaint);
canvas.save();
moveBall();
canvas.restore();
}
}
這是我的球課,我的主要活動是:
package perseus.gfx.test;
import everything;
public class GfxActivity extends Activity implements OnSeekBarChangeListener, OnItemSelectedListener, SensorEventListener {
ViewGroup.LayoutParams vg = new ViewGroup.LayoutParams(-1, -1);
double velx, vely;
double x, y;
float radi;
double finx, finy;
float finrad;
long lastSensorUpdate = -1;
SensorManager sm;
static double ux, uy; //initial ball velocity
SeekBar velo, rad;
Spinner colour;
Ball ball, tester;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ball = new Ball(this);
tester = new Ball(this);
setContentView(R.layout.main);
addContentView(ball, vg);
addContentView(tester, vg);
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
colour = (Spinner)findViewById(R.id.color);
colour.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> aa = ArrayAdapter.createFromResource(this, R.array.colors, android.R.layout.simple_spinner_item);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
colour.setAdapter(aa);
velo = (SeekBar)findViewById(R.id.vel);
velo.setOnSeekBarChangeListener(this);
rad = (SeekBar)findViewById(R.id.rad);
rad.setOnSeekBarChangeListener(this);
ux = ball.getSpeedX();
uy = ball.getSpeedY();
radi = ball.getRad();
tester.setSpeedX(5);
tester.setSpeedY(3);
tester.setX(0);
tester.setY(0);
tester.setColor(Color.DKGRAY);
}
@Override
public void onProgressChanged(SeekBar seeker, int arg1, boolean arg2) {
switch(seeker.getId())
{
case R.id.vel:
x = ball.getSpeedX();
y = ball.getSpeedY();
if(x<0)
finx = -ux-arg1;
else if(x >= 0)
finx = ux+arg1;
if(y<0)
finy = -uy-arg1;
else if(finy>=0)
finy = uy+arg1;
ball.setSpeedX(finx);
ball.setSpeedY(finy);
break;
case R.id.rad:
finrad = radi + arg1;
ball.setRad(finrad);
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
//nothing lol
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
//nothing lol
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
switch(position)
{
case 0:
ball.setColor(Color.CYAN);
break;
case 1:
ball.setColor(Color.RED);
break;
case 2:
ball.setColor(Color.BLUE);
break;
case 3:
ball.setColor(Color.GREEN);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent sensorevent) {
if(sensorevent.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
ball.setAX(sensorevent.values[2]);
ball.setAY(sensorevent.values[1]);
}
if((Math.pow(ball.getX() - tester.getX(), 2) + Math.pow(ball.getY() - tester.getY(), 2)) <= (ball.getRad() + tester.getRad())*(ball.getRad() + tester.getRad()))
{
/*tester.setSpeedX(-1*tester.getSpeedX());
tester.setSpeedY(-1*tester.getSpeedY());*/
}
}
}
球和檢測儀是我的兩個球,球被加速計控制。我知道代碼不是非常高效,但我認爲我會先着手實現碰撞。
嗯,我忘了提及,我想把他們都視爲平等的羣衆(現在)。我已經發布了我的代碼。 – 2012-01-28 13:40:03
如果將質量同等對待將簡化動量守恆方程,但仍然需要確定它們是彈性碰撞還是非彈性碰撞(無論動能是否守恆)。 關於碰撞檢測的其他問題,我注意到的第一件事是在移動球之後進行碰撞檢測。這可能是問題的一部分,因爲當兩個球正要相交時,您可能會將它們靠得更近,因此在您有機會測試碰撞之前它們看起來會重疊。 – ose 2012-01-28 23:15:27
我想要一個彈性碰撞。你建議我把我的碰撞檢測代碼放在哪裏? – 2012-01-29 09:38:06