Blame |
Last modification |
View Log
| RSS feed
package com.gebauz.bauzoid.math.collisionx;
import com.gebauz.bauzoid.graphics.spritex.Sprite;
import com.gebauz.bauzoid.math.Line2;
import com.gebauz.bauzoid.math.Matrix4;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.Vector4;
public class RectElement
extends BaseShapeElement
{
// Constants========================================================================================
public static final int NUM_LINE_SEGMENTS =
4;
public static final int TOP_LEFT =
0;
public static final int TOP_RIGHT =
1;
public static final int BOTTOM_RIGHT =
2;
public static final int BOTTOM_LEFT =
3;
// Embedded Types===================================================================================
// Fields===========================================================================================
public float x
;
public float y
;
public float w
;
public float h
;
// Methods==========================================================================================
public RectElement
(float initX,
float initY,
float initW,
float initH
)
{
x = initX
; y = initY
; w = initW
; h = initH
;
}
@
Override
public boolean isInside
(float pX,
float pY
)
{
if ((pX
>= x
) && (pX
<=
(x + w
)) &&
(pY
>= y
) && (pY
<=
(y + h
)))
return true;
return false;
}
@
Override
public boolean intersects
(Shape shapeB, Matrix4 transformA2B
)
{
// NOTE: transform is the matrix that transforms points from this shape's space into the space of shapeB
Vector4 points
[] =
new Vector4
[NUM_LINE_SEGMENTS
];
points
[TOP_LEFT
] = transformA2B.
transform(new Vector4
(x, y,
0,
1));
points
[TOP_RIGHT
] = transformA2B.
transform(new Vector4
(x + w, y,
0,
1));
points
[BOTTOM_RIGHT
] = transformA2B.
transform(new Vector4
(x + w, y + h,
0,
1));
points
[BOTTOM_LEFT
] = transformA2B.
transform(new Vector4
(x, y + h,
0,
1));
//Gdx.app.log("BLA", "topleft: " + points[TOP_LEFT].x + ", " + points[TOP_LEFT].y + " --- " + x + ", " + y + ", " + w + ", " + h);
for (int i =
0; i
< NUM_LINE_SEGMENTS
; i++
)
{
int j =
(i +
1) % (NUM_LINE_SEGMENTS-
1);
if (shapeB.
isInside(points
[i
].
x, points
[i
].
y) || shapeB.
isInside(points
[j
].
x, points
[j
].
y))
//if (shape.isInside(points[i].x, points[i].y) != shape.isInside(points[j].x, points[j].y))
{
/*for (int n = 0; n < NUM_LINE_SEGMENTS; n++)
Gdx.app.log("BLA", "[" + n + "] " + points[n].x + ", " + points[n].y + " --- " + x + ", " + y + ", " + w + ", " + h);
Gdx.app.log("BLA", "TRUE");*/
return true;
}
Line2 line =
new Line2
(points
[i
].
x, points
[i
].
y, points
[j
].
x, points
[j
].
y);
if (shapeB.
intersectsLine(line
))
return true;
}
// generate bounding line segments and transform them
/*Vector2 points[] = new Vector2[NUM_LINE_SEGMENTS];
points[TOP_LEFT] = param.transformPoint(x, y);
points[TOP_RIGHT] = param.transformPoint(x + w, y);
points[BOTTOM_RIGHT] = param.transformPoint(x + w, y + h);
points[BOTTOM_LEFT] = param.transformPoint(x, y + h);
for (int i = 0; i < NUM_LINE_SEGMENTS; i++)
{
int j = (i + 1) % (NUM_LINE_SEGMENTS-1);
if (shape.isInside(points[i].x, points[i].y) != shape.isInside(points[j].x, points[j].y))
return true;
}*/
return false;
}
@
Override
public boolean intersects
(Shape shape, Matrix4 transform, Vector2 displaceResult
)
{
return intersects
(shape, transform
);
}
public boolean intersectsLine
(Line2 line
)
{
/** TODO: quicker test by using an algorithm with the rectangle. */
Vector4 points
[] =
new Vector4
[NUM_LINE_SEGMENTS
];
points
[TOP_LEFT
] =
new Vector4
(x, y,
0,
1);
points
[TOP_RIGHT
] =
new Vector4
(x + w, y,
0,
1);
points
[BOTTOM_RIGHT
] =
new Vector4
(x + w, y + h,
0,
1);
points
[BOTTOM_LEFT
] =
new Vector4
(x, y + h,
0,
1);
for (int i =
0; i
< NUM_LINE_SEGMENTS
; i++
)
{
int j =
(i +
1) % (NUM_LINE_SEGMENTS-
1);
Line2 thisLine =
new Line2
(points
[i
].
x, points
[i
].
y, points
[j
].
x, points
[j
].
y);
if (thisLine.
intersectsSegment(line
))
return true;
}
return false;
}
public void debugRender
(Sprite sprite
)
{
}
// Getters/Setters==================================================================================
}