Search in sources :

Example 36 with Vec2

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;
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2)

Example 37 with Vec2

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;
        }
    }
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2)

Example 38 with Vec2

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);
}
Also used : ContactPoint(nars.rover.physics.ContactPoint) Vec2(spacegraph.space2d.phys.common.Vec2) MouseJoint(spacegraph.space2d.phys.dynamics.joints.MouseJoint) ContactPoint(nars.rover.physics.ContactPoint)

Example 39 with Vec2

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());
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2)

Example 40 with 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);
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2)

Aggregations

Vec2 (spacegraph.space2d.phys.common.Vec2)41 Body (spacegraph.space2d.phys.dynamics.Body)9 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)6 Tuple2f (spacegraph.util.math.Tuple2f)6 MouseJoint (spacegraph.space2d.phys.dynamics.joints.MouseJoint)4 JoglAbstractDraw (nars.rover.physics.gl.JoglAbstractDraw)3 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)3 Color3f (spacegraph.space2d.phys.common.Color3f)3 BodyDef (spacegraph.space2d.phys.dynamics.BodyDef)3 Fixture (spacegraph.space2d.phys.dynamics.Fixture)3 VisionRay (nars.rover.obj.VisionRay)2 Physics2dBody (ptrman.difficultyEnvironment.physics.Physics2dBody)2 CircleShape (spacegraph.space2d.phys.collision.shapes.CircleShape)2 DistanceJoint (spacegraph.space2d.phys.dynamics.joints.DistanceJoint)2 GLCapabilities (com.jogamp.opengl.GLCapabilities)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Material (nars.rover.Material)1 PhysicsModel (nars.rover.PhysicsModel)1 Sim (nars.rover.Sim)1