2

I am developing a 3d first person shooter game in javaFx8. I am having trouble with 3DShapes which I use as walls in my scene. There is this weird effect where I can see through a wall the other walls behind.

I noticed that the order I add the walls in my group does affect the effect.
It seems that the last wall in the list behaves normally and stays opaque when the player camera point through it, but if that same wall is first in the list it will be transparent.

Enabling the depthBuffer of the scene did not help, as I understand this is for overlaping shapes which is not my case. Maybe I am missing something. Any help would be much appreciated.

Illustration of the problem:

enter image description here

public class Display extends Application {
  private final int WIDTH = 1920;
  private final int HEIGHT = 1080;
  private Group stage;
  private MyCamera camera;
  private Obstacle platform;
  private Player player;
  private Scene scene;

  @Override
  public void start(Stage GameWorld) {
    platform = new Obstacle(7000, 7000, 0, 0, 0, 0);
    player = new Player(100, -3000, 0, 92.0);
    camera = new MyCamera(player);
    initStage();
    initScene();
    initKeyListner(scene);
    initMouseControl(stage, scene);
    GameWorld.setScene(scene);
    GameWorld.show();
  }

  private void initScene(){
    scene = new Scene(stage, WIDTH, HEIGHT, true, SceneAntialiasing.BALANCED);// True allows 3D features
    scene.setFill(Color.WHITE);
    scene.setCamera(camera.getCamera());
  }

  private void initStage(){
    stage = new Group();
    stage.getChildren().add(new AmbientLight(Color.WHITE));
    stage.getChildren().add(platform.getBody());
    stage.getChildren().add(player.getBody());

    Obstacle wall1 = new Obstacle(60, 7000, 200,3500, 0, GROUND_Z);
    Obstacle wall2 = new Obstacle(7000, 60, 200,10, -3500, GROUND_Z);
    Obstacle wall3 = new Obstacle(60, 7000, 200,-3500, 0, GROUND_Z);
    Obstacle wall4 = new Obstacle(7000, 60, 200,10, 3500, GROUND_Z);
    Obstacle wall5 = new Obstacle(600, 2500, 200,-2000, 1600, GROUND_Z);
    Obstacle wall6 = new Obstacle(600, 2500, 200,2000, 1600, GROUND_Z);
    Obstacle wall7 = new Obstacle(600, 2500, 200,2000, -1600, GROUND_Z);
    Obstacle wall8 = new Obstacle(600, 2500, 200,-2000, -1600, GROUND_Z);

    Group groupWall = new Group(wall1.getBody(), wall2.getBody(), wall3.getBody(),
            wall4.getBody(), wall6.getBody(),wall5.getBody(), wall7.getBody(), wall8.getBody());

    groupWall.setId("walls");
    stage.getChildren().add(groupWall);
  }

  public static void main(String[] args) {
    Application.launch(args);
  }

}

public class MyCamera {
    private Camera camera;

    public MyCamera(Player player){
        camera = new PerspectiveCamera(true);
        init(player);
    }

    public void init(Player player){
            camera.getTransforms().addAll(new Translate(0, player.getSize()/2, -30), new Rotate(90, Rotate.X_AXIS)); 
        camera.setFarClip(6000); 
        camera.setNearClip(0.01);  
    }
    public Camera getCamera(){
        return camera;
    }

}

public class Obstacle {
    public Box obstacle;

    public Obstacle(double w, double h, double depth,
                    double xt, double yt, double zt){
        obstacle = new Box(w, h, depth);
        setTranslation(xt, yt, zt);
    }

    public void setTranslation(double x, double y, double z){
        obstacle.setTranslateX(x);
        obstacle.setTranslateY(y);
        obstacle.setTranslateZ(z);
    }

    public Box getBody(){
        return obstacle;
    }
}

xavier
  • 21
  • 3
  • Hard to say what is going on with all your custom classes and resources. For starters don't set `camera.setNearClip(0)`, but `camera.setNearClip(0.01)` or alike. Do you use transparency in your resources? – José Pereda May 01 '19 at 10:36
  • There is no transparent layer in the texture file. If I use solid colours instead of a texture for the walls and ground, it does not fix the issue. – xavier May 01 '19 at 11:04
  • Did you fix the near clip and check? – José Pereda May 01 '19 at 11:05
  • yes I tried 0.01 and 0.1 but no success. – xavier May 01 '19 at 11:07
  • You must enable depth buffer as well as near clip > 0. Please update the post with those changes, and verify the issue still persist. – José Pereda May 01 '19 at 11:09
  • it's working. I had to enable the depth buffer and set the near clip value >0 and play with the far clip value a little bit. Thanks! – xavier May 01 '19 at 11:26

0 Answers0