Kollision von zwei Objekten

  • Antworten:1
  • OffenNicht stickyNicht beantwortet
  • Forum-Beiträge: 1

11.04.2015, 14:58:32 via Website

Hallo Zusammen!

Ich habe folgendes Problem:
Bei der App, die ich gerade programmiere, sollen die zwei Objekte, die kollideren, danach kurz stehen bleiben und sich dann weiter bewegen. Nun ist mein Problem, dass ich nicht weiß an welcher Stelle ich die mögliche Kollidierung überprüfen muss.
Ich hoffe, dass mir jemand behilflich sein kann.
Hier einmal mein bisheriges Programm:

package de.example.henne.bewegung;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Surface;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends ActionBarActivity implements SensorEventListener {

private SensorManager sm;
private Sensor accelerometer;
private Display dspl;
private Context context;
public int x_acc,y_acc;
public String string10;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = MainActivity.this;
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);


    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    WindowManager mWindowManger = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
    dspl = mWindowManger.getDefaultDisplay();

    setContentView(new GameView(this));



}

@Override
public void onSensorChanged(SensorEvent event) {
    event.values[0]*=10;
    event.values[1]*=10;
    event.values[2]*=10; //10=speedfactor
    switch (dspl.getRotation()) {
        case Surface.ROTATION_0:
            x_acc = (int) -event.values[0];
            y_acc = (int) event.values[1];
            break;
        case Surface.ROTATION_90:

            x_acc = (int) event.values[1];
            y_acc = (int) event.values[0];
            break;
        case Surface.ROTATION_180:

            x_acc = (int) event.values[0];
            y_acc = (int) -event.values[1];
            break;
        case Surface.ROTATION_270:
            x_acc = (int) -event.values[1];
            y_acc = (int) -event.values[0];
            break;
    }
    onPause();
    onResume();

}

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

}


@Override
protected void onPause() {
    super.onPause();

    sm.unregisterListener(this);
}

@Override
protected void onResume() {
    super.onResume();
    //Sensor Werte werden wieder verarbeitet
    sm.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_GAME);

}

}

Das erste Objekt:

package de.example.henne.bewegung;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Monster {

private GameView gameview;
private Bitmap monsterbitmap;
public Rect destRect,bitmapRect;
private MonsterThread monsterthread;
private int x,y;
private Ball ball;



public Monster(Resources resources, GameView gameView, int new_x, int new_y,Ball ball) {

    gameview = gameView;
    ball = ball;

    monsterbitmap = BitmapFactory.decodeResource(resources, R.drawable.monster);
    bitmapRect = new Rect(0,0,monsterbitmap.getWidth(),monsterbitmap.getHeight());
    destRect = new Rect(0,0,monsterbitmap.getWidth(),monsterbitmap.getHeight());
    monsterthread = new MonsterThread(this,gameview,ball);
    monsterthread.start();


}
public void drawBitmap(Canvas canvas) {
    destRect.right = destRect.left + monsterbitmap.getWidth();
    destRect.bottom = destRect.top + monsterbitmap.getHeight();
    canvas.drawBitmap(monsterbitmap, bitmapRect,destRect,null);
}
public void setY(int value) {
    destRect.top = value;
    destRect.bottom = destRect.top + monsterbitmap.getHeight();
}
public void setX(int value) {
    destRect.left = value;
    destRect.right = destRect.left + monsterbitmap.getWidth();
}

public void addx(int value) {
    if ((destRect.right+value)>gameview.breitespielflaeche) {
        value = gameview.breitespielflaeche - destRect.right;
    }
    if ((destRect.left+value)<0) {
        value = destRect.left*(-1);
    }
    destRect.left = destRect.left+value;
    destRect.right = destRect.left + monsterbitmap.getWidth();
}

public void addy(int value) {//Veränderung hier
    if ((destRect.bottom+value)>gameview.hoehespielflaeche) {
        value = gameview.hoehespielflaeche - destRect.bottom;
    }
    if ((destRect.top+value)<0) {
        value = destRect.top*(-1);
    }
    destRect.top = destRect.top+value;
    destRect.bottom = destRect.top + monsterbitmap.getHeight();
}

}
Thread:

package de.example.henne.bewegung;

import android.content.Intent;

import java.util.concurrent.TimeUnit;

import static android.support.v4.app.ActivityCompat.startActivity;

