Search in sources :

Example 1 with JoglAbstractDraw

use of nars.rover.physics.gl.JoglAbstractDraw in project narchy by automenta.

the class Rover method newTorso.

@Override
protected Body newTorso() {
    PolygonShape shape = new PolygonShape();
    Vec2[] vertices = { new Vec2(3.0f, 0.0f), new Vec2(-1.0f, +2.0f), new Vec2(-1.0f, -2.0f) };
    shape.set(vertices, vertices.length);
    // shape.m_centroid.set(bodyDef.position);
    BodyDef bd = new BodyDef();
    bd.linearDamping = (linearDamping);
    bd.angularDamping = (angularDamping);
    bd.type = BodyType.DYNAMIC;
    bd.position.set(0, 0);
    Body torso = getWorld().createBody(bd);
    Fixture f = torso.createFixture(shape, mass);
    f.setRestitution(restitution);
    f.setFriction(friction);
    // for (int i = -pixels / 2; i <= pixels / 2; i++) {
    for (int i = 0; i < retinaPixels; i++) {
        final int ii = i;
        final float angle = /*MathUtils.PI / 2f*/
        aStep * i;
        final boolean eats = ((angle < mouthArc / 2f) || (angle > (Math.PI * 2f) - mouthArc / 2f));
        // System.out.println(i + " " + angle + " " + eats);
        VisionRay v = new VisionRay(this, torso, /*eats ?*/
        mouthPoint, /*: new Vec2(0,0)*/
        angle, aStep, retinaRaysPerPixel, L, distanceResolution) {

            @Override
            public void onTouch(Body touched, float di) {
                if (touched == null)
                    return;
                if (touched.getUserData() instanceof Sim.Edible) {
                    if (eats) {
                        if (di <= biteDistanceThreshold)
                            eat(touched);
                    /*} else if (di <= tasteDistanceThreshold) {
                                //taste(touched, di );
                            }*/
                    }
                }
            }
        };
        v.sparkColor = new Color3f(0.5f, 0.4f, 0.4f);
        v.normalColor = new Color3f(0.4f, 0.4f, 0.4f);
        ((JoglAbstractDraw) draw).addLayer(v);
        senses.add(v);
    }
    return torso;
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) VisionRay(nars.rover.obj.VisionRay) Color3f(spacegraph.space2d.phys.common.Color3f) Vec2(spacegraph.space2d.phys.common.Vec2) Fixture(spacegraph.space2d.phys.dynamics.Fixture) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) Body(spacegraph.space2d.phys.dynamics.Body) JoglAbstractDraw(nars.rover.physics.gl.JoglAbstractDraw)

Example 2 with JoglAbstractDraw

use of nars.rover.physics.gl.JoglAbstractDraw in project narchy by automenta.

the class Test method main.

