We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm working on a personal project that will render a low-quality (32x32) heightmap as 3D terrain. I'm having issues loading the terrain file, which I've defined as a JSON file with a particular format, as a big 2D array. I'm probably doing many things horribly wrong (especially loading array elements from a JSON), please help me...
JSON object format:
{
"row": 0,
"data": [ -128, -109, -70, ... 30, 102 ]
},
There's 32 of them labeled 0 to 31, and each "data" array has 32 values ranging from -128 to 127. I've verified that the JSON is valid.
Code:
JSONArray rawHeight;
byte[][] hmap = null;
byte[] temp = null;
void prepareHMapAndTemp() {
for (int h = 0; h < 32; h++) {
hmap[i] = new byte[32];
temp[i] = new byte;
}
}
void processHeightData() {
for (int i = 0; i < 32; i++) {
JSONObject row = rawHeight.getJSONObject(i);
for (int j = 0; j < 32; j++) {
temp = (byte[])append(hmap, row.getJSONObject("data").getInt(j));
}
hmap = (byte[][])append(hmap, temp);
println(i + ": " + row);
}
}
void setup() {
rawHeight = loadJSONArray("map.hmp");
prepareHMapAndTemp();
processHeightData();
}
On temp[i] = new byte;
, byte;
is underlined and gives this very ambiguous error: Error on "Dimensions"
. Trying to run it anyways has it yelling unexpected token ";"
. Also, temp = (byte[])append(hmap, row.getJSONObject("data").getInt(j));
gives the error The function "getInt()" expects parameters like: "getInt(String)"
even though the online reference says it takes an int for the index?
Edit: Just realized I was I started hmap without a value and temp with a value of null. Now both are be started as null. Don't think there's actually a difference, though.
Answers
Try:
For line 17 and 19, you should be doing something like:
temp[j] = row.getJSONObject("data").getInt(j);
Can you share your file, or you can copy and paste its content here or send it to me by PM.
Kf
I'm about to try that. And that was the entire file, minus a couple comments I put in for myself. Working on getting the heightmap loaded before anything else.
Edit: tried it, and with some mucking around I got it to not give me red squiggly lines in the IDE. It still gives me a NullPointerException when trying to load each int from the "data" objects in the JSON.
The data in your first comment is all the data you have right now? Then you will get a NPE because the code is try to load an array of 32 by 32. Either use a data set with 32 x 32 data array as input or modify the code to read only available data.
Kf
He means the data file
No, I do have an entire file with 32 objects that have 32 points each. I meant that was all the code (but I've PM'd you the new code)
Sorry, I was not clear, I was inquiring about your data file.
Kf
Oh, yeah. It's a lot of text so here's a pastebin.
Here is a tested version.
Kf
Yep, that worked. Thanks!