public class MonsterThread extends Thread {

private Monster monster;
private GameView gameview;
private int factorx,factory;
private int monstermittelx,monstermittely;
private int ballmittelx,ballmittely;
private int differenzpositionenx,differenzpositioneny;
private Ball ball;

public String string10;

public MonsterThread(Monster monster1, GameView gameview1, Ball ball1) {
    monster = monster1;
    gameview = gameview1;
    ball = ball1;

}
@Override
public void run() {
    try {
        monster.setX(gameview.breitespielflaeche - gameview.breitespielflaeche/2);
        monster.setY(gameview.hoehespielflaeche-gameview.hoehespielflaeche/3);
        while(true) {
            ballmittelx = (gameview.ball.destRect.left + gameview.ball.destRect.right) / 2;
            ballmittely = (gameview.ball.destRect.top + gameview.ball.destRect.bottom) / 2;
            monstermittelx = (monster.destRect.left + monster.destRect.right) / 2;
            monstermittely = (monster.destRect.top + monster.destRect.bottom) / 2;
            differenzpositionenx = ballmittelx - monstermittelx;
            differenzpositioneny = ballmittely - monstermittely;
            if (differenzpositioneny == 0) {
                factory = 0;
            } else if (differenzpositioneny > 0) {
                factory = 1;
            } else
                factory = -1;
            if (differenzpositionenx == 0) {
                factorx = 0;
            } else if (differenzpositionenx > 0) {
                factorx = 1;
            } else
                factorx = -1;
            TimeUnit.MILLISECONDS.sleep(10);
            monster.addx(5 * factorx);
            monster.addy(5 * factory);

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    super.run();
}

Objekt 2:
package de.example.henne.bewegung;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
public class Ball {

private Bitmap ballbitmap;
public Rect destRect,bitmapRect;
private GameView gameview;
private BallThread ballthread;


public Ball(Resources resources, GameView gameView1) {
    gameview = gameView1;
    ballbitmap = BitmapFactory.decodeResource(resources,R.drawable.ball);
    bitmapRect = new Rect(0,0,ballbitmap.getWidth(),ballbitmap.getHeight());
    destRect = new Rect(0,0,ballbitmap.getWidth(),ballbitmap.getHeight());
    ballthread = new BallThread(Ball.this,gameview);
    ballthread.start();
}

public void drawBitmap(Canvas canvas) {
    destRect.right = destRect.left + ballbitmap.getWidth();
    destRect.bottom = destRect.top + ballbitmap.getHeight();
    canvas.drawBitmap(ballbitmap, bitmapRect,destRect,null);
}

public void addx(int value) {
    if ((destRect.right+value)>gameview.breitespielflaeche) {
        value = gameview.breitespielflaeche - destRect.right;
    }
    if ((destRect.left+value)<0) {
        value = destRect.left*(-1);
    }
    destRect.left = destRect.left+value;
    destRect.right = destRect.left + ballbitmap.getWidth();
}

public void addy(int value) {
    if ((destRect.bottom+value)>gameview.hoehespielflaeche) {
        value = gameview.hoehespielflaeche - destRect.bottom;
    }
    if ((destRect.top+value)<0) {
        value = destRect.top*(-1);
    }
    destRect.top = destRect.top+value;
    destRect.bottom = destRect.top + ballbitmap.getHeight();
}

}
Thread 2:
package de.example.henne.bewegung;

import java.util.concurrent.TimeUnit;

public class BallThread extends Thread {

private Ball ball;
private GameView gameview;

public BallThread(Ball ball1, GameView gameview1) {
    ball = ball1;
    gameview = gameview1;
}

@Override
public void run() {
    try {
        while(true) {
            TimeUnit.MILLISECONDS.sleep(20);
            try {
                ball.addx(gameview.ma.x_acc);
                ball.addy(gameview.ma.y_acc);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}
Und die GameView:
package de.example.henne.bewegung;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.SurfaceView;

public class GameView extends SurfaceView {

public Ball ball;

public MainActivity ma;
public int hoehespielflaeche, breitespielflaeche;
private Monster monster;
public int zahl;
private int factorx, factory;


public GameView(MainActivity ma1) {
    super(ma1);
    ma = ma1;



    setZOrderOnTop(true);
    getHolder().setFormat(PixelFormat.TRANSPARENT);
    ball = new Ball(getResources(), this);
    monster = new Monster(getResources(), this);
    setWillNotDraw(false);

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    breitespielflaeche = metrics.widthPixels;
    hoehespielflaeche = metrics.heightPixels;
}

@Override
protected void onDraw(Canvas canvas) {
    postInvalidate();
    monster.drawBitmap(canvas);
    ball.drawBitmap(canvas);
    super.onDraw(canvas);
}

}

Antworten

Empfohlene Artikel