public static void main(String[] args) {
    Environment environment = new Environment();
    JavascriptDescriptor javascriptDescriptor = new JavascriptDescriptor();
    javascriptDescriptor.engine = new JavascriptEngine();
    javascriptDescriptor.environmentScriptingAccessor = new EnvironmentScriptingAccessor(environment);
    javascriptDescriptor.helperScriptingAccessor = new HelperScriptingAccessor();
    javascriptDescriptor.componentManipulationScriptingAccessor = new ComponentManipulationScriptingAccessor();
    // execute the initialisation script
    final String worldInitialisationScript = "function initializeWorld(environment) {" + "   environmentScriptingAccessor.physics2dCreateWorld();" + "}";
    WorldInitialisation.executeWorldInitialisationScript(environment, javascriptDescriptor, worldInitialisationScript);
    final String spawnScript = "function spawn() {" + "   var resultEntity = environmentScriptingAccessor.createNewEntity(helperScriptingAccessor.create2dArrayRealVector(0.0, 0.0));\n" + "   // create physics body of a rover body and set physics parameters\n" + "   var verticesPoints = helperScriptingAccessor.createList();\n" + "   verticesPoints.add(helperScriptingAccessor.create2dArrayRealVector(3.0, 0.0));\n" + "   verticesPoints.add(helperScriptingAccessor.create2dArrayRealVector(-1.0, 2.0));\n" + "   verticesPoints.add(helperScriptingAccessor.create2dArrayRealVector(-1.0, -2.0));\n" + "   var linearDamping = 0.9;\n" + "   var angularDamping = 0.6;\n" + "   var restitution = 0.9; // bounciness\n" + "   var friction = 0.5;\n" + "   var mass = 5.0;\n" + "   var static = false;\n" + "   var position = helperScriptingAccessor.create2dArrayRealVector(0.0, 0.0);\n" + "   resultEntity.physics2dBody = environmentScriptingAccessor.physics2dCreateBodyWithShape(static, position, verticesPoints, linearDamping, angularDamping, mass, restitution, friction);\n" + "   " + "   var physicsComponent = componentManipulationScriptingAccessor.createTopDownViewWheeledPhysicsComponent(8.0, 0.44 * 0.25);" + "   resultEntity.components.addComponent(physicsComponent);\n" + "   var controllerComponent = componentManipulationScriptingAccessor.createTopDownViewWheeledControllerComponent(1.0);" + "   controllerComponent.physicsComponent = physicsComponent;" + "   resultEntity.components.addComponent(controllerComponent);\n" + "   var biasedRandomAIComponent = componentManipulationScriptingAccessor.createBiasedRandomAIComponent(3.0, 0.5, 0.5);" + "   biasedRandomAIComponent.topDownViewWheeledControllerComponent = controllerComponent;" + "   resultEntity.components.addComponent(biasedRandomAIComponent);\n" + "   " + "   return resultEntity;\n" + "}\n";
    EntityDescriptor entityDescriptor = EntitySpawner.spawn(javascriptDescriptor, spawnScript, new ArrayList<>());
    environment.entities.add(entityDescriptor);
    // spawn testboxes manually
    final List<ArrayRealVector> boxPositions = RandomSampler.sample(new ArrayRealVector(new double[] { 10.0, 10.0 }), 10);
    for (final ArrayRealVector boxPosition : boxPositions) {
        EntityDescriptor createdEntity = javascriptDescriptor.environmentScriptingAccessor.createNewEntity(javascriptDescriptor.helperScriptingAccessor.create2dArrayRealVector(0.0f, 0.0f));
        // create physics body of a rover body and set physics parameters
        List verticesPoints = javascriptDescriptor.helperScriptingAccessor.createList();
        verticesPoints.add(javascriptDescriptor.helperScriptingAccessor.create2dArrayRealVector(-.5f, 0.5f));
        verticesPoints.add(javascriptDescriptor.helperScriptingAccessor.create2dArrayRealVector(0.5f, 0.5f));
        verticesPoints.add(javascriptDescriptor.helperScriptingAccessor.create2dArrayRealVector(0.5f, -0.5f));
        verticesPoints.add(javascriptDescriptor.helperScriptingAccessor.create2dArrayRealVector(-0.5f, -0.5f));
        float linearDamping = 0.9f;
        float angularDamping = 0.6f;
        float restitution = 0.9f;
        float friction = 0.5f;
        float mass = 5.0f;
        boolean isStatic = false;
        ArrayRealVector position = boxPosition;
        createdEntity.physics2dBody = javascriptDescriptor.environmentScriptingAccessor.physics2dCreateBodyWithShape(isStatic, position, verticesPoints, linearDamping, angularDamping, mass, restitution, friction);
        environment.entities.add(createdEntity);
    }
    final float timeDelta = 0.1f;
    final float cameraZoomScaleDiff = 2.0f;
    final float initScale = 50.0f;
    final Vec2 initPosition = new Vec2(-10.0f, -10.0f);
    PhysicsCamera physicsCamera = new PhysicsCamera(initPosition, initScale, cameraZoomScaleDiff);
    JoglAbstractDraw debugDraw = new JoglDraw(physicsCamera);
    GLCapabilities config = new GLCapabilities(GLProfile.getDefault());
    config.setHardwareAccelerated(true);
    config.setAlphaBits(8);
    config.setAccumAlphaBits(8);
    config.setAccumRedBits(8);
    config.setAccumGreenBits(8);
    config.setAccumBlueBits(8);
    config.setNumSamples(1);
    // config.setBackgroundOpaque(false);
    JoglPanel panel = new JoglPanel(environment.physicsWorld2d, debugDraw, null, config);
    debugDraw.setPanel(panel);
    DrawerRunnable drawerRunnable = new DrawerRunnable();
    drawerRunnable.panel = panel;
    JFrame window = new JFrame();
    window.setTitle("Testbed");
    window.setLayout(new BorderLayout());
    window.add(panel, "Center");
    window.pack();
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    for (; ; ) {
        environment.stepFrame(timeDelta, javascriptDescriptor);
        for (EntityDescriptor iterationEntity : environment.entities) {
            if (iterationEntity.physics2dBody == null) {
                continue;
            }
        }
        // draw
        SwingUtilities.invokeLater(drawerRunnable);
        try {
            Thread.sleep((int) (timeDelta * 1000.0f));
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) PhysicsCamera(nars.rover.physics.PhysicsCamera) JavascriptEngine(ptrman.difficultyEnvironment.JavascriptEngine) EnvironmentScriptingAccessor(ptrman.difficultyEnvironment.scriptAccessors.EnvironmentScriptingAccessor) JoglDraw(ptrman.difficultyEnvironment.view.JoglDraw) DrawerRunnable(ptrman.difficultyEnvironment.view.DrawerRunnable) ComponentManipulationScriptingAccessor(ptrman.difficultyEnvironment.scriptAccessors.ComponentManipulationScriptingAccessor) EntityDescriptor(ptrman.difficultyEnvironment.EntityDescriptor) GLCapabilities(com.jogamp.opengl.GLCapabilities) HelperScriptingAccessor(ptrman.difficultyEnvironment.scriptAccessors.HelperScriptingAccessor) Vec2(spacegraph.space2d.phys.common.Vec2) JoglPanel(ptrman.difficultyEnvironment.view.JoglPanel) JavascriptDescriptor(ptrman.difficultyEnvironment.JavascriptDescriptor) Environment(ptrman.difficultyEnvironment.Environment) ArrayList(java.util.ArrayList) List(java.util.List) JoglAbstractDraw(nars.rover.physics.gl.JoglAbstractDraw)

Example 3 with JoglAbstractDraw

use of nars.rover.physics.gl.JoglAbstractDraw in project narchy by automenta.

the class Turret method getMaterial.

@Override
public RoboticMaterial getMaterial() {
    return new RoboticMaterial(this) {

        @Override
        public void before(Body b, JoglAbstractDraw d, float time) {
            super.before(b, d, time);
            if (!explosions.isEmpty()) {
                Iterator<BulletData> ii = explosions.iterator();
                while (ii.hasNext()) {
                    BulletData bd = ii.next();
                    if (bd.explosionTTL-- <= 0)
                        ii.remove();
                    d.drawSolidCircle(bd.getCenter(), bd.explosionTTL / 8 + rng.nextFloat() * 4, new Vec2(), new Color3f(1 - rng.nextFloat() / 3f, 0.8f - rng.nextFloat() / 3f, 0f));
                }
            }
        }
    };
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2) Color3f(spacegraph.space2d.phys.common.Color3f) Body(spacegraph.space2d.phys.dynamics.Body) JoglAbstractDraw(nars.rover.physics.gl.JoglAbstractDraw)

Aggregations

JoglAbstractDraw (nars.rover.physics.gl.JoglAbstractDraw)3 Vec2 (spacegraph.space2d.phys.common.Vec2)3 Color3f (spacegraph.space2d.phys.common.Color3f)2 Body (spacegraph.space2d.phys.dynamics.Body)2 GLCapabilities (com.jogamp.opengl.GLCapabilities)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 VisionRay (nars.rover.obj.VisionRay)1 PhysicsCamera (nars.rover.physics.PhysicsCamera)1 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)1 EntityDescriptor (ptrman.difficultyEnvironment.EntityDescriptor)1 Environment (ptrman.difficultyEnvironment.Environment)1 JavascriptDescriptor (ptrman.difficultyEnvironment.JavascriptDescriptor)1 JavascriptEngine (ptrman.difficultyEnvironment.JavascriptEngine)1 ComponentManipulationScriptingAccessor (ptrman.difficultyEnvironment.scriptAccessors.ComponentManipulationScriptingAccessor)1 EnvironmentScriptingAccessor (ptrman.difficultyEnvironment.scriptAccessors.EnvironmentScriptingAccessor)1 HelperScriptingAccessor (ptrman.difficultyEnvironment.scriptAccessors.HelperScriptingAccessor)1 DrawerRunnable (ptrman.difficultyEnvironment.view.DrawerRunnable)1 JoglDraw (ptrman.difficultyEnvironment.view.JoglDraw)1 JoglPanel (ptrman.difficultyEnvironment.view.JoglPanel)1