Image, Boxes and Rotations [Solved]

Hi,

So for faster collision check, i use ( very innovative) Boxes ( well formally rectangles ).

To draw faster with Java2D, i create accelerated images and thus only draw them directly rotated, and i only store them once ( So i don’t have a rotated image for each object)

Now i have two problems, that i can’t manage to solve because i can’t see where i am doing something wrong.


[b]Problem 1[/b]

So to handle next step collision , i use grid collision, when i load a sprite, i make a grid of 4x4px cells, i iterate over the pixels in each cell to determine if this cell is considered as collidable or not. This grid is saved to the image sprite.

The problem is when I access this grid, the grid isn’t rotated, so i rotate the access coordinates of my grid. But it doesn’t seem to work at all.

My code



private final boolean[][] collisionMap;
private static final int GRID_SIZE = 4;
//getWidth() is the actual width of the image not rotated
//getHeight() is the actual height of the image not rotated
public boolean isCollidable (final int x, final int y, final float angle)
    {
        final float[] pt = transformPoint(x, y, angle, getWidth() / 2, getHeight() / 2);
        final int nX = (int) pt[0];
        final int nY = (int) pt[1];
        if (nX < 0 || nX >= getWidth())
            return false;
        if (nY < 0 || nY >= getHeight())
            return false;
        return collisionMap[nX / GRID_SIZE][nY / GRID_SIZE];
    }

//Inspired from AffineTransform
public static float[] transformPoint (final int x, final int y, final float angle, final int rotX, final int rotY)
    {
        final float c = FastTrig.cos(angle);
        final float s = FastTrig.sin(angle);
        final int myX = x - rotX;
        final int myY = y - rotY;
        final float nX = myX * c - s * myY + rotX;
        final float nY = s * myX + c * myY + rotY;
        final float[] arr = { nX, nY };
        return arr;
    }

[b]Problem 2[/b]

Now every time i rotate my object i need to update my bounding box.
Here is what i get :

http://img4.hostingpics.net/pics/638881Capture.png

It’s clearly too big.


// box parameter is used as a buffer
public void updateBoundingBox(final float angle, final Box box) {
        final float dx = box.getLeft();
        final float dy = box.getTop();
        final int nX = getWidth();
        final int nY = getHeight();
        final float[] pt1 = transformPoint(nX, nY, angle, nX / 2, nY / 2);//transformPoint is the same as in Problem 1
        final float[] pt2 = transformPoint(nX, 0, angle, nX / 2, nY / 2);
        final float[] pt3 = transformPoint(0, nY, angle, nX / 2, nY / 2);
        final float[] pt4 = transformPoint(0, 0, angle, nX / 2, nY / 2);
        Box.enclosing(box, pt1, pt2, pt3, pt4);//this method should return the smallest box containing those points
        box.setPosition(0, 0);
        box.translate(dx, dy);
    }

And in case my enclosing method.

public static Box enclosing(final Box box, final float[]... points) {
        box.reset();//set left, top, right, bot to 0
        for(int i = points.length - 1; i >= 0; i--) {
            final float[] point = points[i];
            if (!box.contains(point[0], point[1]))// return whether this box contains the point, trust me this one works
            {
                box.setLeft(FastMath.min(box.left, point[0]));
                box.setRight(FastMath.max(box.right, point[0]));
                box.setTop(FastMath.min(box.top, point[1]));
                box.setBottom(FastMath.max(box.bot, point[1]));
            }
        }
        return box;
    }

Thanks for reading

I solved it all. I was asking for the wrong coordinates when i cheked for collision and i changed my methods for calculating bounds.