My guiNode is translated and scaled to adjust a 1080p frame into any Display resolution and aspect ratio.
I’d like to add a zoom effect but my math is failing me (I can’t zoom reliably on a position regardless of the current resolution).
This is my code:
public class HelloJME3 extends SimpleApplication {
public static void main(String[] args) {
HelloJME3 app = new HelloJME3();
app.setShowSettings(false);
AppSettings settings = new AppSettings(true);
//settings.setResolution(320, 180);
//settings.setResolution(800, 450);
//settings.setResolution(1024, 576);
//settings.setResolution(1024, 768);
settings.setResolution(1280, 720); // 16/9
//settings.setResolution(1280, 800); // 16/10
//settings.setResolution(1280, 960); // 4/3
//settings.setResolution(1280, 1024); // 5/4
//settings.setResolution(1366, 768);
//settings.setResolution(1600, 900);
//settings.setResolution(1920, 1080);
//settings.setFullscreen(true);
settings.setSettingsDialogImage("Interface/splash2.jpg");
app.setSettings(settings);
app.start(); // start the game
}
@Override
public void simpleInitApp() {
stateManager.attach(new ZoommAppState());
}
class ZoommAppState extends BaseAppState {
/*
-------------
| |
| |
| |
-------------
-------------------------------
| |
| |
Hz| | (H zoomed)
| |
Hr------------- | (H real)
| | |
| | |
| | |
-------------------------------
Hz = Hr * scale
Hr = Hz / scale
(Hz-Hr) = (Hr*scale) - Hr
*/
@Override
protected void initialize(Application app) {
baseScale = 1/1.5f;
baseTranslationY = 0;
System.out.println("Local scale is " + baseScale);
System.out.println("traslation y " + baseTranslationY);
Box b = new Box(1500, 10, 1); // create cube shape
Geometry geom = new Geometry("Box", b); // create cube geometry from the shape
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
mat.setColor("Color", ColorRGBA.Red); // set color of material to blue
geom.setMaterial(mat); // set the cube's material
geom.move(0, targetY, 99990);
((SimpleApplication) getApplication()).getGuiNode().attachChild(geom); // make
for (int i = 0; i < 72; i++) {
Box bo = new Box(1500, 1, 1);
Geometry geom2 = new Geometry("Box", bo); // create cube geometry from the shape
Material mat2 = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
mat2.setColor("Color", ColorRGBA.Green); // set color of material to blue
geom2.setMaterial(mat2); // set the cube's material
geom2.move(0, i * 10, 99990);
((SimpleApplication) getApplication()).getGuiNode().attachChild(geom2); // make
}
}
float maxZoom = 1.5f;
//float targetY = 0;
float targetY = 720 / 2;
float baseTranslationY;
float baseScale;
float scale;
float tTPF;
@Override
public void update(float tpf) {
tTPF += tpf;
if (tTPF < FastMath.PI) {
float varScale = FastMath.sin(tTPF) * maxZoom;
float zoomEffect = 1f + varScale;
//adatto lo zoom all'aspect ratio
scale = baseScale * zoomEffect;
((SimpleApplication) getApplication()).getGuiNode().setLocalScale(scale);
((SimpleApplication) getApplication()).getGuiNode().setLocalTranslation(0, baseTranslationY - ((targetY * scale) - targetY) / baseScale, 0);
} else {
tTPF = 0;
//setEnabled(false);
}
}
@Override
protected void cleanup(Application app) {
}
@Override
protected void onEnable() {
}
@Override
protected void onDisable() {
//System.out.println("traslation y " + baseTranslationY);
//((SimpleApplication) getApplication()).getGuiNode().setLocalScale(baseScale);
//((SimpleApplication) getApplication()).getGuiNode().setLocalTranslation(0, baseTranslationY, 0);
}
}
}
Any help is appreciated, thanks!