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
;
private Vector2 mTempA =
new Vector2
();
private Vector2 mTempB =
new Vector2
();
// Methods==========================================================================================
public RectElement
(Shape shape,
float initX,
float initY,
float initW,
float initH,
int startFrame,
int endFrame
)
{
super(shape, startFrame, endFrame
);
x = initX
; y = initY
; w = initW
; h = initH
;
}
public RectElement
(Shape shape,
float initX,
float initY,
float initW,
float initH
)
{
this(shape, initX, initY, initW, initH, -
1, -
1);
}
@
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 boolean isInside
(float px,
float py
)
{
SpriteTransform t = getOwner
().
transform;
return ((px/t.
w >= x
) && (py/t.
h >= y
) && (px/t.
w <=
(x+w
) && (py/t.
h <=
(y+h
))));
}
@
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];
for (int i =
0; i
< mPoints.
length; i++
)
mPoints
[i
] =
new Vector2
();
}
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);*/
t.
spriteToWorld(x
* t.
w, y
* t.
h, mPoints
[0]);
t.
spriteToWorld((x + w
) * t.
w, y
* t.
h, mPoints
[1]);
t.
spriteToWorld((x + w
) * t.
w,
(y + h
) * t.
h, mPoints
[2]);
t.
spriteToWorld(x
* t.
w,
(y + h
) * t.
h, mPoints
[3]);
return mPoints
;
}
@
Override
public Line2
[] getUntransformedLineSegments
()
{
if (mUntransformedLines ==
null)
{
mUntransformedLines =
new Line2
[4];
for (int i =
0; i
< mUntransformedLines.
length; i++
)
mUntransformedLines
[i
] =
new Line2
();
}
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);*/
float leftX = x
* t.
w;
float topY = y
* t.
h;
float rightX =
(x+w
) * t.
w;
float bottomY =
(y+h
) * t.
h;
mUntransformedLines
[0].
set(leftX, topY, rightX, topY
);
mUntransformedLines
[1].
set(rightX, topY, rightX, bottomY
);
mUntransformedLines
[2].
set(rightX, bottomY, leftX, bottomY
);
mUntransformedLines
[3].
set(leftX, bottomY, leftX, topY
);
/*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];
for (int i =
0; i
< mLines.
length; i++
)
mLines
[i
] =
new Line2
();
}
SpriteTransform t = getOwner
().
transform;
t.
spriteToWorld(x
* t.
w, y
* t.
h, mTempA
);
t.
spriteToWorld((x+w
) * t.
w, y
* t.
h, mTempB
);
mLines
[0].
set(mTempA, mTempB
);
t.
spriteToWorld((x+w
) * t.
w,
(y+h
) * t.
h, mTempA
);
mLines
[1].
set(mTempB, mTempA
);
t.
spriteToWorld(x
* t.
w,
(y+h
) * t.
h, mTempB
);
mLines
[2].
set(mTempA, mTempB
);
t.
spriteToWorld(x
* t.
w, y
* t.
h, mTempA
);
mLines
[3].
set(mTempB, mTempA
);
return mLines
;
/*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];
for (int i =
0; i
< mProjectionAxes.
length; i++
)
mProjectionAxes
[i
] =
new Vector2
();
}
// only two axes suffice
SpriteTransform t = getOwner
().
transform;
t.
spriteToWorld(x
* t.
w, y
* t.
h, mTempA
);
t.
spriteToWorld((x+w
) * t.
w, y
* t.
h, mTempB
);
mProjectionAxes
[0].
set(mTempB.
x - mTempA.
x, mTempB.
y - mTempA.
y);
t.
spriteToWorld((x+w
) * t.
w,
(y+h
) * t.
h, mTempA
);
mProjectionAxes
[1].
set(mTempA.
x - mTempB.
x, mTempA.
y - mTempB.
y);
/*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);
mProjectionAxes[0] = new Vector2(topRight.x-topLeft.x, topRight.y-topLeft.y);
mProjectionAxes[1] = new Vector2(bottomRight.x-topRight.x, bottomRight.y-topRight.y);*/
for (int i =
0; i
< mProjectionAxes.
length; i++
)
{
mProjectionAxes
[i
].
set(mProjectionAxes
[i
].
y, -mProjectionAxes
[i
].
x);
}
return mProjectionAxes
;
}
// Getters/Setters==================================================================================
}