Exemple #1
0
  @Override
  public void onInit() {
    /* Get time */
    TimeFloat time = new TimeFloat();

    /* Set title */
    setTitle("Simple rotate gears demo, pim 2015");

    /* Create a debug renderer */
    debugRenderer = new DebugRenderer();

    /* Load images */
    bitmapClock = new BitmapImage("data/images/clock.png");
    bitmapSecond = new BitmapImage("data/images/clock_second.png");
    bitmapMinute = new BitmapImage("data/images/clock_minute.png");
    bitmapHour = new BitmapImage("data/images/clock_hour.png");

    /* Create the frame, the motors will be attached to it */
    PhysicsStaticBox frame = new PhysicsStaticBox("frame", CLOCK_CENTER, 10, 10);

    /* Create the hands, at the current system time */
    hand_second = new PhysicsBox("seconds", CLOCK_CENTER, 10, 50, time.getSecondAngle());
    hand_minute = new PhysicsBox("minutes", CLOCK_CENTER, 10, 40, time.getMinuteAngle());
    hand_hour = new PhysicsBox("hours", CLOCK_CENTER, 10, 30, time.getHourAngle());

    /* Prevent collision between hands */
    hand_second.setCollisionGroup(-2);
    hand_minute.setCollisionGroup(-2);
    hand_hour.setCollisionGroup(-2);

    /* Create the motors */
    motor_seconds =
        new PhysicsMotor(frame.getBody(), hand_second.getBody(), frame.getBody().getWorldCenter());

    motor_minutes =
        new PhysicsMotor(frame.getBody(), hand_minute.getBody(), frame.getBody().getWorldCenter());

    /*
     * This motor will only be used as a skeleton for the gear.
     */
    PhysicsMotor motor_m2h =
        new PhysicsMotor(frame.getBody(), hand_hour.getBody(), frame.getBody().getWorldCenter());

    /*
     * Create the gear between the minute hand and the hour hand
     */
    GearJointDef gear_m2h = new GearJointDef();

    /* Do the connections */
    gear_m2h.bodyA = hand_minute.getBody();
    gear_m2h.bodyB = hand_hour.getBody();
    gear_m2h.joint1 = motor_minutes.getJoint();
    gear_m2h.joint2 = motor_m2h.getJoint();

    /*
     * Negative ratio because the minute and hour hand rotates the same
     * direction
     */
    gear_m2h.ratio = -60;
    world.createJoint(gear_m2h);

    /*
     * Start the clock, the second hand is running and the minute hand is
     * stopped
     */
    motor_seconds.initializeMotor(MOTOR_SPEED_SECOND, 1.0f, true);
    motor_minutes.initializeMotor(0.0f, 1.0f, true);

    System.out.println("click to switch debug/rendering mode");
  }