Search code examples
androidcollision-detection

obscure results returned from getHitsRect on certain android devices


I wrote an android game for android 4+. the game works fine on different genymotion emulators and on my device, which is Samsung Galaxy Young GT-6312 os 4.1.2. when i install it on a Galaxy S3 Mini GT-I8190T os 4.1.2 the function getHitsRect returns weird results. every time i call the function from the same view i get different size of rectangle, which makes my game unplayable. I have set all of my views padding to (0,0,0,0) and used wrap_content params for width and height, for no avail. I've tried to check the view hierarchy dump in ddms but i got an error, although i doubt it can show me something i don't already know. I have absolutely no idea what to do, any help would be highly appreciated.

public class CheckShotsHitThread implements Runnable {
    private volatile ArrayList<ShotController> m_Shots;
    private volatile RainbowDashController m_RD;

    public CheckShotsHitThread(GameController[] controllers, RainbowDashController rainbowDashController) {
        m_Shots = new ArrayList<ShotController>();
        for (GameController controller : controllers) {
            if (controller instanceof ShotController) m_Shots.add((ShotController) controller);
        }
        m_RD = rainbowDashController;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while (GameModel.isRunning) {
            m_RD.getView().getHitRect(m_RD.mHitRect);
            for (ShotController shot : m_Shots) {
                if(shot.isOutOfGame()||shot.getModel().isDead()) continue;
                shot.getView().getHitRect(shot.mHitRect);
                if (shot.mHitRect.intersect(m_RD.mHitRect)) kill(shot, m_RD);
            }
            try {
                Thread.sleep(GameModel.ITERATION_PAUSE_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        Log.d("test", "shots thread dead");

    }

    private void kill(ShotController shot, RainbowDashController rd) {
        Log.d("test","shot width: "+shot.getView().getWidth()+" RD width:"+rd.getView().getWidth()+" hit rect: left: "+m_RD.mHitRect.left+", right: "+m_RD.mHitRect.right);
        shot.getModel().setDead(true);
        rd.getModel().setCaptured(true);
    }
}

Solution

  • solved by overriding getHitRect(Rect outRect) method

    @Override
    public void getHitRect(Rect outRect) {
        outRect.top=(int)getY()-getHeight()/2+getPaddingBottom()/2;;
        outRect.bottom=(int)getY()+getHeight()/2-getPaddingBottom()/2;;
        outRect.left=(int)getX();
        outRect.right=(int)getX()+getWidth();
    
    }