Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1051 chris 1
package com.gebauz.bauzoid.math.collisionx;
2
 
3
import com.gebauz.bauzoid.graphics.spritex.Sprite;
4
import com.gebauz.bauzoid.math.Line2;
5
import com.gebauz.bauzoid.math.Matrix4;
6
import com.gebauz.bauzoid.math.Vector2;
7
import com.gebauz.bauzoid.math.Vector4;
8
 
9
public class RectElement extends BaseShapeElement
10
{
11
 
12
        // Constants========================================================================================
13
 
14
        public static final int NUM_LINE_SEGMENTS = 4;
15
 
16
        public static final int TOP_LEFT = 0;
17
        public static final int TOP_RIGHT = 1;
18
        public static final int BOTTOM_RIGHT = 2;
19
        public static final int BOTTOM_LEFT = 3;
20
 
21
 
22
        // Embedded Types===================================================================================
23
 
24
        // Fields===========================================================================================
25
 
26
        public float x;
27
        public float y;
28
        public float w;
29
        public float h;
30
 
31
        // Methods==========================================================================================
32
 
33
        public RectElement(float initX, float initY, float initW, float initH)
34
        {
35
                x = initX; y = initY; w = initW; h = initH;
36
        }
37
 
38
        @Override
39
        public boolean isInside(float pX, float pY)
40
        {
41
                if ((pX >= x) && (pX <= (x + w)) &&
42
                        (pY >= y) && (pY <= (y + h)))
43
                        return true;
44
 
45
                return false;
46
        }
47
 
48
        @Override
49
        public boolean intersects(Shape shapeB, Matrix4 transformA2B)
50
        {
51
                // NOTE: transform is the matrix that transforms points from this shape's space into the space of shapeB 
52
 
53
                Vector4 points[] = new Vector4[NUM_LINE_SEGMENTS];
54
 
55
                points[TOP_LEFT] = transformA2B.transform(new Vector4(x, y, 0, 1));
56
                points[TOP_RIGHT] = transformA2B.transform(new Vector4(x + w, y, 0, 1));
57
                points[BOTTOM_RIGHT] = transformA2B.transform(new Vector4(x + w, y + h, 0, 1));
58
                points[BOTTOM_LEFT] = transformA2B.transform(new Vector4(x, y + h, 0, 1));
59
 
60
                //Gdx.app.log("BLA", "topleft: " + points[TOP_LEFT].x + ", " + points[TOP_LEFT].y + " --- " + x + ", " + y + ", " + w + ", " + h);
61
 
62
                for (int i = 0; i < NUM_LINE_SEGMENTS; i++)
63
                {
64
                        int j = (i + 1) % (NUM_LINE_SEGMENTS-1);
65
 
66
                        if (shapeB.isInside(points[i].x, points[i].y) || shapeB.isInside(points[j].x, points[j].y))
67
                        //if (shape.isInside(points[i].x, points[i].y) != shape.isInside(points[j].x, points[j].y))
68
                        {
69
                                /*for (int n = 0; n < NUM_LINE_SEGMENTS; n++)                          
70
                                        Gdx.app.log("BLA", "[" + n + "] " + points[n].x + ", " + points[n].y + " --- " + x + ", " + y + ", " + w + ", " + h);
71
 
72
                                Gdx.app.log("BLA", "TRUE");*/
73
                                return true;
74
                        }
75
 
76
                        Line2 line = new Line2(points[i].x, points[i].y, points[j].x, points[j].y);
77
                        if (shapeB.intersectsLine(line))
78
                                return true;
79
                }
80
 
81
                // generate bounding line segments and transform them
82
                /*Vector2 points[] = new Vector2[NUM_LINE_SEGMENTS];
83
                points[TOP_LEFT] = param.transformPoint(x, y);
84
                points[TOP_RIGHT] = param.transformPoint(x + w, y);
85
                points[BOTTOM_RIGHT] = param.transformPoint(x + w, y + h);
86
                points[BOTTOM_LEFT] = param.transformPoint(x, y + h);
87
 
88
                for (int i = 0; i < NUM_LINE_SEGMENTS; i++)
89
                {
90
                        int j = (i + 1) % (NUM_LINE_SEGMENTS-1);
91
 
92
                        if (shape.isInside(points[i].x, points[i].y) != shape.isInside(points[j].x, points[j].y))
93
                                return true;
94
                }*/
95
 
96
                return false;
97
        }
98
 
99
        @Override
100
        public boolean intersects(Shape shape, Matrix4 transform, Vector2 displaceResult)
101
        {
102
                return intersects(shape, transform);
103
        }
104
 
105
        public boolean intersectsLine(Line2 line)
106
        {
107
                /** TODO: quicker test by using an algorithm with the rectangle. */
108
 
109
                Vector4 points[] = new Vector4[NUM_LINE_SEGMENTS];
110
 
111
                points[TOP_LEFT] = new Vector4(x, y, 0, 1);
112
                points[TOP_RIGHT] = new Vector4(x + w, y, 0, 1);
113
                points[BOTTOM_RIGHT] = new Vector4(x + w, y + h, 0, 1);
114
                points[BOTTOM_LEFT] = new Vector4(x, y + h, 0, 1);
115
 
116
                for (int i = 0; i < NUM_LINE_SEGMENTS; i++)
117
                {
118
                        int j = (i + 1) % (NUM_LINE_SEGMENTS-1);
119
 
120
                        Line2 thisLine = new Line2(points[i].x, points[i].y, points[j].x, points[j].y);
121
                        if (thisLine.intersectsSegment(line))
122
                                return true;
123
                }
124
 
125
                return false;
126
        }
127
 
128
        public void debugRender(Sprite sprite)
129
        {
130
 
131
        }
132
 
133
        // Getters/Setters==================================================================================
134
 
135
}