Search in sources :

Example 91 with Vec2

use of org.jbox2d.common.Vec2 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 92 with Vec2

use of org.jbox2d.common.Vec2 in project SnapStudio by reportmill.

the class PhysicsRunner method createShape.

/**
 * Creates a Box2D shape for given snap shape.
 */
public org.jbox2d.collision.shapes.Shape createShape(Polygon aPoly) {
    // If invalid, just return null
    if (!aPoly.isSimple() || !aPoly.isConvex() || aPoly.getPointCount() > 8)
        return null;
    // Create Box2D PolygonShape and return
    int pc = aPoly.getPointCount();
    Vec2[] vecs = new Vec2[pc];
    for (int i = 0; i < pc; i++) vecs[i] = viewToBox(aPoly.getX(i), aPoly.getY(i));
    PolygonShape pshape = new PolygonShape();
    pshape.set(vecs, vecs.length);
    return pshape;
}
Also used : PolygonShape(org.jbox2d.collision.shapes.PolygonShape) Vec2(org.jbox2d.common.Vec2)

Example 93 with Vec2

use of org.jbox2d.common.Vec2 in project SnapStudio by reportmill.

the class PhysicsRunner method updateBody.

/**
 * Updates a body from a view.
 */
public void updateBody(View aView) {
    // Get ViewPhysics and body
    ViewPhysics<Body> phys = aView.getPhysics();
    if (phys == null || phys.isDynamic() || phys.isJoint())
        return;
    Body body = phys.getNative();
    // Get/set position
    Vec2 pos0 = body.getPosition();
    Vec2 pos1 = viewToBox(aView.getMidX(), aView.getMidY());
    double vx = (pos1.x - pos0.x) * 25;
    double vy = (pos1.y - pos0.y) * 25;
    body.setLinearVelocity(new Vec2((float) vx, (float) vy));
    // Get/set rotation
    double rot0 = body.getAngle();
    double rot1 = Math.toRadians(-aView.getRotate());
    double dr = rot1 - rot0;
    if (dr > Math.PI || dr < -Math.PI)
        dr = MathUtils.mod(dr + Math.PI, Math.PI * 2) - Math.PI;
    body.setAngularVelocity((float) dr * 25);
}
Also used : Vec2(org.jbox2d.common.Vec2)

Example 94 with Vec2

use of org.jbox2d.common.Vec2 in project SnapStudio by reportmill.

the class PhysicsRunner method updateDrag.

/**
 * Updates drag view's body.
 */
void updateDrag(View aView, double dragX, double dragY) {
    ViewPhysics<Body> phys = aView.getPhysics();
    Body body = phys.getNative();
    Vec2 pos0 = body.getPosition();
    Vec2 pos1 = viewToBox(dragX, dragY);
    double dx = pos1.x - pos0.x;
    double dy = pos1.y - pos0.y;
    double vx = (pos1.x - pos0.x) * 25;
    double vy = (pos1.y - pos0.y) * 25;
    body.setLinearVelocity(new Vec2((float) vx, (float) vy));
}
Also used : Vec2(org.jbox2d.common.Vec2)

Example 95 with Vec2

use of org.jbox2d.common.Vec2 in project libgdx by libgdx.

the class WorldRayCastWrapper method drawDebugData.

/**
   * Call this to draw shapes and other debug draw data.
   */
