public Body createTempBodyDistanceJoint(Body bodyA, Body bodyB, Vector2 localA, Vector2 localB, int length) { DistanceJointDef dJoint = new DistanceJointDef(); FixtureDef fixtureDef = createFixtureDef(1f, 0.5f, 0.2f, (short) 0x0004); IPhysicsObject sprite = (IPhysicsObject) bodyA.getUserData(); Body temp = createTempBody(sprite.getX(), sprite.getY(), fixtureDef); createRevoluteJoint(bodyA, temp, localA, Vector2.Zero); dJoint.bodyA = temp; dJoint.bodyB = bodyB; dJoint.localAnchorA.set(localA.x * WORLD_TO_BOX, localA.y * WORLD_TO_BOX); dJoint.localAnchorB.set(localB.x * WORLD_TO_BOX, localB.y * WORLD_TO_BOX); dJoint.length = length * WORLD_TO_BOX; world.createJoint(dJoint); return temp; }
public JointSerializer(RubeScene scene,Json _json) { this.scene = scene; _json.setSerializer(RevoluteJointDef.class, new RevoluteJointDefSerializer()); _json.setSerializer(PrismaticJointDef.class, new PrismaticJointDefSerializer()); _json.setSerializer(PulleyJointDef.class, new PulleyJointDefSerializer()); _json.setSerializer(WeldJointDef.class, new WeldJointDefSerializer()); _json.setSerializer(FrictionJointDef.class, new FrictionJointDefSerializer()); _json.setSerializer(WheelJointDef.class, new WheelJointDefSerializer()); _json.setSerializer(RopeJointDef.class, new RopeJointDefSerializer()); _json.setSerializer(DistanceJointDef.class, new DistanceJointDefSerializer()); _json.setSerializer(GearJointDef.class, new GearJointDefSerializer()); mouseJointDefSerializer = new MouseJointDefSerializer(); _json.setSerializer(MouseJointDef.class, mouseJointDefSerializer); }
@SuppressWarnings("rawtypes") @Override public DistanceJointDef read(Json json, JsonValue jsonData, Class type) { DistanceJointDef defaults = RubeDefaults.Joint.distanceDef; DistanceJointDef def = new DistanceJointDef(); Vector2 anchorA = json.readValue("anchorA", Vector2.class, defaults.localAnchorA, jsonData); Vector2 anchorB = json.readValue("anchorB", Vector2.class, defaults.localAnchorB, jsonData); if(anchorA != null && anchorB != null) { def.localAnchorA.set(anchorA); def.localAnchorB.set(anchorB); def.length = json.readValue("length", float.class, defaults.length, jsonData); def.frequencyHz = json.readValue("frequency", float.class, defaults.frequencyHz, jsonData); def.dampingRatio = json.readValue("dampingRatio", float.class, defaults.dampingRatio, jsonData); } return def; }
/** * Create a distance joint between this actor and some other actor * * @param anchor The actor to which this actor is connected * @param anchorX The X coordinate (relative to center) where joint fuses to the anchor * @param anchorY The Y coordinate (relative to center) where joint fuses to the anchor * @param localAnchorX The X coordinate (relative to center) where joint fuses to this actor * @param localAnchorY The Y coordinate (relative to center) where joint fuses to this actor */ public void setDistanceJoint(WorldActor anchor, float anchorX, float anchorY, float localAnchorX, float localAnchorY) { // make the body dynamic setCanFall(); // set up a joint so the head can't move too far mDistJointDef = new DistanceJointDef(); mDistJointDef.bodyA = anchor.mBody; mDistJointDef.bodyB = mBody; mDistJointDef.localAnchorA.set(anchorX, anchorY); mDistJointDef.localAnchorB.set(localAnchorX, localAnchorY); mDistJointDef.collideConnected = false; mDistJointDef.dampingRatio = 0.1f; mDistJointDef.frequencyHz = 2; mDistJoint = mScene.mWorld.createJoint(mDistJointDef); }
public DistanceJoint createDistanceJoint(Body bodyA, Body bodyB, Vector2 localA, Vector2 localB, int length) { DistanceJointDef dJoint = new DistanceJointDef(); dJoint.bodyA = bodyA; dJoint.bodyB = bodyB; dJoint.localAnchorA.set(localA.x * WORLD_TO_BOX, localA.y * WORLD_TO_BOX); dJoint.localAnchorB.set(localB.x * WORLD_TO_BOX, localB.y * WORLD_TO_BOX); dJoint.length = length * WORLD_TO_BOX; return (DistanceJoint) world.createJoint(dJoint); }
@Override protected void setParticles() { ParticleDef plusDef=new ParticleDef(getXMin(),getXMax(),getYMin(), getYMax()); plusDef.minCharge=plusDef.maxCharge=10; plusDef.minMass=plusDef.maxMass=0.5f; ParticleDef minusDef=new ParticleDef(getXMin(),getXMax(),getYMin(), getYMax()); minusDef.minCharge=minusDef.maxCharge=-5; minusDef.minMass=minusDef.maxMass=0.01f; for (int i = 0; i < 100; i++) { Particle plusParticle = this.generateRandomParticle(plusDef); Particle minusParticle = this.generateRandomParticle(minusDef); DistanceJointDef jointDef=new DistanceJointDef(); jointDef.length=5*(plusParticle.getRadius()+minusParticle.getRadius()); jointDef.bodyA=plusParticle.getBody(); jointDef.bodyB=minusParticle.getBody(); jointDef.frequencyHz=1f; this.getWorld().createJoint(jointDef); this.addParticle(plusParticle); this.addParticle(minusParticle); } this.setLevelProceeder(new LevelProceeder() { @Override public void proceed(Level level, float delta) { GridLevel.this.interactAllWithAllParticles(); } }); }
/** * Creates the joint. * @return This joint. Handy for chaining stuff together. */ @Override public B2FlxDistanceJoint create() { ((DistanceJointDef) jointDef).initialize(bodyA, bodyB, anchorA, anchorB); joint = B2FlxB.world.createJoint(jointDef); return this; }
/** * When a hero collides with a "sticky" obstacle, this figures out what to do * * @param sticky The sticky actor... it should always be an obstacle for now * @param other The other actor... it should always be a hero for now * @param contact A description of the contact event */ private void handleSticky(final WorldActor sticky, final WorldActor other, Contact contact) { // don't create a joint if we've already got one if (other.mDJoint != null) return; // don't create a joint if we're supposed to wait if (System.currentTimeMillis() < other.mStickyDelay) return; // go sticky obstacles... only do something if we're hitting the // obstacle from the correct direction if ((sticky.mIsSticky[0] && other.getYPosition() >= sticky.getYPosition() + sticky.mSize.y) || (sticky.mIsSticky[1] && other.getXPosition() + other.mSize.x <= sticky.getXPosition()) || (sticky.mIsSticky[3] && other.getXPosition() >= sticky.getXPosition() + sticky.mSize.x) || (sticky.mIsSticky[2] && other.getYPosition() + other.mSize.y <= sticky.getYPosition())) { // create distance and weld joints... somehow, the combination is needed to get this to // work. Note that this function runs during the box2d step, so we need to make the // joint in a callback that runs later final Vector2 v = contact.getWorldManifold().getPoints()[0]; mOneTimeEvents.add(new LolAction() { @Override public void go() { other.mBody.setLinearVelocity(0, 0); DistanceJointDef d = new DistanceJointDef(); d.initialize(sticky.mBody, other.mBody, v, v); d.collideConnected = true; other.mDJoint = (DistanceJoint) mWorld.createJoint(d); WeldJointDef w = new WeldJointDef(); w.initialize(sticky.mBody, other.mBody, v); w.collideConnected = true; other.mWJoint = (WeldJoint) mWorld.createJoint(w); } }); } }
@Override public void reset() { jointDef = new DistanceJointDef(); }
public static Entity createBall(World world, com.badlogic.gdx.physics.box2d.World box2dWorld, float x, float y) { Entity e = world.createEntity(); e.addComponent(new Position(x, y)); e.addComponent(new Size(15, 15)); e.addComponent(new Rotation()); e.addComponent(new Sprite("airbase_ball")); e.addComponent(new Effect("effect")); world.getManager(GroupManager.class).add(e, Constants.EFFECTS); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DynamicBody; bodyDef.position.set(x / 10, y / 10); Body body = box2dWorld.createBody(bodyDef); body.setUserData(e); // body.applyForce(100, 100, 1, 1); body.applyLinearImpulse(3, 3, 1, 1); CircleShape shape = new CircleShape(); shape.setRadius(0.75f); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 1f; fixtureDef.friction = 0.1f; fixtureDef.restitution = 1; body.createFixture(fixtureDef); bodyDef.type = BodyType.StaticBody; bodyDef.position.set(x / 10, 9); Body body2 = box2dWorld.createBody(bodyDef); DistanceJointDef distanceJointDef = new DistanceJointDef(); distanceJointDef.bodyA = body; distanceJointDef.bodyB = body2; distanceJointDef.length = 9 - y / 10; distanceJointDef.dampingRatio = 0.1f; distanceJointDef.frequencyHz = 0.6f; box2dWorld.createJoint(distanceJointDef); return e; }
/** * Set the frequencyHz. * @param frequencyHz * @return This joint. Handy for chaining stuff together. */ public B2FlxDistanceJoint setFrequencyHz(float frequencyHz) { ((DistanceJointDef) jointDef).frequencyHz = frequencyHz; return this; }
/** * Set the damping ratio. * @param dampingRatio * @return This joint. Handy for chaining stuff together. */ public B2FlxDistanceJoint setDampingRatio(float dampingRatio) { ((DistanceJointDef) jointDef).dampingRatio = dampingRatio; return this; }
private long createProperJoint(JointDef paramJointDef) { if (paramJointDef.type == JointDef.JointType.DistanceJoint) { DistanceJointDef localDistanceJointDef = (DistanceJointDef)paramJointDef; return jniCreateDistanceJoint(this.addr, localDistanceJointDef.bodyA.addr, localDistanceJointDef.bodyB.addr, localDistanceJointDef.collideConnected, localDistanceJointDef.localAnchorA.x, localDistanceJointDef.localAnchorA.y, localDistanceJointDef.localAnchorB.x, localDistanceJointDef.localAnchorB.y, localDistanceJointDef.length, localDistanceJointDef.frequencyHz, localDistanceJointDef.dampingRatio); } if (paramJointDef.type == JointDef.JointType.FrictionJoint) { FrictionJointDef localFrictionJointDef = (FrictionJointDef)paramJointDef; return jniCreateFrictionJoint(this.addr, localFrictionJointDef.bodyA.addr, localFrictionJointDef.bodyB.addr, localFrictionJointDef.collideConnected, localFrictionJointDef.localAnchorA.x, localFrictionJointDef.localAnchorA.y, localFrictionJointDef.localAnchorB.x, localFrictionJointDef.localAnchorB.y, localFrictionJointDef.maxForce, localFrictionJointDef.maxTorque); } if (paramJointDef.type == JointDef.JointType.GearJoint) { GearJointDef localGearJointDef = (GearJointDef)paramJointDef; return jniCreateGearJoint(this.addr, localGearJointDef.bodyA.addr, localGearJointDef.bodyB.addr, localGearJointDef.collideConnected, localGearJointDef.joint1.addr, localGearJointDef.joint2.addr, localGearJointDef.ratio); } if (paramJointDef.type == JointDef.JointType.MouseJoint) { MouseJointDef localMouseJointDef = (MouseJointDef)paramJointDef; return jniCreateMouseJoint(this.addr, localMouseJointDef.bodyA.addr, localMouseJointDef.bodyB.addr, localMouseJointDef.collideConnected, localMouseJointDef.target.x, localMouseJointDef.target.y, localMouseJointDef.maxForce, localMouseJointDef.frequencyHz, localMouseJointDef.dampingRatio); } if (paramJointDef.type == JointDef.JointType.PrismaticJoint) { PrismaticJointDef localPrismaticJointDef = (PrismaticJointDef)paramJointDef; return jniCreatePrismaticJoint(this.addr, localPrismaticJointDef.bodyA.addr, localPrismaticJointDef.bodyB.addr, localPrismaticJointDef.collideConnected, localPrismaticJointDef.localAnchorA.x, localPrismaticJointDef.localAnchorA.y, localPrismaticJointDef.localAnchorB.x, localPrismaticJointDef.localAnchorB.y, localPrismaticJointDef.localAxisA.x, localPrismaticJointDef.localAxisA.y, localPrismaticJointDef.referenceAngle, localPrismaticJointDef.enableLimit, localPrismaticJointDef.lowerTranslation, localPrismaticJointDef.upperTranslation, localPrismaticJointDef.enableMotor, localPrismaticJointDef.maxMotorForce, localPrismaticJointDef.motorSpeed); } if (paramJointDef.type == JointDef.JointType.PulleyJoint) { PulleyJointDef localPulleyJointDef = (PulleyJointDef)paramJointDef; return jniCreatePulleyJoint(this.addr, localPulleyJointDef.bodyA.addr, localPulleyJointDef.bodyB.addr, localPulleyJointDef.collideConnected, localPulleyJointDef.groundAnchorA.x, localPulleyJointDef.groundAnchorA.y, localPulleyJointDef.groundAnchorB.x, localPulleyJointDef.groundAnchorB.y, localPulleyJointDef.localAnchorA.x, localPulleyJointDef.localAnchorA.y, localPulleyJointDef.localAnchorB.x, localPulleyJointDef.localAnchorB.y, localPulleyJointDef.lengthA, localPulleyJointDef.lengthB, localPulleyJointDef.ratio); } if (paramJointDef.type == JointDef.JointType.RevoluteJoint) { RevoluteJointDef localRevoluteJointDef = (RevoluteJointDef)paramJointDef; return jniCreateRevoluteJoint(this.addr, localRevoluteJointDef.bodyA.addr, localRevoluteJointDef.bodyB.addr, localRevoluteJointDef.collideConnected, localRevoluteJointDef.localAnchorA.x, localRevoluteJointDef.localAnchorA.y, localRevoluteJointDef.localAnchorB.x, localRevoluteJointDef.localAnchorB.y, localRevoluteJointDef.referenceAngle, localRevoluteJointDef.enableLimit, localRevoluteJointDef.lowerAngle, localRevoluteJointDef.upperAngle, localRevoluteJointDef.enableMotor, localRevoluteJointDef.motorSpeed, localRevoluteJointDef.maxMotorTorque); } if (paramJointDef.type == JointDef.JointType.WeldJoint) { WeldJointDef localWeldJointDef = (WeldJointDef)paramJointDef; return jniCreateWeldJoint(this.addr, localWeldJointDef.bodyA.addr, localWeldJointDef.bodyB.addr, localWeldJointDef.collideConnected, localWeldJointDef.localAnchorA.x, localWeldJointDef.localAnchorA.y, localWeldJointDef.localAnchorB.x, localWeldJointDef.localAnchorB.y, localWeldJointDef.referenceAngle); } if (paramJointDef.type == JointDef.JointType.RopeJoint) { RopeJointDef localRopeJointDef = (RopeJointDef)paramJointDef; return jniCreateRopeJoint(this.addr, localRopeJointDef.bodyA.addr, localRopeJointDef.bodyB.addr, localRopeJointDef.collideConnected, localRopeJointDef.localAnchorA.x, localRopeJointDef.localAnchorA.y, localRopeJointDef.localAnchorB.x, localRopeJointDef.localAnchorB.y, localRopeJointDef.maxLength); } if (paramJointDef.type == JointDef.JointType.WheelJoint) { WheelJointDef localWheelJointDef = (WheelJointDef)paramJointDef; return jniCreateWheelJoint(this.addr, localWheelJointDef.bodyA.addr, localWheelJointDef.bodyB.addr, localWheelJointDef.collideConnected, localWheelJointDef.localAnchorA.x, localWheelJointDef.localAnchorA.y, localWheelJointDef.localAnchorB.x, localWheelJointDef.localAnchorB.y, localWheelJointDef.localAxisA.x, localWheelJointDef.localAxisA.y, localWheelJointDef.enableMotor, localWheelJointDef.maxMotorTorque, localWheelJointDef.motorSpeed, localWheelJointDef.frequencyHz, localWheelJointDef.dampingRatio); } return 0L; }
/** * Creates a distance joint. * @param spriteA The first body. * @param spriteB The second body. * @param jointDef The joint definition. */ public B2FlxDistanceJoint(B2FlxShape spriteA, B2FlxShape spriteB, DistanceJointDef jointDef) { super(spriteA, spriteB, jointDef); }