Rev 1209 |
Rev 1260 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package com.gebauz.bauzoid.math.collision;
import java.util.Vector;
import com.gebauz.bauzoid.graphics.sprite.SpriteTransform;
import com.gebauz.bauzoid.math.Vector2;
/** Axis-aligned bounding box. */
public class AABoundingBox
{
// Constants========================================================================================
// Embedded Types===================================================================================
// Fields===========================================================================================
public float left =
0;
public float top =
0;
public float bottom =
0;
public float right =
0;
// Methods==========================================================================================
public AABoundingBox
(float l,
float t,
float r,
float b
)
{
left = l
; top = t
; bottom = b
; right = r
;
}
public AABoundingBox
(Vector<Vector2
> points
)
{
left =
Float.
MAX_VALUE;
top =
Float.
MAX_VALUE;
right = -
Float.
MAX_VALUE;
bottom = -
Float.
MAX_VALUE;
for (int i =
0; i
< points.
size(); i++
)
{
Vector2 v = points.
get(i
);
if (v.
x < left
)
left = v.
x;
if (v.
x > right
)
right = v.
x;
if (v.
y < top
)
top = v.
y;
if (v.
y > bottom
)
bottom = v.
y;
}
}
public AABoundingBox
(Vector2
[] points
)
{
left =
Float.
MAX_VALUE;
top =
Float.
MAX_VALUE;
right = -
Float.
MAX_VALUE;
bottom = -
Float.
MAX_VALUE;
for (int i =
0; i
< points.
length; i++
)
{
Vector2 v = points
[i
];
if (v.
x < left
)
left = v.
x;
if (v.
x > right
)
right = v.
x;
if (v.
y < top
)
top = v.
y;
if (v.
y > bottom
)
bottom = v.
y;
}
}
public boolean isInside
(Vector2 point
)
{
return ((point.
x >= left
) &&
(point.
x <= right
) &&
(point.
y >= top
) &&
(point.
y <= bottom
));
}
public boolean intersetcs
(AABoundingBox other
)
{
return !((other.
left > right
) ||
(other.
right < left
) ||
(other.
top > bottom
) ||
(other.
bottom < top
));
}
public AABoundingBox transform
(SpriteTransform t
)
{
Vector2 points
[] =
new Vector2
[4];
points
[0] = t.
spriteToWorld(left
* t.
w, top
* t.
w);
points
[1] = t.
spriteToWorld(right
* t.
w, top
* t.
w);
points
[2] = t.
spriteToWorld(right
* t.
w, bottom
* t.
w);
points
[3] = t.
spriteToWorld(left
* t.
w, bottom
* t.
w);
return new AABoundingBox
(points
);
}
/** Create a bounding box from a ray. */
public static AABoundingBox fromRay
(Vector2 pos, Vector2 velocity
)
{
Vector2 points
[] =
new Vector2
[4];
points
[0] =
new Vector2
(pos.
x, pos.
y);
points
[1] =
new Vector2
(pos.
x + velocity.
x, pos.
y);
points
[2] =
new Vector2
(pos.
x + velocity.
x, pos.
y + velocity.
y);
points
[3] =
new Vector2
(pos.
x, pos.
y + velocity.
y);
return new AABoundingBox
(points
);
}
// Getters/Setters==================================================================================
}