这是我在这里发布的第一篇文章,如有任何错误请谅解。我正在使用OpenGL ES 2.0和Android 2.3开发一个简单的动作游戏。我目前正在开发的游戏框架基于二维精灵,存在于三维世界中。当然,我的世界实体具有诸如虚拟世界中的位置、旋转值(以float[]矩阵形式)、OpenGL纹理句柄以及Android的Bitmap句柄等信息(我不确定后者是否必要,因为我正在使用OpenGl机器进行栅格化,但暂时它就在那里,方便我使用)。这是简要的背景介绍,现在来说问题所在。
目前我卡在了基于像素的碰撞检测上,因为我不确定需要采样哪个对象(这里指OGL纹理或Android Bitmap)。我的意思是,我已经尝试过采样Android的Bitmap,但完全行不通——与读取位图外部有关的许多运行时崩溃。当然,为了能够从位图中读取像素,我使用了Bitmap.create方法来获取正确旋转的精灵。以下是代码片段:
另一个可能导致问题的问题,甚至可能是主要问题的问题,是我的交集矩形(指两个对象共同的二维空间矩形)由使用OpenGL矩阵Matrix.multiplyMV功能计算得出的两个边界框的部分组成(以下是代码)。这两种Android和OpenGL矩阵计算方法是否不相等?
目前我卡在了基于像素的碰撞检测上,因为我不确定需要采样哪个对象(这里指OGL纹理或Android Bitmap)。我的意思是,我已经尝试过采样Android的Bitmap,但完全行不通——与读取位图外部有关的许多运行时崩溃。当然,为了能够从位图中读取像素,我使用了Bitmap.create方法来获取正确旋转的精灵。以下是代码片段:
android.graphics.Matrix m = new android.graphics.Matrix();
if(o1.angle != 0.0f) {
m.setRotate(o1.angle);
b1 = Bitmap.createBitmap(b1, 0, 0, b1.getWidth(), b1.getHeight(), m, false);
}
另一个可能导致问题的问题,甚至可能是主要问题的问题,是我的交集矩形(指两个对象共同的二维空间矩形)由使用OpenGL矩阵Matrix.multiplyMV功能计算得出的两个边界框的部分组成(以下是代码)。这两种Android和OpenGL矩阵计算方法是否不相等?
Matrix.rotateM(mtxRotate, 0, -angle, 0, 0, 1);
// original bitmap size, equal to sprite size in it's model space,
// as well as in world's space
float[] rect = new float[] {
origRect.left, origRect.top, 0.0f, 1.0f,
origRect.right, origRect.top, 0.0f, 1.0f,
origRect.left, origRect.bottom, 0.0f, 1.0f,
origRect.right, origRect.bottom, 0.0f, 1.0f
};
android.opengl.Matrix.multiplyMV(rect, 0, mtxRotate, 0, rect, 0);
android.opengl.Matrix.multiplyMV(rect, 4, mtxRotate, 0, rect, 4);
android.opengl.Matrix.multiplyMV(rect, 8, mtxRotate, 0, rect, 8);
android.opengl.Matrix.multiplyMV(rect, 12, mtxRotate, 0, rect, 12);
// computation of object's bounding box (it is necessary as object has been
// rotated second ago and now it's bounding rectangle doesn't match it's host
float left = rect[0];
float top = rect[1];
float right = rect[0];
float bottom = rect[1];
for(int i = 4; i < 16; i += 4) {
left = Math.min(left, rect[i]);
top = Math.max(top, rect[i+1]);
right = Math.max(right, rect[i]);
bottom = Math.min(bottom, rect[i+1]);
};