use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.
the class ChainShape method createLoop.
/**
* Create a loop. This automatically adjusts connectivity.
*
* @param vertices an array of vertices, these are copied
* @param count the vertex count
*/
public void createLoop(final Vec2[] vertices, int count) {
assert m_vertices == null && m_count == 0;
assert count >= 3;
m_count = count + 1;
m_vertices = new Vec2[m_count];
for (int i = 1; i < count; i++) {
Vec2 v1 = vertices[i - 1];
Vec2 v2 = vertices[i];
// If the code crashes here, it means your vertices are too close together.
if (v1.distanceSquared(v2) < JBoxSettings.linearSlop * JBoxSettings.linearSlop) {
throw new RuntimeException("Vertices of chain shape are too close together");
}
}
for (int i = 0; i < count; i++) {
m_vertices[i] = new Vec2(vertices[i]);
}
m_vertices[count] = new Vec2(m_vertices[0]);
m_prevVertex.set(m_vertices[m_count - 2]);
m_nextVertex.set(m_vertices[1]);
m_hasPrevVertex = true;
m_hasNextVertex = true;
}
use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.
the class ChainShape method computeAABB.
@Override
public void computeAABB(AABB aabb, Transform transform, int childIndex) {
final Vec2 lower = aabb.lowerBound;
final Vec2 upper = aabb.upperBound;
int i1 = childIndex;
int i2 = childIndex + 1;
if (i2 == m_count) {
i2 = 0;
}
Vec2 vi1 = m_vertices[i1];
Vec2 vi2 = m_vertices[i2];
float v1x = transform.mulX(vi1);
float v1y = transform.mulY(vi1);
float v2x = transform.mulX(vi2);
float v2y = transform.mulY(vi2);
lower.x = min(v1x, v2x);
lower.y = min(v1y, v2y);
upper.x = max(v1x, v2x);
upper.y = max(v1y, v2y);
}
use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.
the class AABB method raycast.
/**
* From Real-time Collision Detection, p179.
*/
public boolean raycast(final RayCastOutput output, final RayCastInput input, IWorldPool argPool) {
float tmin = -Float.MAX_VALUE;
float tmax = Float.MAX_VALUE;
final Vec2 p = argPool.popVec2();
final Vec2 d = argPool.popVec2();
final Vec2 absD = argPool.popVec2();
final Vec2 normal = argPool.popVec2();
p.set(input.p1);
d.set(input.p2).subLocal(input.p1);
absD.x = abs(d.x);
absD.y = abs(d.y);
// x then y
if (absD.x < JBoxSettings.EPSILON) {
// Parallel.
if (p.x < lowerBound.x || upperBound.x < p.x) {
argPool.pushVec2(4);
return false;
}
} else {
final float inv_d = 1.0f / d.x;
float t1 = (lowerBound.x - p.x) * inv_d;
float t2 = (upperBound.x - p.x) * inv_d;
// Sign of the normal vector.
float s = -1.0f;
if (t1 > t2) {
final float temp = t1;
t1 = t2;
t2 = temp;
s = 1.0f;
}
// Push the min up
if (t1 > tmin) {
normal.setZero();
normal.x = s;
tmin = t1;
}
// Pull the max down
tmax = Math.min(tmax, t2);
if (tmin > tmax) {
argPool.pushVec2(4);
return false;
}
}
if (absD.y < JBoxSettings.EPSILON) {
// Parallel.
if (p.y < lowerBound.y || upperBound.y < p.y) {
argPool.pushVec2(4);
return false;
}
} else {
final float inv_d = 1.0f / d.y;
float t1 = (lowerBound.y - p.y) * inv_d;
float t2 = (upperBound.y - p.y) * inv_d;
// Sign of the normal vector.
float s = -1.0f;
if (t1 > t2) {
final float temp = t1;
t1 = t2;
t2 = temp;
s = 1.0f;
}
// Push the min up
if (t1 > tmin) {
normal.setZero();
normal.y = s;
tmin = t1;
}
// Pull the max down
tmax = Math.min(tmax, t2);
if (tmin > tmax) {
argPool.pushVec2(4);
return false;
}
}
// Does the ray intersect beyond the max fraction?
if (tmin < 0.0f || input.maxFraction < tmin) {
argPool.pushVec2(4);
return false;
}
// Intersection.
output.fraction = tmin;
output.normal.x = normal.x;
output.normal.y = normal.y;
argPool.pushVec2(4);
return true;
}
use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.
the class AABB method set.
/**
* Sets this object from the given object.
*
* @param aabb the object to copy from
*/
public void set(AABB aabb) {
Vec2 v = aabb.lowerBound;
lowerBound.x = v.x;
lowerBound.y = v.y;
Vec2 v1 = aabb.upperBound;
upperBound.x = v1.x;
upperBound.y = v1.y;
}
use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.
the class PhysicsComponent method overwritePosition.
/**
* Repositions an entity that supports physics directly in the physics world.
* Note: depending on how it is used, it may cause non-physical behavior.
*
* @param point point in game world coordinates (pixels)
*/
public void overwritePosition(Point2D point) {
double w = getEntity().getWidth();
double h = getEntity().getHeight();
Vec2 positionMeters = getPhysicsWorld().toPoint(new Point2D(point.getX() + w / 2, point.getY() + h / 2));
getBody().setTransform(positionMeters, getBody().getAngle());
}
Aggregations