Subversion Repositories AndroidProjects

Rev

Rev 1240 | Rev 1280 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.math.collision;

import com.gebauz.bauzoid.graphics.Graphics;
import com.gebauz.bauzoid.graphics.RenderUtil;
import com.gebauz.bauzoid.graphics.sprite.SpriteTransform;
import com.gebauz.bauzoid.math.Line2;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.Vector4;
import com.gebauz.bauzoid.math.collision.BaseShapeElement;

public class RectElement extends BaseShapeElement
{
        // Constants========================================================================================

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

        // Fields===========================================================================================
       
        public float x;
        public float y;
        public float w;
        public float h;
       

        // Methods==========================================================================================

        public RectElement(Shape shape, float initX, float initY, float initW, float initH)
        {
                super(shape);
                x = initX; y = initY; w = initW; h = initH;
        }
       
        @Override
        public BaseShapeElement copy(Shape newOwner)
        {
                return new RectElement(newOwner, x, y, w, h);          
        }

        @Override
        public void renderDebug(Graphics graphics, SpriteTransform t)
        {
                RenderUtil.drawQuad(graphics, x * t.w, y * t.h, (x+w) * t.w, (y+h) * t.h, t, new Vector4(1, 0, 0, 1));
               
                Vector2 c = getCenter();               
               
                RenderUtil.drawEllipse(graphics, c.x-5, c.y-5, c.x+5, c.y+5, new Vector4(0, 1, 1, 1));
               
                Line2 lines[] = getLineSegments();
               
                for (int i = 0; i < lines.length; i++)
                {
                        Vector2 midPoint = lines[i].getMidPoint();
                        Vector2 lineVec = lines[i].getLineVector();
                        Vector2 normal = new Vector2(lineVec.y, -lineVec.x);
                        normal.normalize();
                       
                        final float d = 20;
                       
                        RenderUtil.drawArrow(graphics, midPoint.x, midPoint.y, midPoint.x + normal.x * d, midPoint.y + normal.y * d, new Vector4(0, 0, 1, 1));
                }
        }
       
        @Override
        public Vector2[] getUntransformedPoints()
        {
                if (mUntransformedPoints == null)
                        mUntransformedPoints = new Vector2[4];
               
                mUntransformedPoints[0] = new Vector2(x, y);
                mUntransformedPoints[1] = new Vector2(x + w, y);
                mUntransformedPoints[2] = new Vector2(x + w, y + h);
                mUntransformedPoints[3] = new Vector2(x, y + h);
               
                return mUntransformedPoints;
        }
       
        @Override
        public Vector2[] getPoints()
        {
                if (mPoints == null)
                        mPoints = new Vector2[4];
               
                SpriteTransform t = getOwner().transform;
               
                mPoints[0] = t.spriteToWorld(x * t.w, y * t.h);
                mPoints[1] = t.spriteToWorld((x + w) * t.w, y * t.h);
                mPoints[2] = t.spriteToWorld((x + w) * t.w, (y + h) * t.h);
                mPoints[3] = t.spriteToWorld(x * t.w, (y + h) * t.h);
               
                return mPoints;
        }
       
        @Override
        public Line2[] getUntransformedLineSegments()
        {
                if (mUntransformedLines == null)
                        mUntransformedLines = new Line2[4];
               
                SpriteTransform t = getOwner().transform;

                Vector2 topLeft = new Vector2(x * t.w, y * t.h);
                Vector2 topRight = new Vector2((x+w) * t.w, y * t.h);
                Vector2 bottomRight = new Vector2((x+w) * t.w, (y+h) * t.h);
                Vector2 bottomLeft = new Vector2(x * t.w, (y+h) * t.h);
               
                mUntransformedLines[0] = new Line2(topLeft, topRight);
                mUntransformedLines[1] = new Line2(topRight, bottomRight);
                mUntransformedLines[2] = new Line2(bottomRight, bottomLeft);
                mUntransformedLines[3] = new Line2(bottomLeft, topLeft);
               
                return mUntransformedLines;
        }
       
        @Override
        public Line2[] getLineSegments()
        {
                if (mLines == null)
                        mLines = new Line2[4];
               
                SpriteTransform t = getOwner().transform;
               
                Vector2 topLeft = t.spriteToWorld(x * t.w, y * t.h);
                Vector2 topRight = t.spriteToWorld((x+w) * t.w, y * t.h);
                Vector2 bottomRight = t.spriteToWorld((x+w) * t.w, (y+h) * t.h);
                Vector2 bottomLeft = t.spriteToWorld(x * t.w, (y+h) * t.h);
               
                mLines[0] = new Line2(topLeft, topRight);
                mLines[1] = new Line2(topRight, bottomRight);
                mLines[2] = new Line2(bottomRight, bottomLeft);
                mLines[3] = new Line2(bottomLeft, topLeft);
               
                return mLines;
        }
       
        @Override
        public Line2[] getSweepLineSegments(Vector2 move)
        {
                if (mSweepLines == null)
                        mSweepLines = new Line2[8];
               
                SpriteTransform t = getOwner().transform;
               
                Vector2 topLeft = t.spriteToWorld(x * t.w, y * t.h);
                Vector2 topRight = t.spriteToWorld((x+w) * t.w, y * t.h);
                Vector2 bottomRight = t.spriteToWorld((x+w) * t.w, (y+h) * t.h);
                Vector2 bottomLeft = t.spriteToWorld(x * t.w, (y+h) * t.h);

                mSweepLines[0] = new Line2(topLeft, topRight);
                mSweepLines[1] = new Line2(topRight, bottomRight);
                mSweepLines[2] = new Line2(bottomRight, bottomLeft);
                mSweepLines[3] = new Line2(bottomLeft, topLeft);
               
                topLeft.addVector(move);
                topRight.addVector(move);
                bottomRight.addVector(move);
                bottomLeft.addVector(move);
               
                mSweepLines[4] = new Line2(topLeft, topRight);
                mSweepLines[5] = new Line2(topRight, bottomRight);
                mSweepLines[6] = new Line2(bottomRight, bottomLeft);
                mSweepLines[7] = new Line2(bottomLeft, topLeft);

                return mSweepLines;
        }
       
        @Override
        public Vector2[] getProjectionAxes()
        {
                if (mProjectionAxes == null)
                        mProjectionAxes = new Vector2[2];
               
                // only two axes suffice
                SpriteTransform t = getOwner().transform;
               
                Vector2 topLeft = t.spriteToWorld(x * t.w, y * t.h);
                Vector2 topRight = t.spriteToWorld((x+w) * t.w, y * t.h);
                Vector2 bottomRight = t.spriteToWorld((x+w) * t.w, (y+h) * t.h);
                //Vector2 bottomLeft = t.spriteToWorld(x * t.w, (y+h) * t.h);

                mProjectionAxes[0] = new Vector2(topRight.x-topLeft.x, topRight.y-topLeft.y);
                mProjectionAxes[1] = new Vector2(bottomRight.x-topRight.x, bottomRight.y-topRight.y);
                /*axes[2] = axes[0].getInverse();
                axes[3] = axes[1].getInverse();*/

               
                for (int i = 0; i < mProjectionAxes.length; i++)
                {
                        mProjectionAxes[i].set(mProjectionAxes[i].y, -mProjectionAxes[i].x);
                }
       
                return mProjectionAxes;
        }
       
        // Getters/Setters==================================================================================

}