Working on the update cube example in the helloLoop tutorial

Hello, I’m a beginner with jmonkey, I’m working on getting a cube to pulsate found in the 4th beginner tutorial. Right now, my cube continues to grow bigger until it is out of the frame. I tried just looking at the solutions in this link http://jmonkeyengine.org/wiki/doku.php/jm3:solutions, but this just redirects me to the documentation page, so perhaps the link is broken?
This is the code I have for HelloLoop:
package jme3test.helloworld;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

/** Sample 4 - how to trigger repeating actions from the main event loop.

  • In this example, you use the loop to make the player character

  • rotate continuously. */
    public class HelloLoop extends SimpleApplication {

    public static void main(String[] args){
    HelloLoop app = new HelloLoop();
    app.start();
    }
    private int count;
    protected Geometry player;

    @Override
    public void simpleInitApp() {
    /** this blue box is our player character */
    Box b = new Box(1, 1, 1);
    player = new Geometry(“blue cube”, b);

     count = 0;
     Material mat = new Material(assetManager,
       "Common/MatDefs/Misc/Unshaded.j3md");
     mat.setColor("Color", ColorRGBA.Blue);
     player.setMaterial(mat);
     rootNode.attachChild(player);
    

    }

    /* Use the main event loop to trigger repeating actions. */
    @Override
    public void simpleUpdate(float tpf) {
    the axis of rotation
    if ((count % 2 ) == 0){

      player.scale(1.03f);
      count++;
     }
     else{
         player.scale(1.0f);
         count++;
     }
    

    }
    }

I think the author of this example might have been slightly confused or at least partially so.

Since scale() is applied relative to the current scale, scale(1) will never ever have any effect.

Try changing that to setScale(1) instead.

P.S.: This was a perfectly worded question… it almost brought a tear to my eye. It has exactly the stuff we need to help as quickly as possible. Welcome to the forum. :slight_smile:

Hi there @cvtoro,
Well if you want to scale the spatial every second count I would rather use this:

@Override
public void simpleUpdate(float tpf) {
    if ((count % 2) == 0) {

        player.setLocalScale(1.3f);
        count++;
    } else {
        player.setLocalScale(1.0f);
        count++;
    }
}

But if you want a smooth up down scale here is a little control to add to the spatial:

public class ScaleUpDownControl extends AbstractControl {

private float maxScale = 1f;
private float speed = 1;
private boolean scaleUp = true;

public ScaleUpDownControl(float maxScale, float speed) {
    this.maxScale = maxScale;
    this.speed = speed;
}

@Override
protected void controlUpdate(float tpf) {
    if (scaleUp) {
        if (spatial.getLocalScale().x >= maxScale) {
            scaleUp = false;
            spatial.setLocalScale(maxScale);
            
        } else {
            spatial.setLocalScale(spatial.getLocalScale().x + (tpf*speed));
        }
        
    } else {
        if (spatial.getLocalScale().x <= 1f) {
            scaleUp = true;
            spatial.setLocalScale(1f);
        } else {
            spatial.setLocalScale(spatial.getLocalScale().x - (tpf*speed));
        }
    }
}

@Override
protected void controlRender(RenderManager rm, ViewPort vp) {

}

}
Then you can add the control to the player cube like this:

player.addControl(new ScaleUpDownControl(1.1f, 1));

2 Likes

Great answer @ndebruyn. This really helped!