vbGore Free Online RPG Engine

Revolutionizing Visual Basic ORPG Development
It is currently Sun May 26, 2013 12:07 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Axis Aligned Bounding Box Code
PostPosted: Sat Feb 21, 2009 3:29 am 
Slave to the BB

Joined: Sat Feb 24, 2007 11:17 pm
Posts: 2704
Location: The Aussie Land
Here's my axis-aligned bounding-box code (lots of trial and error :P). I was wondering if there was a less cpu intensive way of doing it, or atleast do it in less code? or if there are any unnecessary statements?

What it does is peek at the matrix on the matrix stack and pulls it into an array. In OpenGL each number in the matrix (16 in total) has a different role in getting the object to render the right way, but the important ones are the rotation ones [0],[1],[4] and [5]. So i make a temporary Rectangle (my own class called "Quad" is the same as a Rect but uses 4 points rather than 2).

(if you want to learn more about matrices you can go here :) http://www.sjbaker.org/steve/omniv/matr ... iends.html)

These values are like sin,cos and tan on the unit circle. They only go from 0 to 1, and stretch/shrink in relation to the rotation. Each one of my calculations calculates the x value of each point on the rectangle, then each y values of the same points, by multiplying it with the width or height respectively.

Then it iterates through all the combinations and gets the min/max x/y values.

I'm bad at geometry/specialist maths so this was the only way i could think of. :( I especially don't like the part where you have to loop through all the combinations. I was wondering if there was an algorithm of some sort.

Any advice would be appreciated :D Thanks!!!

Code:
Code:
   public void calculate(Quad aabb, GL gl) {
        float[] matrix = new float[16];
        gl.glGetFloatv(GL.GL_MODELVIEW_MATRIX, matrix, 0);

        Quad temp = new Quad(matrix[4] * quad.width,matrix[5] * quad.height,matrix[0] * quad.width,matrix[1] * quad.height);

        float xpoint[] = new float[3];
        float ypoint[] = new float[3];

        float xmax = 0;
        float ymax = 0;
        float ymin = 0;
        float xmin = 0;

        xpoint[0]=temp.x;
        xpoint[1]=temp.x2;
        xpoint[2]=temp.x + temp.x2;

        ypoint[0]=temp.y;
        ypoint[1]=temp.y2;
        ypoint[2]=temp.y + temp.y2;

        for (int i = 0; i < 3; i++) {
            if (xpoint[i] > xmax) {
                xmax = xpoint[i];
            }
            if (ypoint[i] > ymax) {
                ymax = ypoint[i];
            }
            if (xpoint[i] < xmin) {
                xmin = xpoint[i];
            }
            if (ypoint[i] < ymin) {
                ymin = ypoint[i];
            }
        }

        aabb.x = xmin;
        aabb.x2 = xmax;
        aabb.y = ymin;
        aabb.y2 = ymax;
    }


Screenshot:
Image


Its for collision detection, maybe there are better methods of collision detection aswell?


Top
 Profile  
 
 Post subject: Re: Axis Aligned Bounding Box Code
PostPosted: Sat Feb 21, 2009 7:33 pm 
Slave to the BB

Joined: Sat Feb 24, 2007 11:17 pm
Posts: 2704
Location: The Aussie Land
Here is the revised version of my AABB code.

It supports bounding an entity which rotates around an origin, via getting the x and y translations from the matrix and backtracking to the origin.

Even though it does more, ive managed to reduce the amount of code by letting OpenGL do more work for me :D
Code:
Code:
    public void calculateAABB(GL gl) {
        gl.glGetFloatv(GL.GL_MODELVIEW_MATRIX, matrix, 0);

        float[] xpoint = new float[3];
        float[] ypoint = new float[3];

        xpoint[0] = (matrix[4] * quad.height) + matrix[12];
        xpoint[1] = (matrix[5] * quad.width) + matrix[12];
        xpoint[2] = (xpoint[0] + xpoint[1]) - matrix[12];

        ypoint[0] = (matrix[0] * quad.height) + matrix[13];
        ypoint[1] = (matrix[1] * quad.width) + matrix[13];
        ypoint[2] = (ypoint[0] + ypoint[1]) - matrix[13];

        float xmax = matrix[12];
        float xmin = matrix[12];
        float ymax = matrix[13];
        float ymin = matrix[13];

        for (int i = 0; i < 3; i++) {
            if (xpoint[i] < xmin) {
                xmin = xpoint[i];
            }
            if (xpoint[i] > xmax) {
                xmax = xpoint[i];
            }
            if (ypoint[i] < ymin) {
                ymin = ypoint[i];
            }
            if (ypoint[i] > ymax) {
                ymax = ypoint[i];
            }
        }

        aabb.x = xmin;
        aabb.y = ymin;
        aabb.x2 = xmax;
        aabb.y2 = ymax;
    }


Although this is the fastest version of collision detection, im still looking for how to do rotated rectangle collision and pixel-perfect collision. For when higher-precision is required.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group