Ejemplo n.º 1
0
    // Handles all ball-ball collisions
    void handleBallBallCollisions() {

      for (int i = 0; i < _scene.numChildren(); i++)
        for (int j = 0; j < _scene.numChildren(); j++) {

          if (i == j) continue;

          Object3d b1 = _scene.getChildAt(i);
          Object3d b2 = _scene.getChildAt(j);

          // if (b1 == _room || b2 == _room)
          // continue;

          if (testBallBallCollision(b1, b2)) {
            // Make the balls reflect off of each other

            Number3d displacement = Number3d.subtract(b1.position(), b2.position());
            displacement.normalize();
            float objDot = Number3d.dot(b1.velocity(), displacement);
            displacement.multiply(objDot);
            displacement.multiply(2f);
            b1.velocity().subtract(displacement);

            displacement = Number3d.subtract(b1.position(), b2.position());
            displacement.normalize();
            objDot = Number3d.dot(b2.velocity(), displacement);
            displacement.multiply(objDot);
            displacement.multiply(2f);
            b2.velocity().subtract(displacement);
          }
        }
    }
Ejemplo n.º 2
0
    @Override
    public void onTouchEvent(MotionEvent e) {
      float x = e.getX();
      float y = e.getY();
      switch (e.getAction()) {
        case MotionEvent.ACTION_UP:
          x = x * .001f;
          y = y * .001f;
          for (int i = 0; i < _scene.numChildren(); i++) {
            Object3d obj = _scene.getChildAt(i);

            if (obj == _up || obj == _down || obj == _east || obj == _west) continue;

            // TODO: make pressure equal to distance from touch

            obj.velocity().z = -(e.getPressure() * 100);
            obj.velocity().z = -(e.getPressure() * 100);
          }
          break;
        case MotionEvent.ACTION_MOVE:
          float dx = x - mPreviousX;
          float dy = y - mPreviousY;
          for (int i = 0; i < _scene.numChildren(); i++) {
            Object3d obj = _scene.getChildAt(i);

            if (obj == _up || obj == _down || obj == _east || obj == _west) continue;

            obj.rotation().x += dx * Object3d.TOUCH_SCALE_FACTOR;
            obj.rotation().y += dy * Object3d.TOUCH_SCALE_FACTOR;
          }
          break;
      }
      mPreviousX = x;
      mPreviousY = y;
    }
Ejemplo n.º 3
0
    public void onDrawFrame(GL10 gl) {

      double time = System.currentTimeMillis();
      double elapsed = time - _lastDraw;
      _lastDraw = time;

      if (elapsed > 1000) elapsed = 60;

      elapsed = elapsed / 1000;

      float pitch = _sensor.getPitch();
      float roll = _sensor.getRoll();

      for (int i = 0; i < _scene.numChildren(); i++) {
        Object3d obj = _scene.getChildAt(i);
        if (obj != _down && obj != _up && obj != _east && obj != _west) {
          updateObject(obj, elapsed, _sensor.getAccelerometer(), roll, pitch);
        }
      }

      _renderer.onDrawFrame(gl);
    }
Ejemplo n.º 4
0
  public static void loadBackGroundImage(Scene scene) {
    if (background == null) {
      IParser parser =
          Parser.createParser(
              Parser.Type.OBJ, Shared.context().getResources(), "com.knb:raw/background_obj", true);
      parser.parse();
      background = parser.getParsedObject();
      background.scale().x = background.scale().y = background.scale().z = SBStore.BACKGROUND_SCALE;
      background.position().z = -1.99f;
    }

    if (scene != null) {

      scene.addChild(background);
    }
  }
