Jump to content
  • Advertisement

thelordposeidon

Member
  • Content Count

    9
  • Joined

  • Last visited

Community Reputation

0 Neutral

About thelordposeidon

  • Rank
    Newbie

Personal Information

  • Interests
    Programming

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. thelordposeidon

    Marching Cubes "Fun"

    Unfortunately I can't read c++, I try and fail miserably, but thanks for the code. I'll see if anyone I know can talk me through it.
  2. thelordposeidon

    Marching Cubes "Fun"

    So the only difference between mine and scrawks code seems to be the code below. Scrawk's code : //Get the values in the 8 neighbours which make up a cube for (i = 0; i < 8; i++) { ix = x + VertexOffset[i, 0]; iy = y + VertexOffset[i, 1]; iz = z + VertexOffset[i, 2]; Cube[i] = voxels[ix + iy * width + iz * width * height]; } My code : private static boolean checkIfExpose(Voxel[][][] voxels, int x, int y, int z, float[] cube) { Voxel[] surroundingVoxels = getSurroundingVoxels(voxels, x, y, z); boolean bool = false; for (int i = 0; i < cube.length; i++) { cube[i] = 0; } if (surroundingVoxels[0].getMaterial().equals(VoxelMaterial.Air)) { cube[0] = 1f; cube[1] = 1f; cube[4] = 1f; cube[5] = 1f; bool = true; } if (surroundingVoxels[1].getMaterial().equals(VoxelMaterial.Air)) { cube[0] = 1f; cube[3] = 1f; cube[4] = 1f; cube[7] = 1f; bool = true; } if (surroundingVoxels[2].getMaterial().equals(VoxelMaterial.Air)) { cube[4] = 1f; cube[5] = 1f; cube[6] = 1f; cube[7] = 1f; bool = true; } if (surroundingVoxels[3].getMaterial().equals(VoxelMaterial.Air)) { cube[0] = 1f; cube[1] = 1f; cube[2] = 1f; cube[3] = 1f; bool = true; } if (surroundingVoxels[4].getMaterial().equals(VoxelMaterial.Air)) { cube[1] = 1f; cube[2] = 1f; cube[5] = 1f; cube[6] = 1f; bool = true; } if (surroundingVoxels[5].getMaterial().equals(VoxelMaterial.Air)) { cube[2] = 1f; cube[3] = 1f; cube[6] = 1f; cube[7] = 1f; bool = true; } return bool; } Bare in mind my code is Java and his is C#. If anyone has any ideas, please let me know. Here's a link to the file overall : here Edit : when I print out the value of a single cube to be rendered (a grid of 1 x 1 x 1) the corners all return 1, shouldn't that mean they are to be rendered?
  3. thelordposeidon

    Marching Cubes "Fun"

    Do you know of any off the top of your head that is similar enough that I can compare it easily? Edit : So I found that good old scrawk's code is similar to mine (though in c# and using unity). So the plan is to try and use that as a debug tool as that is definitely confirmed working. See where the difference in logic and code is. If I find anything I'll post it here, if there's nothing feel free to offer suggestions on what's wrong in the code as I've probably not solved it.
  4. thelordposeidon

    Marching Cubes "Fun"

    So i took some more pictures and did some debugging. First and foremost here are some better pictures of the cube at the original size (16 x 16 x 16 in opengl world coords). I uploaded it to imgur here. Next I tried some debuging. No cube shows when I have the grid size as 1 x 1 x 1. When it is 2 x 2 x 2 I get a small triangle (pic here) and finally when I do 4 x 4 x 4 I get this load of weirdness. Obviously my logic has gone wrong but honestly I have no idea where. Any ideas would be amazing.
  5. thelordposeidon

    Marching Cubes "Fun"

    Yeah, I'm pretty sure it's me making a silly mistake or a mistake in logic
  6. thelordposeidon

    Marching Cubes "Fun"

    Here's the pastebin : here, and the pictures will go up later. I added it because I was having some issues I think with direction culling and it seemed to help. This was awhile ago so I can't quite remember what I was doing.
  7. thelordposeidon

    Marching Cubes "Fun"

    Thanks for the advice, I'll get some better quality images tomorrow for you, unfortunately I had to take them under some time pressure but tomorrow I shouldn't have that issue. I'll take a look at the tutorial and thanks for the unit tests, again I'll look at them tomorrow.
  8. thelordposeidon

    My first game i need help

    Well think of what you're doing in simple steps. First : Move platform from side to side Second : Detect Player Collision Third : Center platform and player. Now that we have what you need to get done laid out in clear view we can start on figuring out the best way forward. For moving the platform side to side we merely need to change the position of the platform. Now you could move it a certain distance per frame but that will cause it to depend on the player's frame rate. A better solution would be to move the the platform a certain distance over a certain amount of time. I think there is two ways of doing this. The first is lerping the platform, the second being measuring the amount of time the last frame took and add the distance that the platform should of moved onto that. For lerping there is plenty of code all over the place for it so just google it. The second you'll have to start measuring for fps, yet again google is your friend. Now you have the platform moving from the center to one side, you need to set a limit and once the limit has been hit, invert the amount being added to the platform so that it moves backwards. Again have a limit set on the other side and repeat the invert once it reaches the limit. The easiest way for a limit is a if statement. Now we have the platform moving from side to side we need to check if the player collides with the platform. There's two things you need to think about for this. First does the player touch the platform and second is the player falling. If the player jumps and hits the platform underneath you don't want it to magically appear onto and in the middle. So the falling bit is easy if you have the jumping script already done. Just check to see if the movement on the y axis is minus or something along those lines. The collision is a bit harder. I believe unity has its own collision system for 2d and 3d programs so I would look at a collision tutorial for what you require. Once you can detect the collision, check if the player is falling. To center the player and platform the easiest way would be to have a predetermined middle point and just move the platform there and disable the platform's moving. Whether this is by having a simple boolean or setting the platform's movement to 0 it is up to you ( though the first is more efficient). Move both the player and platform to this middle point (I would just adjust the x axis and leave the player and platform on the same y as they are when they collide). Once they have been moved it is important to remember to set the player's falling to 0 or turn it off and re-enable jumping (depends on how you have your jumping system set up). Hopefully this is a good enough walk through for you. I don't really want to write the code as it would take a little bit of set up and it is better if you try it yourself because you'll learn more. If you need help google some of the things I talked about or just ask here and post the code you're having troubles with.
  9. Hey guys, I've been working on some marching cubes, and I've been having some weird issues that I really can't seem to understand. Basically the mesh isn't forming properly. Here are some screenshots and the code as it will show you the issue quicker than me describing it. As you can see the cube kinda forms but some areas are messed up, Here is the relevant code. package voxels; import java.util.ArrayList; import java.util.List; import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction; import org.lwjgl.util.vector.Vector3f; import models.MarchingCubesCases; import toolbox.UniversalFunctions; public class MeshGenerator { private static int[][] vertexOffset = new int[][] { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 0, 1, 1 } }; private static float target = 0.5f; public static Voxel[][][] generateVoxels(int size) { Voxel[][][] voxels = new Voxel[size][size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { for (int k = 0; k < size; k++) { voxels[i][j][k] = new Voxel(VoxelMaterial.Grass); } } } return voxels; } public static MeshData generateMeshMarchingCubes(Voxel[][][] voxels) { List<Vector3f> verts = new ArrayList<Vector3f>(); List<Float> normals = new ArrayList<Float>(); List<Integer> indices = new ArrayList<Integer>(); List<Float> textureCoords = new ArrayList<Float>(); for (int x = 0; x < voxels.length; x++) { for (int y = 0; y < voxels.length; y++) { for (int z = 0; z < voxels.length; z++) { if (voxels[x][y][z].getMaterial() == VoxelMaterial.Air) { continue; } float[] cube = new float[8]; if (checkIfExpose(voxels, x, y, z, cube) == false) { continue; } MarchCube(new Vector3f(x, y, z), cube, verts, indices); normals.add(0f); normals.add(1f); normals.add(0f); textureCoords.add(0f); textureCoords.add(1f); } } } MeshData data = new MeshData(); data.setIndices(UniversalFunctions.intListToArray(indices)); data.setVertsFromVector3f(verts); data.setNormals(UniversalFunctions.floatListToArray(normals)); data.setTextureCoords(UniversalFunctions.floatListToArray(textureCoords)); return data; } private static boolean checkIfExpose(Voxel[][][] voxels, int x, int y, int z, float[] cube) { Voxel[] surroundingVoxels = getSurroundingVoxels(voxels, x, y, z); boolean bool = false; for (int i = 0; i < cube.length; i++) { cube[i] = 1; } if (surroundingVoxels[0].getMaterial().equals(VoxelMaterial.Air)) { cube[0] = 0; cube[1] = 0; cube[4] = 0; cube[5] = 0; bool = true; } if (surroundingVoxels[1].getMaterial().equals(VoxelMaterial.Air)) { cube[0] = 0; cube[3] = 0; cube[4] = 0; cube[7] = 0; bool = true; } if (surroundingVoxels[2].getMaterial().equals(VoxelMaterial.Air)) { cube[4] = 0; cube[5] = 0; cube[6] = 0; cube[7] = 0; bool = true; } if (surroundingVoxels[3].getMaterial().equals(VoxelMaterial.Air)) { cube[0] = 0; cube[1] = 0; cube[2] = 0; cube[3] = 0; bool = true; } if (surroundingVoxels[4].getMaterial().equals(VoxelMaterial.Air)) { cube[1] = 0; cube[2] = 0; cube[5] = 0; cube[6] = 0; bool = true; } if (surroundingVoxels[5].getMaterial().equals(VoxelMaterial.Air)) { cube[2] = 0; cube[3] = 0; cube[6] = 0; cube[7] = 0; bool = true; } return bool; } private static Voxel[] getSurroundingVoxels(Voxel[][][] voxels, int x, int y, int z) { int maxLength = voxels.length; List<Voxel> voxelList = new ArrayList<Voxel>(); if (y - 1 > 0 && y - 1 < maxLength) { voxelList.add(voxels[x][y - 1][z]); } else { voxelList.add(new Voxel(VoxelMaterial.Air)); } if (x - 1 > 0 && x - 1 < maxLength) { voxelList.add(voxels[x - 1][y][z]); } else { voxelList.add(new Voxel(VoxelMaterial.Air)); } if (z - 1 > 0 && z - 1 < maxLength) { voxelList.add(voxels[x][y][z - 1]); } else { voxelList.add(new Voxel(VoxelMaterial.Air)); } if (z + 1 > 0 && z + 1 < maxLength) { voxelList.add(voxels[x][y][z + 1]); } else { voxelList.add(new Voxel(VoxelMaterial.Air)); } if (x + 1 > 0 && x + 1 < maxLength) { voxelList.add(voxels[x + 1][y][z]); } else { voxelList.add(new Voxel(VoxelMaterial.Air)); } if (y + 1 > 0 && y + 1 < maxLength) { voxelList.add(voxels[x][y + 1][z]); } else { voxelList.add(new Voxel(VoxelMaterial.Air)); } return voxelList.toArray(new Voxel[voxelList.size()]); } // MarchCube performs the Marching Cubes algorithm on a single cube private static void MarchCube(Vector3f pos, float[] cube, List<Vector3f> vertList, List<Integer> indexList) { int i, j, vert, idx; int flagIndex = 0; float offset = 0.0f; Vector3f[] edgeVertex = new Vector3f[12]; for (int inter = 0; inter < 12; inter++) { edgeVertex[inter] = new Vector3f(); } // Find which vertices are inside of the surface and which are outside for (i = 0; i < 8; i++) { if (cube[i] <= target) { flagIndex |= 1 << i; } } // Find which edges are intersected by the surface int edgeFlags = MarchingCubesCases.cubeEdgeFlags[flagIndex]; // If the cube is entirely inside or outside of the surface, then there will be // no intersections if (edgeFlags == 0) return; // Find the point of intersection of the surface with each edge for (i = 0; i < 12; i++) { // if there is an intersection on this edge if ((edgeFlags & (1 << i)) != 0) { offset = GetOffset(cube[MarchingCubesCases.edgeConnection[i][0]], cube[MarchingCubesCases.edgeConnection[i][1]]); edgeVertex[i].x = pos.x + (vertexOffset[MarchingCubesCases.edgeConnection[i][0]][0] + offset * MarchingCubesCases.edgeDirection[i][0]); edgeVertex[i].y = pos.y + (vertexOffset[MarchingCubesCases.edgeConnection[i][0]][1] + offset * MarchingCubesCases.edgeDirection[i][1]); edgeVertex[i].z = pos.z + (vertexOffset[MarchingCubesCases.edgeConnection[i][0]][2] + offset * MarchingCubesCases.edgeDirection[i][2]); } } // Save the triangles that were found. There can be up to five per cube for (i = 0; i < 5; i++) { if (MarchingCubesCases.triangleConnectionTable[flagIndex][3 * i] < 0) { break; } idx = vertList.size(); for (j = 0; j < 3; j++) { vert = MarchingCubesCases.triangleConnectionTable[flagIndex][3 * i + j]; indexList.add(idx + MarchingCubesCases.windingOrder[j]); vertList.add(edgeVertex[vert]); } } } // GetOffset finds the approximate point of intersection of the surface // between two points with the values v1 and v2 private static float GetOffset(float v1, float v2) { float delta = v2 - v1; return (delta == 0.0f) ? 0.5f : (target - v1) / delta; } } I can attach the look up tables if needed. The MeshData class is just a class to hold verts, indices, etc. The voxel data is generated using the generateVoxels method and it is for a size of 16. Finally the voxel material only maters if the material is air as then the voxel will need to be rendered. Any help would be amazing. Thanks Euan
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!