Android:画布中对象的碰撞检测 [英] Android: Collision detection of objects in a canvas

查看:75
本文介绍了Android:画布中对象的碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个游戏。在那个应用程序中我必须检测画布中两个对象的碰撞。我正在创建5个机器人对象。其中四个在画布上不断移动,一个由用户控制。



我想检测那些移动物体与其他物体碰撞的事件。我已经阅读了很多教程,但还没有找到解决方案。



代码如下:



I am doing a game.in that app I have to detect the collition of two objects in a canvas. I am creating 5 droid objects. Four of them are continuously moving on the canvas and one is controlled by the user.

I want to detect the event in which those moving objects colliding with other object. I have read many tutorials but did not yet find a solution.

Code is as follows:

public class Droid {   
     private Bitmap bitmap;  
     private int x;         
     private int y; 
     private boolean touched;   
     private Speed speed;     

     public Droid(Bitmap bitmap, int x, int y) {
		this.bitmap = bitmap;
		this.x = x;
		this.y = y;
		this.speed = new Speed();
	}
	
	public Bitmap getBitmap() {
		return bitmap;
	}
	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public boolean isTouched() {
		return touched;
	}
	public void setTouched(boolean touched) {
		this.touched = touched;
	}
	
	public Speed getSpeed() {
		return speed;
	}
	public void setSpeed(Speed speed) {
		this.speed = speed;
	}
	
	public void draw(Canvas canvas) {
		canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), null);
	}
	/**
	 * Method which updates the droid's internal state every tick
	 */
	public void update() {
		if (!touched) {
			x += (speed.getXv() * speed.getxDirection()); 
			y += (speed.getYv() * speed.getyDirection());
		}
	}
  public void handleActionDown(int eventX, int eventY) {
		if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <=(x+bitmap.getWidth()/2))) {
			if (eventY >= (y - bitmap.getHeight() / 2) && (y <= (y + bitmap.getHeight() / 2))) {
				// droid touched
				setTouched(true);
			} else {
				setTouched(false);
			}
		} else {
			setTouched(false);
		}
	}
}

推荐答案

你看过Androng,一个Android的Pong克隆 [ ^ ]在这里CP?



/ Darren
Have you taken a look at Androng, a Pong clone for Android[^] here on CP ?

/Darren


这篇关于Android:画布中对象的碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