Search in sources :

Example 6 with PolygonShape

use of org.jbox2d.collision.shapes.PolygonShape in project HackerHop by nicovank.

the class PlayerTest method playerShapeTest.

@Test
void playerShapeTest() {
    World world = new World(new Vec2(0, -50));
    Vec2 position = new Vec2(0, 10);
    Player p = new Player(world, position);
    Shape s = p.getBody().getFixtureList().m_shape;
    PolygonShape r = new PolygonShape();
    r.setAsBox(3, 3);
    assertEquals(s.getType(), r.getType());
}
Also used : PolygonShape(org.jbox2d.collision.shapes.PolygonShape) PolygonShape(org.jbox2d.collision.shapes.PolygonShape) Shape(org.jbox2d.collision.shapes.Shape) Vec2(org.jbox2d.common.Vec2) World(org.jbox2d.dynamics.World) Test(org.junit.jupiter.api.Test)

Example 7 with PolygonShape

use of org.jbox2d.collision.shapes.PolygonShape in project opennars by opennars.

the class RobotArm method initTest.

@Override
public void initTest(boolean argDeserialized) {
    for (int i = 0; i < 10; i++) {
        getCamera().zoomToPoint(new Vec2(0, 0), PhysicsCamera.ZoomType.ZOOM_IN);
    }
    Body ground = null;
    {
        BodyDef bd = new BodyDef();
        ground = getWorld().createBody(bd);
        EdgeShape shape = new EdgeShape();
        shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
        ground.createFixture(shape, 0.0f);
    }
    {
        Body prevBody = ground;
        // Define upper arm.
        {
            PolygonShape shape = new PolygonShape();
            shape.setAsBox(0.5f, 4.0f);
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DYNAMIC;
            bd.position.set(0.0f, 5.0f);
            upperArm = getWorld().createBody(bd);
            upperArm.createFixture(shape, 2.0f);
            RevoluteJointDef rjd = new RevoluteJointDef();
            rjd.initialize(prevBody, upperArm, new Vec2(0.0f, 2.0f));
            // 1.0f * MathUtils.PI;
            rjd.motorSpeed = 0;
            rjd.maxMotorTorque = 10000.0f;
            rjd.enableMotor = true;
            rjd.lowerAngle = -MathUtils.PI / 2f * 1.5f;
            rjd.upperAngle = MathUtils.PI / 2f * 1.5f;
            rjd.enableLimit = true;
            shoulderJoint = (RevoluteJoint) getWorld().createJoint(rjd);
            prevBody = upperArm;
        }
        // Define lower arm.
        {
            PolygonShape shape = new PolygonShape();
            shape.setAsBox(0.5f, 2.0f);
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DYNAMIC;
            bd.position.set(0.0f, 11.0f);
            lowerArm = getWorld().createBody(bd);
            lowerArm.createFixture(shape, 1.0f);
            RevoluteJointDef rjd = new RevoluteJointDef();
            rjd.initialize(prevBody, lowerArm, new Vec2(0.0f, 9.0f));
            rjd.enableMotor = true;
            rjd.lowerAngle = -MathUtils.PI / 2f * 1.5f;
            rjd.upperAngle = MathUtils.PI / 2f * 1.5f;
            rjd.enableLimit = true;
            elbowJoint = (RevoluteJoint) getWorld().createJoint(rjd);
        }
        // Finger Right
        {
            PolygonShape shape = new PolygonShape();
            shape.setAsBox(0.1f, 0.75f);
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DYNAMIC;
            bd.position.set(0.5f, 13.5f);
            Body body = getWorld().createBody(bd);
            body.createFixture(shape, 0.25f);
            RevoluteJointDef rjd = new RevoluteJointDef();
            rjd.initialize(lowerArm, body, new Vec2(0.5f, 13.0f));
            rjd.enableMotor = true;
            rjd.upperAngle = MathUtils.PI / 8f * 1.5f;
            rjd.lowerAngle = -MathUtils.PI / 4f * 1.5f;
            rjd.enableLimit = true;
            fingerRightJoint = (RevoluteJoint) getWorld().createJoint(rjd);
            prevBody = body;
        }
        // Finger Left
        {
            PolygonShape shape = new PolygonShape();
            shape.setAsBox(0.1f, 0.75f);
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DYNAMIC;
            bd.position.set(-0.5f, 13.5f);
            Body body = getWorld().createBody(bd);
            body.createFixture(shape, 0.25f);
            RevoluteJointDef rjd = new RevoluteJointDef();
            rjd.initialize(lowerArm, body, new Vec2(-0.5f, 13.0f));
            rjd.enableMotor = true;
            rjd.upperAngle = MathUtils.PI / 4f * 1.5f;
            rjd.lowerAngle = -MathUtils.PI / 8f * 1.5f;
            rjd.enableLimit = true;
            fingerLeftJoint = (RevoluteJoint) getWorld().createJoint(rjd);
        }
    // // Define piston
    // {
    // PolygonShape shape = new PolygonShape();
    // shape.setAsBox(1.5f, 1.5f);
    // 
    // BodyDef bd = new BodyDef();
    // bd.type = BodyType.DYNAMIC;
    // bd.fixedRotation = true;
    // bd.position.set(0.0f, 17.0f);
    // Body body = getWorld().createBody(bd);
    // body.createFixture(shape, 2.0f);
    // 
    // RevoluteJointDef rjd = new RevoluteJointDef();
    // rjd.initialize(prevBody, body, new Vec2(0.0f, 17.0f));
    // getWorld().createJoint(rjd);
    // 
    // PrismaticJointDef pjd = new PrismaticJointDef();
    // pjd.initialize(ground, body, new Vec2(0.0f, 17.0f), new Vec2(0.0f, 1.0f));
    // 
    // pjd.maxMotorForce = 1000.0f;
    // pjd.enableMotor = false;
    // 
    // m_joint2 = (PrismaticJoint) getWorld().createJoint(pjd);
    // }
    // // Create a payload
    // {
    // PolygonShape shape = new PolygonShape();
    // shape.setAsBox(1.5f, 1.5f);
    // 
    // BodyDef bd = new BodyDef();
    // bd.type = BodyType.DYNAMIC;
    // bd.position.set(0.0f, 23.0f);
    // Body body = getWorld().createBody(bd);
    // body.createFixture(shape, 2.0f);
    // }
    }
}
Also used : EdgeShape(org.jbox2d.collision.shapes.EdgeShape) PolygonShape(org.jbox2d.collision.shapes.PolygonShape) Vec2(org.jbox2d.common.Vec2) Body(org.jbox2d.dynamics.Body) BodyDef(org.jbox2d.dynamics.BodyDef) RevoluteJoint(org.jbox2d.dynamics.joints.RevoluteJoint) RevoluteJointDef(org.jbox2d.dynamics.joints.RevoluteJointDef)

Aggregations

PolygonShape (org.jbox2d.collision.shapes.PolygonShape)7 Vec2 (org.jbox2d.common.Vec2)5 CircleShape (org.jbox2d.collision.shapes.CircleShape)3 Shape (org.jbox2d.collision.shapes.Shape)2 Body (org.jbox2d.dynamics.Body)2 EdgeShape (org.jbox2d.collision.shapes.EdgeShape)1 Rot (org.jbox2d.common.Rot)1 Transform (org.jbox2d.common.Transform)1 BodyDef (org.jbox2d.dynamics.BodyDef)1 Fixture (org.jbox2d.dynamics.Fixture)1 World (org.jbox2d.dynamics.World)1 RevoluteJoint (org.jbox2d.dynamics.joints.RevoluteJoint)1 RevoluteJointDef (org.jbox2d.dynamics.joints.RevoluteJointDef)1 Test (org.junit.jupiter.api.Test)1