Crawling FPS when trying to create my own classes

I’m trying to slowly make the switch from C-Style programming to OOP, so forgive me if this complaint is painfully obvious. I wanted to try using my own classes to create elements of the world. I can’t help but feel like I’m still imagining everything to be too function-based but I wanted something essentially like createTree(x,y,z). So, for testing I did createBox(material) instead, and put it under a ‘world’ class: world.createBox(material).



Doing this made my FPS drop from 50-60 to 3-4.



[java]

public class Main extends SimpleApplication {



public static void main(String[] args) {

Main app = new Main();

app.start();

}



@Override

public void simpleInitApp() {

Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

Geometry box1 = world.createBox(mat);



rootNode.attachChild(box1);

}

}

[/java]



The above code is part of the default Main.java that you get when creating a new project in the SDK. The following is my class, it is found in the same destination as Main.java:



[java]

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

    */

    package mygame;



    /**

    *
  • @author Lou

    */



    import com.jme3.material.Material;

    import com.jme3.math.ColorRGBA;

    import com.jme3.math.Vector3f;

    import com.jme3.scene.Geometry;

    import com.jme3.scene.shape.Box;





    public class world {

    public static Geometry createBox(Material mat){



    Box b = new Box(Vector3f.ZERO, 1, 1, 1);

    Geometry geom = new Geometry("Box", b);





    mat.setColor("Color", ColorRGBA.Blue);

    geom.setMaterial(mat);



    return geom;



    }



    }

    [/java]



    Am I using classes wrong?



    I should add that the rendered result is the same.

If your methods are ‘static’, and you access them as such, then you are still sort of doing functional programming. Think of the “World” as an actual object you want to keep interacting with: it will hold state that you might want to access later.



As for your FPS drop, nothing you are doing there should really slow down the app. Especially if you are displaying just one box. Is there any other code you have there that you haven’t shown? Also, what are the specs of your machine?

Full main.java:



[java]

package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.renderer.RenderManager;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;



/**

  • test
  • @author normenhansen

    */

    public class Main extends SimpleApplication {



    public static void main(String[] args) {

    Main app = new Main();

    app.start();

    }



    @Override

    public void simpleInitApp() {

    Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

    Geometry box1 = world.createBox(mat);



    rootNode.attachChild(box1);

    }



    @Override

    public void simpleUpdate(float tpf) {

    //TODO: add update code

    }



    @Override

    public void simpleRender(RenderManager rm) {

    //TODO: add render code

    }

    }[/java]



    My computer is a bit dated and it’s currently running on-board graphics, geforce 6150 SE or something. I’m hoping to get a new card soon, but that still doesn’t really explain why what is essentially the same code, producing the same result, suddenly gets a reduced FPS. Or does it?



    Also, I haven’t been able to really find a useful metaphor to help explain the difference between static and non-static methods. It’s kind of foggy still.

Assuming you have provided us everything then there is nothing different between that code and the standard demo. How much performance difference are you seeing? If it is significant than there is some information we have not been provided.



re: static versus instance methods.



Static methods are not really any different than C functions. They just have longer names.



Instance methods operate on the instance of an object. They have access to the per-object data… ie: encapsulation.



Reading up on Object Oriented design could be beneficial.

How fast do the example test classes run on your machine?



A static method or variable belongs to the “class”, where non-static belongs to the instance of that class (where memory is allocated to create the object).

Calling “new MyClass()” will allocate memory for that class, and all non-static methods in that class operate on the local variables of that class. If you create two MyClass objects, the methods will work on each instance only.



Static methods can only access static variables of that class. So if you have a static variable “static Vector3f foo;” of type World, then no matter how many World objects you create, they will all be using the same foo variable, essentially sharing it. This can be dangerous in OOP.

Running the project now yields the appropriate results, this slowing-down has happened before and I think it may be on account of an out-dated graphics driver. In either case, jMonkey does not seem to be causing it.



So in order for non-static, I’ll need a constructor in my ‘world’ class?

All classes have a default no-arg constructor if you don’t specifically give them a constructor.



re: your performance issues, in case it is relevant. My system seems to fluctuate from about 230 FPS to 190 FPS depending on how hot my GPU/CPU/etc. are when I run it. Cool machine = fast run. Sometimes I have to let the machine sit a few minutes to test performance.

Java will create a default (empty parameter) constructor for you. But you can declare the class like this:

World myWorld = new World();



Definitely try out a couple starting java tutorials. I don’t want to accidently lead you down some wrong paths with incorrect terminology I might use.

If no constructor is there you basically get a non-parameter constructor for free, so you can go MyObject obj = new MyObject(); As soon as you do have a constructor with a parameter you need to add the empty one manually if you still want it (and sometimes you need to e.g. for serialization theres always an empty constructor needed).

@pspeed said:
Sometimes I have to let the machine sit a few minutes to test performance.

Paul needs this: http://hackedgadgets.com/wp-content/2/Mineral_Oil_Submerged_Computer_1.jpg
Placed in ice, on the north pole.

My old machine had an external water tower and I would drop cubes of ice in it sometimes during really long data processing runs.



The new one normally does alright but doing a full clean build of the JME project a few times in a row sends performance south unless I wait and/or crack a window open. :slight_smile:

Cool! Thanks guys, it’s looking a lot clearer. I’ve been watching some Java tutorial videos by aphonik on youtube and they seem like a great resource, any other suggestions?

http://www.javabeginner.com