Skip to content

jme3-examples: TestDoppler - test code optimization #2431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 46 additions & 19 deletions jme3-examples/src/main/java/jme3test/audio/TestDoppler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2012 jMonkeyEngine
* Copyright (c) 2009-2025 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -35,11 +35,16 @@
import com.jme3.app.SimpleApplication;
import com.jme3.audio.AudioData;
import com.jme3.audio.AudioNode;
import com.jme3.math.FastMath;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.debug.Grid;
import com.jme3.scene.shape.Sphere;
import com.jme3.scene.shape.Torus;

import java.util.Locale;

/**
* Test Doppler Effect
Expand All @@ -49,46 +54,68 @@ public class TestDoppler extends SimpleApplication {
private float pos = -5;
private float vel = 5;
private AudioNode ufoNode;
private BitmapText bmp;

public static void main(String[] args){
public static void main(String[] args) {
TestDoppler test = new TestDoppler();
test.start();
}

@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(10);

Torus torus = new Torus(10, 6, 1, 3);
Geometry g = new Geometry("Torus Geom", torus);
g.rotate(-FastMath.HALF_PI, 0, 0);
g.center();

g.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
// rootNode.attachChild(g);
configureCamera();
bmp = createLabelText(10, 20, "<placeholder>");

ufoNode = new AudioNode(assetManager, "Sound/Effects/Beep.ogg", AudioData.DataType.Buffer);
ufoNode.setLooping(true);
ufoNode.setPitch(0.5f);
ufoNode.setRefDistance(1);
ufoNode.setMaxDistance(100000000);
ufoNode.setVelocityFromTranslation(true);
ufoNode.play();
rootNode.attachChild(ufoNode);

Geometry ball = new Geometry("Beeper", new Sphere(10, 10, 0.1f));
ball.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
Geometry ball = makeShape("Beeper", new Sphere(10, 10, .5f), ColorRGBA.Red);
ufoNode.attachChild(ball);

rootNode.attachChild(ufoNode);
Geometry grid = makeShape("DebugGrid", new Grid(21, 21, 2), ColorRGBA.Gray);
grid.center().move(0, 0, 0);
rootNode.attachChild(grid);

ufoNode.play();
}

private void configureCamera() {
flyCam.setMoveSpeed(15f);
flyCam.setDragToRotate(true);

cam.setLocation(Vector3f.UNIT_XYZ.mult(12));
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
}

@Override
public void simpleUpdate(float tpf) {
pos += tpf * vel;
if (pos < -10 || pos > 10) {
if (pos < -10f || pos > 10f) {
vel *= -1;
}
ufoNode.setLocalTranslation(new Vector3f(pos, 0, 0));
ufoNode.setLocalTranslation(pos, 0f, 0f);
bmp.setText(String.format(Locale.ENGLISH, "Audio Position: (%.2f, %.1f, %.1f)", pos, 0f, 0f));
}

private BitmapText createLabelText(int x, int y, String text) {
BitmapText bmp = new BitmapText(guiFont);
bmp.setText(text);
bmp.setLocalTranslation(x, settings.getHeight() - y, 0);
bmp.setColor(ColorRGBA.Red);
guiNode.attachChild(bmp);
return bmp;
}

private Geometry makeShape(String name, Mesh mesh, ColorRGBA color) {
Geometry geo = new Geometry(name, mesh);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", color);
geo.setMaterial(mat);
return geo;
}
}