Exemple #1
0
  @Override
  public void create() {
    Bullet.init();

    modelBatch = new ModelBatch();
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
    environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(3f, 7f, 10f);
    cam.lookAt(0, 4f, 0);
    cam.update();

    camController = new CameraInputController(cam);
    Gdx.input.setInputProcessor(camController);

    ModelBuilder mb = new ModelBuilder();
    mb.begin();
    mb.node().id = "ground";
    mb.part(
            "box",
            GL20.GL_TRIANGLES,
            Usage.Position | Usage.Normal,
            new Material(ColorAttribute.createDiffuse(Color.RED)))
        .box(5f, 1f, 5f);
    mb.node().id = "ball";
    mb.part(
            "sphere",
            GL20.GL_TRIANGLES,
            Usage.Position | Usage.Normal,
            new Material(ColorAttribute.createDiffuse(Color.GREEN)))
        .sphere(1f, 1f, 1f, 10, 10);
    model = mb.end();

    ground = new ModelInstance(model, "ground");
    ball = new ModelInstance(model, "ball");
    ball.transform.setToTranslation(0, 9f, 0);

    instances = new Array<ModelInstance>();
    instances.add(ground);
    instances.add(ball);

    ballShape = new btSphereShape(0.5f);
    groundShape = new btBoxShape(new Vector3(2.5f, 0.5f, 2.5f));

    groundObject = new btCollisionObject();
    groundObject.setCollisionShape(groundShape);
    groundObject.setWorldTransform(ground.transform);

    ballObject = new btCollisionObject();
    ballObject.setCollisionShape(ballShape);
    ballObject.setWorldTransform(ball.transform);

    collisionConfig = new btDefaultCollisionConfiguration();
    dispatcher = new btCollisionDispatcher(collisionConfig);
  }
Exemple #2
0
  public void update(float delta) {
    // tmp.set(acceleration);
    // tmp.scl(delta);
    // velocity.add(tmp);
    // velocity = velocity.scl(0.95f);
    tmp.set(velocity);
    tmp.scl(delta);
    position.add(tmp);

    transform.setTranslation(position);
    collisionObject.setWorldTransform(transform);
  }
Exemple #3
0
  public Projectile(
      Vector3 startingPosition, Vector3 direction, float msSpeed, btCollisionWorld world) {
    btCollisionShape sphere = new btSphereShape(0.5f);
    collisionObject = new btCollisionObject();
    collisionObject.setCollisionShape(sphere);
    collisionObject.userData = this;
    world.addCollisionObject(collisionObject);

    position = new Vector3(startingPosition);
    /*
    acceleration = new Vector3(direction);
    acceleration.nor().scl(msSpeed);*/

    velocity = new Vector3(direction);
    velocity.nor().scl(msSpeed);
    transform = new Matrix4();
    transform.setTranslation(position);
    collisionObject.setWorldTransform(transform);
  }
Exemple #4
0
  @Override
  public void render() {
    final float delta = Math.min(1f / 30f, Gdx.graphics.getDeltaTime());

    if (!collision) {
      ball.transform.translate(0f, -delta, 0f);
      ballObject.setWorldTransform(ball.transform);

      collision = checkCollision();
    }

    camController.update();

    Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1.f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    modelBatch.begin(cam);
    modelBatch.render(instances, environment);
    modelBatch.end();
  }