Subversion Repositories AndroidProjects

Rev

Rev 1237 | Rev 1279 | 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));
               
                if (trajectory != null)
                {
                        Vector2 traj = trajectory.getLineVector();
               
                        RenderUtil.drawArrow(graphics, trajectory.ax, trajectory.ay, trajectory.ax + traj.x * 500.0f, trajectory.ay + traj.y * 500.0f, new Vector4(1, 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()
        {
                Vector2[] points = new Vector2[4];
               
                points[0] = new Vector2(x, y);
                points[1] = new Vector2(x + w, y);
                points[2] = new Vector2(x + w, y + h);
                points[3] = new Vector2(x, y + h);
               
                return points;
        }
       
        @Override
        public Vector2[] getPoints()
        {
                Vector2[] points = new Vector2[4];
               
                SpriteTransform t = getOwner().transform;
               
                points[0] = t.spriteToWorld(x * t.w, y * t.h);
                points[1] = t.spriteToWorld((x + w) * t.w, y * t.h);
                points[2] = t.spriteToWorld((x + w) * t.w, (y + h) * t.h);
                points[3] = t.spriteToWorld(x * t.w, (y + h) * t.h);
               
                return points;
        }
       
        @Override
        public Line2[] getUntransformedLineSegments()
        {
                Line2[] lines = 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);
               
                lines[0] = new Line2(topLeft, topRight);
                lines[1] = new Line2(topRight, bottomRight);
                lines[2] = new Line2(bottomRight, bottomLeft);
                lines[3] = new Line2(bottomLeft, topLeft);
               
                return lines;
        }
       
        @Override
        public Line2[] getLineSegments()
        {
                Line2[] lines = 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);
               
                lines[0] = new Line2(topLeft, topRight);
                lines[1] = new Line2(topRight, bottomRight);
                lines[2] = new Line2(bottomRight, bottomLeft);
                lines[3] = new Line2(bottomLeft, topLeft);
               
                return lines;
        }
       
        @Override
        public Line2[] getSweepLineSegments(Vector2 move)
        {
                Line2[] lines = 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);

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

                return lines;
        }
       
        @Override
        public Vector2[] getProjectionAxes()
        {
                Vector2[] axes = 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);

                axes[0] = new Vector2(topRight.x-topLeft.x, topRight.y-topLeft.y);
                axes[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 < axes.length; i++)
                {
                        axes[i].set(axes[i].y, -axes[i].x);
                }
       
                return axes;
        }
       
        // Getters/Setters==================================================================================

}