Android中的碰撞检测 [英] Collision Detection in Android

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

问题描述



我想尝试在android中实现像素完美碰撞检测.我来了
关于此像素完美检测的一些文章,但无法实际实施.实际上,我们正在做的是:将2位图的像素放入数组中.然后尝试获取重叠区域中的像素.但是对如何获取该重叠部分的像素感到困惑.谁能在这方面提供一些帮助.

我有2个以矩形为边界的图像对象.这两个图像对象已设置动画.我要更改
这两个图像对象发生碰撞时处于某种状态.这是代码正在尝试的操作.

Hi,

Am to trying to implement pixel perfect collision detection in android. I have came
across some articles about this pixel perfect detection but am unable to implement them practically. Actually what am doing is: taking 2 bitmap''s pixels into arrays. And then trying to take pixels in overlap region. But am confused about how to get pixels of that overLap section. Can any one provide some help on this.

Am having 2 image objects which are bounded by rectangles. Those two image objects are animated.I want to change
some state when those two image objects are collided. This is the code am trying.

public static boolean detectCollision(Ball ball,Sprite bat,int currentFrame)
    {
    	try
    	{
    	int left1,left2,top1,top2,right1,right2,bottom1,bottom2;
    	
    	left1=ball.ballLeft;
        top1=ball.ballTop;
        right1=ball.ballRight;
        bottom1=ball.ballBottom;
        left2=bat.batLeft;
        top2=bat.batTop;
        right2=bat.batRight;
        bottom2=bat.batBottom;
        
        if(bottom1<top2)>
        	return false;
        if(right1 < left2)
        	return false;
        if (top1 > bottom2)
            return false;
        if (left1 > right2)
            return false;
        
        int overLap_left,overLap_top,overLap_right,overLap_bottom;
        
        overLap_left=Math.max(left1,left2);
        overLap_top= Math.min(top1, top2);
        overLap_right=Math.max(right1, right2);
        overLap_bottom=Math.min(bottom1, bottom2);
        
        if (overLap_right <= overLap_left || overLap_bottom <= overLap_right) {
    		return false;
    	}

        
        int width,height;
        //Width and Height of the overlapping Section
        width=overLap_right-overLap_left;
        height=overLap_bottom-overLap_top;
        
        
        
        //Copying bitmap pixels to arrays
        
        Bitmap bmp1=((BitmapDrawable) ResourceCollection.ballCollection.get(currentFrame).getCurrent()).getBitmap();
        Bitmap batImage=((BitmapDrawable) ResourceCollection.batCollection.get(0).getCurrent()).getBitmap();
        
        int[] pixels1 = new int[bmp1.getWidth()*bmp1.getHeight()];
        int[] pixels2 = new int[batImage.getWidth()*batImage.getHeight()];
     
        bmp1.getPixels(pixels1, 0, bmp1.getWidth(), 0,0, bmp1.getWidth()-1, bmp1.getHeight()-1);
        batImage.getPixels(pixels2, 0, batImage.getWidth(),0,0, batImage.getWidth()-1, batImage.getHeight()-1);

        
        	for(int X=overLap_left;X<overlap_right;x++)>
        	{
        		for(int Y=overLap_top;Y<overlap_bottom;y++)>
                        {
        		        		
            	       int x;
                       if (X >XandY.X)
                          x = X - XandY.X;
                       else
                          x =  XandY.X - X;

                      int y;
                        if (Y > XandY.Y)
                           y = Y - (int) XandY.Y;
                        else
                           y =  XandY.Y - Y;
        		
                if(x>=bmp1.getWidth() || y>=bmp1.getHeight())
                	return false;
                if(x>=batImage.getWidth() || y>=batImage.getHeight())
                	return false;
                
                
                int a=bmp1.getPixel(x, y);
                int b=batImage.getPixel(x, y); //But these statements are not working as expected (showing collision 
//even if the two images are not collided(not touched each other) visually) 
                
                                
                if (a != 0 && b != 0) // If both colors are not transparent (the alpha channel is not 0), then there is a collision
                {
                    return true;
                }

        		
        	}
        }
        
return false;
    	}
    	catch(Exception ee)
    	{
    		String msg=ee.getMessage();
    		Log.v("Error", ee.getMessage());
    		return false;
    	}









How can i check that the two images are touched each other in the Intersection rectangle(Overlapping region).?

推荐答案

我会尽量简短,但如果您还有其他问题可以张贴;)

首先,在更改子画面动画索引之前,我通常会进行数学计算,以计算帧之间是否会发生碰撞.这比加载/旋转/更改精灵更快,而且如果您关心帧速率,则不必展开这些更改.

完成此步骤后,第一件事就是使用createIntersection [
I''ll try to be brief, but if you have more questions you can post them ;)

First, before I alter sprite animation indexes, I would usually do the math to compute if a collision would have occurred between frames. This is way faster than loading/rotating/altering sprites and you don''t want to have to unroll those changes if you care about framerate.

Once you''ve got this in place, the first thing to do is to get the intersecting region using createIntersection[^]. This will give you another Rectangle2D with the overlap area.

From there, it''s the comparison. It looks like you''ve got a start on that, so the only thing left to do is simplify how to capture that intersection.

During the comparison, you''ll walk over the area contained within the intersection only. Pass the intersection rect into the getpixels method to see the pixels you care about.

Cheers.


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

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