Skip to content

Instantly share code, notes, and snippets.

View erlend-sh's full-sized avatar
🐠
Working on Fish Folk

Erlend Sogge Heggen erlend-sh

🐠
Working on Fish Folk
View GitHub Profile
@erlend-sh
erlend-sh / Timer or delay
Created December 4, 2012 13:38
jMonkeyEngine: Simple timer or delay using Timer.getTimeInSeconds()
/**
* Several forum topics in the past have asked about how to implement
* a timer or delay without using an Appstate or Control or an extra
* thread, which may be overkill for simple purposes such as delaying
* input into an Analog Listener (say, holding down a button to increment
* an integer value). I use getTimeInSeconds() here because getTime()
* returns ticks, and so you get unexpected behaviour like the timer
* counting 9.8 -> 9.9 -> 1.0 -> 1.1 etc.
*/
@erlend-sh
erlend-sh / WorldNode relevant code
Created December 4, 2012 13:42
jMonkeyEngine: WorldNode relevant code
public class WorldNode extends Node {
 
    @Override
    public void read(JmeImporter e) throws IOException {
        super.read(e);
        InputCapsule capsule=e.getCapsule(this);
        enemies=(Node) capsule.readSavable("Enemies", new Node());
        staticObjects=(Node) capsule.readSavable("StaticObjects", new Node());
        powerUps=(Node) capsule.readSavable("PowerUps", new Node());
        sky=(Geometry) capsule.readSavable("Sky", new Geometry());
@erlend-sh
erlend-sh / LittleLevelState
Created December 4, 2012 13:42
jMonkeyEngine: LittleLevelState
class LittleLevelState  implements Savable{
    private WorldNode littleWorld1;
    private WorldNode littleWorld2;
 
 
 
    public LittleLevelState(WorldNode littleWorld1, WorldNode littleWorld2) {      
        this.littleWorld1 = littleWorld1;
        this.littleWorld2 = littleWorld2;
         
@erlend-sh
erlend-sh / Pong
Created December 4, 2012 13:45
jMonkeyEngine: Pong - Something to collide with
public class Main extends SimpleApplication {
     
    public ArrayList<BaseEntity> entities = new ArrayList<BaseEntity>();
    public ArrayList<Spatial> blocks = new ArrayList<Spatial>();
     
    public Player player;
     
    public static void main(String[] args) {
        AppSettings settings= new AppSettings(true);
        settings.setTitle("jMonkey Pong 3D");
@erlend-sh
erlend-sh / Standard RGB To OpenGL conversion
Created December 4, 2012 13:46
jMonkeyEngine: Standard RGB To OpenGL conversion - Those two functions will convert RGB values in 0-255 format to OpenGL format (0-1.0 floats).
public static float[] RGBToOpenGL(int r1, int g1, int b1) {
    float[] tmp = new float[3];
    tmp[0] = r1/255f;
    tmp[1] = g1/255f;
    tmp[2] = b1/255f;
    return tmp;
}
public static float[] RGBAToOpenGL(int r1, int g1, int b1, int a1) {
    float[] tmp = new float[4];
    tmp[0] = r1/255f;
@erlend-sh
erlend-sh / Falling Vehicles
Created December 10, 2012 18:20
jMonkeyEngine: Falling Vehicles - SimpleApp with two falling vehicles and a falling cube. Illustrates tunneling through the terrain with ray cast vehicle.
public class Main extends SimpleApplication {
    static final float START_Y = 150f ;
 
    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }
 
    @Override
    public void simpleInitApp() {
@erlend-sh
erlend-sh / Pulsing Solution
Created December 10, 2012 18:23
jMonkeyEngine: Beginner Test 4 Pulsing Solution
private boolean scale = true ;
float timePassed = 0.0f;
  
   /* This is the update loop */
   @Override
   public void simpleUpdate(float tpf)
   {
      timePassed+= tpf;
 
      if (timePassed >= 0.3 ){timePassed=0; scale=(!scale); } //change decrease/increase every 0.3 sec.
@erlend-sh
erlend-sh / This will change all of faces that a material shows for the passed object
Created December 10, 2012 18:25
jMonkeyEngine (jME2): Set all MaterialFaces for an object (jME2)
    /**
 * Sets the MaterialFaces for a given object.  By using the applyToFront and
 * applyToBack parameters, each desired combination of face settings can be
 * produced (None, Front, Back, Both).
 * @param spatial The spatial for which to set the MaterialFace
 * @param applyToFront True if the front face should have the material applied
 * @param applyToBack True if the back face should have the material applied
 * @see MaterialFace
 */
public static void setMaterialFaceApplication(Spatial spatial, boolean applyToFront, boolean applyToBack){
@erlend-sh
erlend-sh / Modifying objects from another Thread
Created December 10, 2012 18:26
jMonkeyEngine: Enqueue a call in the OpenGL thread and get a return value from another thread (like the AWT thread)
Future fut = application.enqueue(new Callable() {
    public Object call() throws Exception {
        //this is where you modify your object, you can return a result value
        return modifyObject();
    }
});
 
//to retrieve return value (waits for call to finish, fire&forget otherwise):
fut.get();
@erlend-sh
erlend-sh / Create and add a ResourceLocator
Created December 10, 2012 18:26
jMonkeyEngine (jME2): Create and add a ResourceLocator for jME2 to load e.g. Textures.
ResourceLocator myLocator= new ResourceLocator(){
            public URL locateResource(String resourceName){
                //GET THE URL HERE
            }
        });
 
ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, myLocator);