Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX Othographic Camera Problem  (Read 6039 times)
0 Members and 1 Guest are viewing this topic.
Offline Aboogesnickle

Senior Newbie


Exp: 1 month or less



« Posted 2014-07-31 15:53:09 »

So I keep getting a null pointer exception and i have no clue why. For some reason cam is null, but i initialize it the same way i do ScaledCam and it works fine. I think something is messed up from the the path it takes from the Main class to Play class, but i cant find it

this is the Main class were i initialize the cam
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
public class Main extends ApplicationAdapter {
   public static final String Title = "Mead Walla";
   public static final int Width = 360;
   public static final int Height = 240;
   public static final int Scale = 2;

   private SpriteBatch sb;
   private OrthographicCamera cam;
   private OrthographicCamera hudCam;

   private GameStateHandler gsh;
   
   public static ContentHandler res;

   public void create() {
      //initialize handlers
      res = new ContentHandler();
      gsh = new GameStateHandler(this);
     
      //load content
      try {
         res.loadTexture("player/testPlayer.png", "testPlayer");
      }
      catch(Exception e) {
         System.out.println("Cannot find file: testPlayer");
         Gdx.app.exit();
      }
     
      //initialize LibGDX elements
      sb = new SpriteBatch();
      cam = new OrthographicCamera();
      cam.setToOrtho(false, Width, Height);
      hudCam = new OrthographicCamera();
      hudCam.setToOrtho(false, Width, Height);
      Gdx.input.setInputProcessor(new InputHandler());
   }

   public void render() {
      Gdx.graphics.setTitle(Title + " -- FPS: " + Gdx.graphics.getFramesPerSecond());
      gsh.update(Gdx.graphics.getDeltaTime());
      gsh.render();
      InputHandler.update();
     
      sb.setProjectionMatrix(hudCam.combined);
      sb.begin();
      sb.draw(res.getTexture("testPlayer"), 0, 0);
      sb.end();
   }

   public SpriteBatch getSpriteBatch() {
      return sb;
   }

   public OrthographicCamera getCam() {
      return cam;
   }

   public OrthographicCamera getHudCam() {
      return hudCam;
   }


the state class should get the cam from main and set it to the cam in state

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public abstract class State {
   protected GameStateHandler gsh;
   protected Main game;

   protected SpriteBatch sb;
   protected OrthographicCamera cam;
   protected OrthographicCamera hudCam;

   protected State(GameStateHandler gsh) {
      this.gsh = gsh;
      game = gsh.getGame();
      sb = game.getSpriteBatch();
      cam = game.getCam();
      hudCam = game.getHudCam();
   }

   public abstract void handleInput();
   public abstract void update(float dt);
   public abstract void render();
   public abstract void dispose();
}


this is the play class were i try to use the cam to render the TileMap but when i get to setView(cam) its null, if i use ScaledCam it runs its just not the right view

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
public class Play extends State {
   //Box2D elements
   private World world;
   private BodyDef bdef;
   private Body body;
   private Body boxBody;
   private FixtureDef fdef;
   private PolygonShape shape;
   private Box2DDebugRenderer b2dr;
   private OrthographicCamera ScaledCam;
   private TiledMap tileMap;
   private OrthogonalTiledMapRenderer tmr;

   //Box2D variables
   private static float ppm = B2D.PPM;
   private static float gravityX = 0;
   private static float gravityY = -9.81f;

   public Play(GameStateHandler gsh) {
      super(gsh);
      //initialize elements
      world = new World(new Vector2(gravityX, gravityY), true);
      bdef = new BodyDef();
      fdef = new FixtureDef();
      shape = new PolygonShape();
      b2dr = new Box2DDebugRenderer();
      ScaledCam = new OrthographicCamera();
      ScaledCam.setToOrtho(false, Main.Width / ppm, Main.Height / ppm);
      world.setContactListener(new ContactHandler());

      //test platform body definition
      bdef.position.set(160 / ppm, 120 / ppm);
      bdef.type = BodyType.StaticBody;
      body = world.createBody(bdef);

      //test platform fixture definition
      shape.setAsBox(50 / ppm, 5 / ppm);
      fdef.shape = shape;
      fdef.filter.categoryBits = B2D.Bit_One;
      fdef.filter.maskBits = B2D.Bit_Two;
      body.createFixture(fdef).setUserData("platform");

      //test box body definition
      bdef.position.set(160 / ppm, 200 / ppm);
      bdef.type = BodyType.DynamicBody;
      boxBody = world.createBody(bdef);

      //test box fixture definition
      shape.setAsBox(5 / ppm, 5 / ppm);
      fdef.shape = shape;
      fdef.filter.categoryBits = B2D.Bit_Two;
      fdef.filter.maskBits = B2D.Bit_One;
      boxBody.createFixture(fdef).setUserData("box");
     
      //test box sensor
      shape.setAsBox(2 / ppm, 2 / ppm, new Vector2(0, -5 / ppm), 0);
      //set to same stuff between shape and create in player
      fdef.isSensor = true;
      boxBody.createFixture(fdef).setUserData("sensor");
     
      //load tile map
      try {
         tileMap = new TmxMapLoader().load("map/test.tmx");
      }
      catch(Exception e) {
         System.out.println("Cannot find file: map/test.tmx");
         Gdx.app.exit();
      }
      tmr = new OrthogonalTiledMapRenderer(tileMap);
   }

   public void update(float dt) {
      handleInput();
      world.step(dt, 6, 2);
   }

   public void render() {
      //clear screen
      Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

      //draw tile map
      tmr.setView(cam);
      tmr.render();

      //draw box2d world
      b2dr.render(world, ScaledCam.combined);
   }

   public void handleInput() {}

   public void dispose() {}
}


I can not find why its null. any help would be appreciated.
Offline songangel

Junior Devvie


Medals: 1
Exp: 4 years



« Reply #1 - Posted 2014-12-17 21:27:33 »

From what I can see, you initialize the cam in your Main class AFTER initializing the GameStateHandler.  If the GameStateHandler creates and intializes the Play state, then your call to getCam() would return null because the Main class had not initialized the camera yet!  Your posted code doesn't show where and when the Play state is set up but I'm betting that's it.

Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (21 views)
2017-11-21 16:21:00

xxMrPHDxx (14 views)
2017-11-21 16:14:31

xxMrPHDxx (16 views)
2017-11-21 16:10:57

Ecumene (114 views)
2017-09-30 02:57:34

theagentd (150 views)
2017-09-26 18:23:31

cybrmynd (258 views)
2017-08-02 12:28:51

cybrmynd (249 views)
2017-08-02 12:19:43

cybrmynd (247 views)
2017-08-02 12:18:09

Sralse (260 views)
2017-07-25 17:13:48

Archive (878 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!