How can i make a cube/box that pulsates?

-Title-



Its exercise from HelloAsset chapter :slight_smile:

Correction:

its from Hello Loop the chapter about Update loop :smiley:

and i have another question about the variable tpf, i know that he means time per frame but i dont get which value he got?

pulsate how in size, lighting or texture :?

1 Like

private float scale = 1;



[java]public void simpleUpdate(float tpf) {

if(mySpatial.getLocalScale() > 1.0f && mySpatial.getLocalScale() < 2.0f) {

mySpatial.scale(scale * tpf);

} else {

scale = scale * -1;

}[/java]


@yarin1312 said:
and i have another question about the variable tpf, i know that he means time per frame but i dont get which value he got?

if you want to see the time just look at the library and see the code for SimpleApplication
1 Like
@skidrunner said:
scale = scale * -1;


I think a negative scale is probably not what you want.

To the OP, do you know how to make the scale value change the way you want? JME requires a reasonable level of Java+programming skill.

Or if you want a smoother change accumulate a total time and feed it through a call to sin().

[java]



float accumulatedTime = 0f;



public void simpleUpdate(float tpf) {

accumulatedTime += tpf;



mySpatial.setLocalScale(2+FastMath.sin(accumulatedTime));

}



[/java]



Note the setting of the scale rather than applying a scale to the already existing scale.

Yeeh, @skidrunners code looks valid, the variable โ€œscaleโ€ is actually deltaScale.

@pspeed said:
I think a negative scale is probably not what you want.

To the OP, do you know how to make the scale value change the way you want? JME requires a reasonable level of Java+programming skill.

you know if i asked something it doesnt say that i'm dont know java, i thought that this forum its place to help people and to tell them what they know and what they dont, thank you anyway....
by the way i write this :
if((player.getLocalScale() > 1.0f ) && (player.getLocalScale() < 2.0f))
and player its a geometry box not a spatial and its give me an eror and say some thing about bad operator 0.0
@yarin1312 said:
you know if i asked something it doesnt say that i'm dont know java, i thought that this forum its place to help people and to tell them what they know and what they dont, thank you anyway....
by the way i write this :
if((player.getLocalScale() > 1.0f ) && (player.getLocalScale() < 2.0f))
and player its a geometry box not a spatial and its give me an eror and say some thing about bad operator 0.0


I'm just trying to understand what parts you understand and what you don't.

If you are trying to make the scale go up and down then do you at least know how to make a scale variable go up and down? If so then just setLocalScale() on the box. If not... well, then we need to dive deeper into basic programming stuff but I wanted to save you from that if you already knew Java.
@zarch said:
Yeeh, @skidrunners code looks valid, the variable "scale" is actually deltaScale.


Except when you call scale() is is multiplying and not adding... so there is no such thing as a deltaScale unless you add it up yourself.

Yeeh, I see what you mean. You are right.

I dont know why its happen but when i write yours code is give me an error that say this is bad operator 0.0

he say that i cant opernd them together because the getLocalScale return you a Vector3f object or something like that :smiley:

@yarin1312 said:
I dont know why its happen but when i write yours code is give me an error that say this is bad operator 0.0


Show us the code how you have it. Note: skidrunner's code won't work anyway for several other reasons.

I was thinking sudo code -.-



[java]private float varScale = 1.0f;

private float varScaler = 1;



public void simpleUpdate(float tpf) {

// incremint varScal

varScale += tpf * varScaler;

// check scale range

if(varScal < 1.0f || varScale > 2.0) {

varScaler = varScaler * -1;

// valodate in range

if(varScale < 1.0f) {

varScale = 1.0f;

} else if(varScale > 2.0f) {

varScale = 2.0f;

}

}

mySpatial.setLocalScale(varScale);

}[/java]

@skidrunner said:
I was thinking sudo code -.-

private float varScale = 1.0f;
private float varScaler = 1;

public void simpleUpdate(float tpf) {
// incremint varScal
varScale += tpf * varScaler;
// check scale range
if(varScal < 1.0f || varScale > 2.0) {
varScaler = varScaler * -1;
// valodate in range
if(varScal < 1.0f) {
varScal = 1.0f;
} else if(varScal > 2.0f) {
varScal = 2.0f;
}
}
mySpatial.setScale(varScale);
}


That will work better. Note: there is a little Java button above the edit box. Use it and then put your code in that block so that it is readable.... save our eyes. :)

[java]package jme3test.helloworld;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Quaternion;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;



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

  • In this example, we make the player character rotate. /

    public class HelloLoop extends SimpleApplication {



    public static void main(String[] args){

    HelloLoop app = new HelloLoop();

    app.start();

    }



    protected Geometry player, player1;

    private float scale = 1;

    private int count = 0;

    @Override

    public void simpleInitApp() {



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

    player = new Geometry("blue cube", b);

    Material mat = new Material(assetManager,

    "Common/MatDefs/Misc/Unshaded.j3md");

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

    player.setMaterial(mat);

    rootNode.attachChild(player);



    Box c = new Box(new Vector3f(0f, 3.0f, 0f), 1, 1, 1);

    player1 = new Geometry("Box", c);

    Material mat1 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md" );

    mat1.setColor("Color", ColorRGBA.Red);

    player1.setMaterial(mat1);

    rootNode.attachChild(player1);

    }



    /
    This is the update loop /

    @Override

    public void simpleUpdate(float tpf) {

    if((player.getLocalScale() < 0)&&(count == 0)){

    player.setLocalScale(tpf
    scale);

    count++;

    }

    else{

    scale = -1;

    player.setLocalScale(tpf
    scale);

    countโ€“;

    }









    }

    }

    [/java]



    dont look about the second box its thing about the exercise

better :roll:

why would player scale be less than 0?

@yarin1312 said:
[java]package jme3test.helloworld;
public void simpleUpdate(float tpf) {
if((player.getLocalScale() &lt; 0)&amp;&amp;(count == 0)){
player.setLocalScale(tpf*scale);
count++;
}
else{
scale *= -1;
player.setLocalScale(tpf*scale);
count--;
}
[/java]


The above has a few problems. First, you don't really want scale to ever go negative.
Second, the first part of the if block will never run because when getLocalScale() is less than 0 the first time then count will be -1 and dropping.

Use zarch's code or for a linear change look at the last code from skidrunner.
1 Like

So i dont have to give a negative value to the scale never?

whats happen if i will, i will dont see the spatial?

@skidrunner said:
I was thinking sudo code -.-

[java]private float varScale = 1.0f;
private float varScaler = 1;

public void simpleUpdate(float tpf) {
// incremint varScal
varScale += tpf * varScaler;
// check scale range
if(varScal < 1.0f || varScale > 2.0) {
varScaler = varScaler * -1;
// valodate in range
if(varScal < 1.0f) {
varScal = 1.0f;
} else if(varScal > 2.0f) {
varScal = 2.0f;
}
}
mySpatial.setScale(varScale);
}[/java]

you set 2 variables and used 3, in varScal you mean VarScale?
and actually you didnt answer me about the question on tpf, how can i know what his value and right use :D