Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.math.collision;

import java.util.Vector;

import com.gebauz.bauzoid.graphics.sprite.SpriteTransform;
import com.gebauz.bauzoid.math.Vector2;

/** Axis-aligned bounding box. */
public class AABoundingBox
{

        // Constants========================================================================================

        // Embedded Types===================================================================================

        // Fields===========================================================================================
       
        public Vector2 min = new Vector2();
        public Vector2 max = new Vector2();

        private Vector2 mPointsCache[] = null;
       
        // Methods==========================================================================================
       
        public AABoundingBox()
        {
        }
       
        public AABoundingBox(float left, float top, float right, float bottom)
        {
                min.x = min.x; min.y = min.y; max.x = max.x; max.y = max.y;
        }
       
        public AABoundingBox(Vector2 minimum, Vector2 maximum)
        {
                min.x = minimum.x;
                min.y = minimum.y;
                max.x = maximum.x;
                max.y = maximum.y;             
        }
       
        public AABoundingBox(Vector<Vector2> points)
        {
                Vector2[] ps = new Vector2[points.size()];
                for (int i = 0; i < points.size(); i++)
                        ps[i] = points.get(i);
                setFromPoints(ps);
        }
       
        public AABoundingBox(Vector2[] points)
        {
                setFromPoints(points);
        }
       
        public void setFromPoints(Vector2[] points)
        {
                min.x = Float.MAX_VALUE;
                min.y = Float.MAX_VALUE;
                max.x = -Float.MAX_VALUE;
                max.y = -Float.MAX_VALUE;
               
                for (int i = 0; i < points.length; i++)
                {
                        Vector2 v = points[i];
                       
                        if (v.x < min.x)
                                min.x = v.x;
                        if (v.x > max.x)
                                max.x = v.x;
                        if (v.y < min.y)
                                min.y = v.y;
                        if (v.y > max.y)
                                max.y = v.y;
                }
        }
       
        public boolean isInside(Vector2 point)
        {
                return isInside(point.x, point.y);
        }
       
        public boolean isInside(float x, float y)
        {
                return ((x >= min.x) &&
                                (x <= max.x) &&
                                (y >= min.y) &&
                                (y <= max.y));
        }
       
        public boolean intersetcs(AABoundingBox other)
        {
                return !((other.min.x > max.x) ||
                                 (other.max.x < min.x) ||
                                 (other.min.y > max.y) ||
                                 (other.max.y < min.y));
        }
       
        public void transform(SpriteTransform t, AABoundingBox result)
        {
                if (mPointsCache == null)
                {
                        mPointsCache = new Vector2[4];
                        for (int i = 0; i < 4; i++)
                        {
                                mPointsCache[i] = new Vector2();
                        }
                }
               
                t.spriteToWorld(min.x * t.w, min.y * t.h, mPointsCache[0]);
                t.spriteToWorld(min.x * t.w, max.y * t.h, mPointsCache[1]);
                t.spriteToWorld(max.x * t.w, max.y * t.h, mPointsCache[2]);
                t.spriteToWorld(max.x * t.w, min.y * t.h, mPointsCache[3]);
               
                result.setFromPoints(mPointsCache);
        }
       
        public AABoundingBox createFromTransformed(SpriteTransform t)
        {
                AABoundingBox result = new AABoundingBox();
                transform(t, result);
                return result;
        }
       
        /** Create a bounding box from a ray. */
        public static AABoundingBox fromRay(Vector2 pos, Vector2 dir)
        {
                /*Vector2 points[] = new Vector2[4];

                points[0] = new Vector2(pos.x, pos.y);
                points[1] = new Vector2(pos.x + dir.x, pos.y);
                points[2] = new Vector2(pos.x + dir.x, pos.y + dir.y);
                points[3] = new Vector2(pos.x, pos.y + dir.y);
               
                return new AABoundingBox(points);*/

               
                AABoundingBox result = new AABoundingBox();
                result.setFromRay(pos, dir);
                return result;
        }
       
        /** Set the values according to ray. */
        public void setFromRay(Vector2 pos, Vector2 dir)
        {
                min.x = Math.min(pos.x, pos.x + dir.x);
                min.y = Math.min(pos.y, pos.x + dir.y);
                max.x = Math.max(pos.x, pos.x + dir.x);
                max.y = Math.max(pos.y, pos.x + dir.y);
        }
       
        // Getters/Setters==================================================================================

}