public void drawDebugData() {
    if (m_debugDraw == null) {
        return;
    }
    int flags = m_debugDraw.getFlags();
    boolean wireframe = (flags & DebugDraw.e_wireframeDrawingBit) != 0;
    if ((flags & DebugDraw.e_shapeBit) != 0) {
        for (Body b = m_bodyList; b != null; b = b.getNext()) {
            xf.set(b.getTransform());
            for (Fixture f = b.getFixtureList(); f != null; f = f.getNext()) {
                if (b.isActive() == false) {
                    color.set(0.5f, 0.5f, 0.3f);
                    drawShape(f, xf, color, wireframe);
                } else if (b.getType() == BodyType.STATIC) {
                    color.set(0.5f, 0.9f, 0.3f);
                    drawShape(f, xf, color, wireframe);
                } else if (b.getType() == BodyType.KINEMATIC) {
                    color.set(0.5f, 0.5f, 0.9f);
                    drawShape(f, xf, color, wireframe);
                } else if (b.isAwake() == false) {
                    color.set(0.5f, 0.5f, 0.5f);
                    drawShape(f, xf, color, wireframe);
                } else {
                    color.set(0.9f, 0.7f, 0.7f);
                    drawShape(f, xf, color, wireframe);
                }
            }
        }
        drawParticleSystem(m_particleSystem);
    }
    if ((flags & DebugDraw.e_jointBit) != 0) {
        for (Joint j = m_jointList; j != null; j = j.getNext()) {
            drawJoint(j);
        }
    }
    if ((flags & DebugDraw.e_pairBit) != 0) {
        color.set(0.3f, 0.9f, 0.9f);
        for (Contact c = m_contactManager.m_contactList; c != null; c = c.getNext()) {
            Fixture fixtureA = c.getFixtureA();
            Fixture fixtureB = c.getFixtureB();
            fixtureA.getAABB(c.getChildIndexA()).getCenterToOut(cA);
            fixtureB.getAABB(c.getChildIndexB()).getCenterToOut(cB);
            m_debugDraw.drawSegment(cA, cB, color);
        }
    }
    if ((flags & DebugDraw.e_aabbBit) != 0) {
        color.set(0.9f, 0.3f, 0.9f);
        for (Body b = m_bodyList; b != null; b = b.getNext()) {
            if (b.isActive() == false) {
                continue;
            }
            for (Fixture f = b.getFixtureList(); f != null; f = f.getNext()) {
                for (int i = 0; i < f.m_proxyCount; ++i) {
                    FixtureProxy proxy = f.m_proxies[i];
                    AABB aabb = m_contactManager.m_broadPhase.getFatAABB(proxy.proxyId);
                    if (aabb != null) {
                        Vec2[] vs = avs.get(4);
                        vs[0].set(aabb.lowerBound.x, aabb.lowerBound.y);
                        vs[1].set(aabb.upperBound.x, aabb.lowerBound.y);
                        vs[2].set(aabb.upperBound.x, aabb.upperBound.y);
                        vs[3].set(aabb.lowerBound.x, aabb.upperBound.y);
                        m_debugDraw.drawPolygon(vs, 4, color);
                    }
                }
            }
        }
    }
    if ((flags & DebugDraw.e_centerOfMassBit) != 0) {
        for (Body b = m_bodyList; b != null; b = b.getNext()) {
            xf.set(b.getTransform());
            xf.p.set(b.getWorldCenter());
            m_debugDraw.drawTransform(xf);
        }
    }
    if ((flags & DebugDraw.e_dynamicTreeBit) != 0) {
        m_contactManager.m_broadPhase.drawTree(m_debugDraw);
    }
    m_debugDraw.flush();
}
Also used : Vec2(org.jbox2d.common.Vec2) Joint(org.jbox2d.dynamics.joints.Joint) PulleyJoint(org.jbox2d.dynamics.joints.PulleyJoint) Joint(org.jbox2d.dynamics.joints.Joint) PulleyJoint(org.jbox2d.dynamics.joints.PulleyJoint) AABB(org.jbox2d.collision.AABB) ParticleContact(org.jbox2d.particle.ParticleContact) Contact(org.jbox2d.dynamics.contacts.Contact) ParticleBodyContact(org.jbox2d.particle.ParticleBodyContact)

Aggregations

Vec2 (org.jbox2d.common.Vec2)185 Rot (org.jbox2d.common.Rot)36 Body (org.jbox2d.dynamics.Body)11 World (org.jbox2d.dynamics.World)9 AABB (org.jbox2d.collision.AABB)8 Mat22 (org.jbox2d.common.Mat22)7 Joint (org.jbox2d.dynamics.joints.Joint)7 PulleyJoint (org.jbox2d.dynamics.joints.PulleyJoint)7 ManifoldPoint (org.jbox2d.collision.ManifoldPoint)6 Test (org.junit.jupiter.api.Test)6 PolygonShape (org.jbox2d.collision.shapes.PolygonShape)5 Transform (org.jbox2d.common.Transform)5 Vec3 (org.jbox2d.common.Vec3)5 BodyDef (org.jbox2d.dynamics.BodyDef)5 VelocityConstraintPoint (org.jbox2d.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint)5 CircleShape (org.jbox2d.collision.shapes.CircleShape)4 Test (org.junit.Test)4 AffineTransform (java.awt.geom.AffineTransform)3 Shape (org.jbox2d.collision.shapes.Shape)3 Mat33 (org.jbox2d.common.Mat33)3