Ejemplo n.º 5
0
    public void initScene() {
      _scene.backgroundColor().setAll(_backColor);

      _scene.lights().add(new Light());

      Bitmap b = null;
      switch (_wallTexture) {
        case 1:
          b = Utils.makeBitmapFromResourceId(R.drawable.wood);
          break;
        case 2:
          b = Utils.makeBitmapFromResourceId(R.drawable.checkerboard);
          break;
        case 3:
          b = Utils.makeBitmapFromResourceId(R.drawable.gray_waves);
          break;
        case 4:
          b = Utils.makeBitmapFromResourceId(R.drawable.paper);
          break;
      }
      if (b != null) {
        Shared.textureManager().addTextureId(b, "wall", false);
        b.recycle();

        Color4 planeColor = new Color4(255, 255, 255, 255);
        _east = new Rectangle(40, 12, 2, 2, planeColor);
        _west = new Rectangle(40, 12, 2, 2, planeColor);
        _up = new Rectangle(40, 12, 2, 2, planeColor);
        _down = new Rectangle(40, 12, 2, 2, planeColor);

        _east.position().x = -6;
        _east.rotation().y = -90;
        _east.position().z = -20;
        _east.lightingEnabled(false);
        _east.textures().addById("wall");

        _west.position().x = 6;
        _west.rotation().y = 90;
        _west.position().z = -20;
        _west.lightingEnabled(false);
        _west.textures().addById("wall");

        _up.rotation().x = -90;
        _up.rotation().z = 90;
        _up.position().y = 6;
        _up.position().z = -20;
        _up.lightingEnabled(false);
        _up.textures().addById("wall");

        _down.rotation().x = 90;
        _down.rotation().z = 90;
        _down.position().y = -6;
        _down.position().z = -20;
        _down.lightingEnabled(false);
        _down.textures().addById("wall");

        _scene.addChild(_east);
        _scene.addChild(_west);
        _scene.addChild(_up);
        _scene.addChild(_down);
      }

      _scene.fogColor(new Color4(0, 0, 0, 255));
      _scene.fogNear(10);
      _scene.fogFar(40);
      _scene.fogEnabled(true);

      if (_theme != 2) {
        // not rainbow

        _light = new Light();
        _light.type(LightType.POSITIONAL);
        _light.position.setZ(10);
        _light.direction.z = -100;
        _scene.lights().add(_light);

        if (_theme == 0) {
          _light.ambient.setAll(_ballColor);
          _light.diffuse.setAll(_ballColor);
        }
      }

      TextureVo t = null;
      if (_theme >= 3) {
        b = null;
        if (_theme == 3) b = Utils.makeBitmapFromResourceId(Shared.context(), R.drawable.earth);
        if (_theme == 4)
          b = Utils.makeBitmapFromResourceId(Shared.context(), R.drawable.beach_ball);
        if (_theme == 5) b = Utils.makeBitmapFromResourceId(Shared.context(), R.drawable.smiley);
        if (_theme == 6) b = Utils.makeBitmapFromResourceId(Shared.context(), R.drawable.google);
        if (_theme == 7)
          b = Utils.makeBitmapFromResourceId(Shared.context(), R.drawable.checkerboard);
        Shared.textureManager().addTextureId(b, "texture", false);
        b.recycle();
        t = new TextureVo("texture");
      }

      Random r = new Random();
      for (int i = 0; i < _ballCount; i++) {

        Sphere s = new Sphere((float) (_ballSize * .001), 10, 10);

        if (t != null) s.textures().add(t);

        if (_style == 0) s.renderType(RenderType.TRIANGLES);
        if (_style == 1) s.renderType(RenderType.POINTS);
        else if (_style == 2) s.renderType(RenderType.LINES);
        else if (_style == 3) s.renderType(RenderType.LINE_LOOP);
        else if (_style == 4) s.renderType(RenderType.LINE_STRIP);
        else if (_style == 5) s.renderType(RenderType.TRIANGLE_STRIP);
        else if (_style == 6) s.renderType(RenderType.TRIANGLE_FAN);

        s.position().x = r.nextFloat();
        s.position().y = r.nextFloat();
        s.position().z = r.nextInt(15) * -1;

        s.rotation().x = r.nextInt(100);
        s.rotation().y = r.nextInt(100);

        if (_theme == 2) {
          s.colorMaterialEnabled(true);
          s.vertexColorsEnabled(true);
        } else {
          s.vertexColorsEnabled(false);
        }

        _scene.addChild(s);
      }
    }