use of spacegraph.space2d.phys.common.Vec2 in project narchy by automenta.
the class AABB method getCenter.
/**
* Get the center of the AABB
*
* @return
*/
public final Tuple2f getCenter() {
final Vec2 center = new Vec2(lowerBound);
center.addLocal(upperBound);
center.scaled(.5f);
return center;
}
use of spacegraph.space2d.phys.common.Vec2 in project narchy by automenta.
the class TwoDimensionalRaysComponent method frameInteraction.
@Override
public void frameInteraction(JavascriptDescriptor javascriptDescriptor, EntityDescriptor entityDescriptor, float timedelta) {
if (entityDescriptor.physics2dBody == null) {
return;
}
final float halfRaysSpreadAngleInRadiants = raysSpreadAngleInRadiants / 2.0f;
for (int rayIndex = 0; rayIndex < getNumberOfRays(); rayIndex++) {
final float rayIndexAsFraction = (float) rayIndex / (float) getNumberOfRays();
final float rayRelativeAngleInRadiants = -halfRaysSpreadAngleInRadiants + rayIndexAsFraction * raysSpreadAngleInRadiants;
final Vec2 rayDirection = getNormalizedDirectionOfAngleInRadiants(rayRelativeAngleInRadiants);
final Vec2 relativeRayStartPosition = rayDirection.mul(raysStartDistance);
final Vec2 relativeRayEndPosition = rayDirection.mul(raysStartDistance + raysLength);
final Vec2 rayGlobalStartPosition = entityDescriptor.physics2dBody.body.getWorldPoint(relativeRayStartPosition);
final Vec2 rayGlobalStopPosition = entityDescriptor.physics2dBody.body.getWorldPoint(relativeRayEndPosition);
collisionCallback.reset();
collisionCallback.normalizedDirection = rayGlobalStopPosition.sub(rayGlobalStartPosition);
// normalize
collisionCallback.normalizedDirection = collisionCallback.normalizedDirection.mul(1.0f / collisionCallback.normalizedDirection.length());
try {
entityDescriptor.physics2dBody.body.getWorld().raycast(collisionCallback, rayGlobalStartPosition, rayGlobalStopPosition);
} catch (Exception e) {
System.err.println("Phys2D raycast: " + e + " " + rayGlobalStartPosition + " " + rayGlobalStopPosition);
e.printStackTrace();
}
if (collisionCallback.hittedAnything) {
final float closestHitDistance = collisionCallback.closestHitDistance;
hitDistanceFractions[rayIndex] = closestHitDistance / raysLength;
} else {
hitDistanceFractions[rayIndex] = Float.MAX_VALUE;
}
}
}
use of spacegraph.space2d.phys.common.Vec2 in project narchy by automenta.
the class DrawerRunnable method run.
@Override
public void run() {
// TODO< need to be set from outside >
float timeStep = 0.0f;
if (!panel.render()) {
return;
}
if (mouseTracing && mouseJoint == null) {
final float delay = 0.1f;
acceleration.x = 2 / delay * (1 / delay * (mouseWorld.x - mouseTracerPosition.x) - mouseTracerVelocity.x);
acceleration.y = 2 / delay * (1 / delay * (mouseWorld.y - mouseTracerPosition.y) - mouseTracerVelocity.y);
mouseTracerVelocity.x += timeStep * acceleration.x;
mouseTracerVelocity.y += timeStep * acceleration.y;
mouseTracerPosition.x += timeStep * mouseTracerVelocity.x;
mouseTracerPosition.y += timeStep * mouseTracerVelocity.y;
pshape.m_p.set(mouseTracerPosition);
pshape.m_radius = 2;
pshape.computeAABB(paabb, identity, 0);
}
if (mouseJoint != null) {
mouseJoint.getAnchorB(p1);
Vec2 p2 = mouseJoint.getTarget();
draw.drawSegment(p1, p2, mouseColor);
}
if (false) /*settings.getSetting(TestbedSettings.DrawContactPoints).enabled*/
{
final float k_impulseScale = 0.1f;
final float axisScale = 0.3f;
// TODO< need to be get from DrawingContactListener >
int pointCount = 0;
for (int i = 0; i < pointCount; i++) {
ContactPoint point = points[i];
if (point.state == Collision.PointState.ADD_STATE) {
draw.drawPoint(point.position, 10f, color1);
} else if (point.state == Collision.PointState.PERSIST_STATE) {
draw.drawPoint(point.position, 5f, color2);
}
if (settings.getSetting(TestbedSettings.DrawContactNormals).enabled) {
p1.set(point.position);
p2.set(point.normal).mulLocal(axisScale).addLocal(p1);
draw.drawSegment(p1, p2, color3);
} else if (settings.getSetting(TestbedSettings.DrawContactImpulses).enabled) {
p1.set(point.position);
p2.set(point.normal).mulLocal(k_impulseScale).mulLocal(point.normalImpulse).addLocal(p1);
draw.drawSegment(p1, p2, color5);
}
if (settings.getSetting(TestbedSettings.DrawFrictionImpulses).enabled) {
Vec2.crossToOutUnsafe(point.normal, 1, tangent);
p1.set(point.position);
p2.set(tangent).mulLocal(k_impulseScale).mulLocal(point.tangentImpulse).addLocal(p1);
draw.drawSegment(p1, p2, color5);
}
}
}
panel.paintScreen();
drawingQueued.set(false);
}
use of spacegraph.space2d.phys.common.Vec2 in project narchy by automenta.
the class AbstractPolygonBot method stop.
public void stop() {
torso.setAngularVelocity(0);
torso.setLinearVelocity(new Vec2());
}
use of spacegraph.space2d.phys.common.Vec2 in project narchy by automenta.
the class AbstractPolygonBot method thrust.
public void thrust(float angle, float force) {
// + Math.PI / 2; //compensate for initial orientation
angle += torso.getAngle();
// torso.applyForceToCenter(new Vec2((float) Math.cos(angle) * force, (float) Math.sin(angle) * force));
Vec2 v = new Vec2((float) Math.cos(angle) * force, (float) Math.sin(angle) * force);
torso.setLinearVelocity(v);
// torso.applyLinearImpulse(v, torso.getWorldCenter(), true);
}
Aggregations