Hi guyz,
I have a problem and I want to ask you, if you can help me.
I’m trying to make something like “Sombrero’s function”, but I have a problem with making this function.
There is my projekt:
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.VertexBuffer;
import com.jme3.util.SkyFactory;
import java.util.Random;
public class Main extends SimpleApplication {
private Node map;
//Vertices of loaded water patch
VertexBuffer vertices;
//Wave map constants to control waves
int currmap = 0;
int nextmap = 1;
//Value of ripple (lenght)
float waterDampen = 60.0f;
//Our water plane tiles number along X and Y axes
int numTilesX = 100;
int numTilesY = 100;
//Number of vertices along individual axes
int numVertsX = numTilesX + 1;
int numVertsY = numTilesY + 1;
int vertindex = 0;
//Water wave map, deformation update will happen as well as waves propagation
float[][][] waveMap = new float[2][numVertsX + 2][numVertsY + 2];
//don't askme now == remake in some time
Random ran = new Random();
int tileX = (0 + ran.nextInt(numTilesX + 1)) + 1;
int tileY = (0 + ran.nextInt(numTilesY + 1)) + 1;
//Light
private DirectionalLight dl = new DirectionalLight();
private AmbientLight am = new AmbientLight();
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
createSky();
createWaterSurface();
createLight();
initCrossHairs();
initKeys();
cam.setLocation(new Vector3f(0.13642268f, 3.4800892f, 11.605716f));
}
private void createSky() {
rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
}
private void createWaterSurface() {
map = (Node) assetManager.loadModel("Scenes/my_water_surface.j3o");
vertices = ((Geometry) map.getChild("Plane1")).getMesh().getBuffer(VertexBuffer.Type.Position);
map.setLocalTranslation(new Vector3f(1, -10, 0));
rootNode.attachChild(map);
}
private void createLight() {
dl.setColor(ColorRGBA.White.clone().multLocal(2));
dl.setDirection(new Vector3f(-1, -1, -1).normalize());
rootNode.addLight(dl);
am.setColor(ColorRGBA.White.mult(2));
rootNode.addLight(am);
}
protected void initCrossHairs() {
setDisplayStatView(false);
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("+");
ch.setLocalTranslation(settings.getWidth() / 2 - ch.getLineWidth() / 2,
settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
guiNode.attachChild(ch);
}
private void initKeys() {
inputManager.addMapping("Shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "Shoot");
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Shoot") && !keyPressed) {
CollisionResults results = new CollisionResults();
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
map.collideWith(ray, results);
if (results.size() > 0) {
waveMap[currmap][tileX][tileY] = 10f; //<= setting deepness
}
}
}
};
@Override
public void simpleUpdate(float tpf) {
for (int countY = 1; countY < numVertsY + 1; countY++) {
for (int countX = 1; countX < numVertsX + 1; countX++) {
//calculate wave intensity for this point
float n = waveMap[currmap][countX - 1][countY] + waveMap[currmap][countX + 1][countY]
+ waveMap[currmap][countX][countY - 1] + waveMap[currmap][countX][countY + 1];
n = (n / 2) - waveMap[nextmap][countX][countY];
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
createSky();
createWaterSurface();
createLight();
initCrossHairs();
initKeys();
cam.setLocation(new Vector3f(0.13642268f, 3.4800892f, 11.605716f));
}
private void createSky() {
rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
}
private void createWaterSurface() {
map = (Node) assetManager.loadModel("Scenes/my_water_surface.j3o");
vertices = ((Geometry) map.getChild("Plane1")).getMesh().getBuffer(VertexBuffer.Type.Position);
map.setLocalTranslation(new Vector3f(1, -10, 0));
rootNode.attachChild(map);
}
private void createLight() {
dl.setColor(ColorRGBA.White.clone().multLocal(2));
dl.setDirection(new Vector3f(-1, -1, -1).normalize());
rootNode.addLight(dl);
am.setColor(ColorRGBA.White.mult(2));
rootNode.addLight(am);
}
protected void initCrossHairs() {
setDisplayStatView(false);
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("+");
ch.setLocalTranslation(settings.getWidth() / 2 - ch.getLineWidth() / 2,
settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
guiNode.attachChild(ch);
}
private void initKeys() {
inputManager.addMapping("Shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "Shoot");
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Shoot") && !keyPressed) {
CollisionResults results = new CollisionResults();
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
map.collideWith(ray, results);
if (results.size() > 0) {
waveMap[currmap][tileX][tileY] = 10f; //<= setting deepness
}
}
}
};
@Override
public void simpleUpdate(float tpf) {
for (int countY = 1; countY < numVertsY + 1; countY++) {
for (int countX = 1; countX < numVertsX + 1; countX++) {
//calculate wave intensity for this point
float n = waveMap[currmap][countX - 1][countY] + waveMap[currmap][countX + 1][countY]
+ waveMap[currmap][countX][countY - 1] + waveMap[currmap][countX][countY + 1];
n = (n / 2) - waveMap[nextmap][countX][countY];
//dampen
n = n - (n / waterDampen);
waveMap[nextmap][countX][countY] = n;
//set Point Height
vertindex = ((countY - 1) * numVertsX) + countX;
//set height of corresponding vertex trough vertex buffer
vertices.setElementComponent(vertindex, 1, n);
}
}
//swap the wave maps
currmap = (currmap + 1) % 2;
nextmap = (nextmap + 1) % 2;
}
It is crasing on line “vertices.setElementComponent(vertindex, 1, n);” and I don’t have an idea why. Can you please help me solve this problem? I’m not sure if you will need model “my_water_surface.j3o” so if you will need it, just ask me, I can send it to you anywhere you will need.
If you can help me, I will be really glad to.
Thanks
Sebastian
Edit: fixed the code because OP couldn’t.