I am working on a infinite world generated with Perlin Noise, Java and LWJGL. But I am having a problem, it is kinda hard to explain, so I made a video: http://youtu.be/D_NUBJZ_5Kw Obviously the problem is the black spaces in between all the pieces of ground.
I already tried making all the values doubles instead of floats, but that didn't fix it.
Here is a piece of code I am using:
float height2, height = (float)getHeight(x, y);
height2 = (float) ((getHeight(x-1, y+1) + height) / 2);
vertexhelper.addVertexColorAndTexture(x, height2, y+1, r, g, b, a, 0f, 1f);
height2 = (float) ((getHeight(x+1, y+1) + height) / 2);
vertexhelper.addVertexColorAndTexture(x+1, height2, y+1, r, g, b, a, 1f, 1f);
height2 = (float) ((getHeight(x+1, y-1) + height) / 2);
vertexhelper.addVertexColorAndTexture(x+1, height2, y, r, g, b, a, 1f, 0f);
height2 = (float) ((getHeight(x-1, y-1) + height) / 2);
vertexhelper.addVertexColorAndTexture(x, height2, y, r, g, b, a, 0f, 0f);
I loop through this at the initialization of a chunk with x->16 and y->16. vertexhelper is a class I made that just puts everything in a array.
(I am using floats here, but that's after doing the maths, so that shouldn't be a problem)
I had to take 4 places on the heightmap in account instead of 2. So instead of
height2 = (float) ((getHeight(x-1, y-1) + height) / 2);
I had to use
height2 = (float) ((getHeight(x, y-1) + getHeight(x-1, y) + getHeight(x-1, y-1) + height) / 4);
That fixed it.
Related
I am drawing a series of rectangles on a Canvas. The rectangles are supposed to move on an angle. For some reason, when they move, they scale up:
xPos += xSpeed;
yPos += ySpeed;
updateBounds(xPos, yPos, width, height);
My UpdateBounds method:
public void updateBounds(double x, double y, double w, double h) {
bounds.setRect(x, y, w, h);
}
Bounds is a Rectangle2D object.
And my Drawing method:
g.fillRect((int) bounds.getX(), (int) bounds.getY(),
(int) bounds.getMaxX(), (int) bounds.getMaxY());
Why am I getting this behaviour?
Graphics.fillRect() accepts a width and height parameter, not the largest x and y position of the rectangle to draw.
The third and fourth parameters to fillRect should be Rectangle2D's getWidth() and getHeight().
As a reference, a link to what getMaxX() would give you.
Im trying to implement Bitmap font rendering. The only thing that confuses me so much, that i have to ask about it here, is that when I draw the quads for each char, every time it a new quad get rendered, the UV`s are absolutelly broken.
If I just render a single quad, everything is OK.
(tile x = 3, y = 4)
Thats what it looks like with a few more letters.
Thats my code to render this stuff:
...
rc.translate(getTransform().getPos());
drawTileAt(3, 4);
rc.translate(100, 0, 0);
drawTileAt(5, 5);
rc.translate(200, 0, 0);
drawTileAt(2,6);
rc.translate(300,0,0);
drawTileAt(1, 7);
...
public void drawTileAt(int x, int y)
{
float w = 1f / 16f;
float _x = 1f / 16f * x;
float _y = 1f / 16f * y;
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(0, 100);GL11.glTexCoord2f(_x,_y);
GL11.glVertex2f(0, 0);GL11.glTexCoord2f(_x + w,_y);
GL11.glVertex2f(100,0);GL11.glTexCoord2f(_x + w,_y + w);
GL11.glVertex2f(100, 100);
GL11.glTexCoord2f(_x,_y + w);
GL11.glEnd();
}
Im also drawing VBO quads with tex-coords, everything looks fine...
glVertex() bundles up the current vertex state (like the texture coordinate!) and sends it to the GPU.
So you need to set the texcoord before you set the vertex position:
GL11.glTexCoord2f(_x,_y); GL11.glVertex2f(0, 100);
GL11.glTexCoord2f(_x + w,_y); GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(_x + w,_y + w); GL11.glVertex2f(100,0);
GL11.glTexCoord2f(_x,_y + w); GL11.glVertex2f(100, 100);
In a middle of developing my game I created a function to rotate a 2d triangle.
But the problem is that when I rotate an object, the rotation is very weird.
If the object's center is at the origin (the (0,0) point) of the axis then it rotates fine, but if I move the object then it rotates in a very weird way that it shouldn't be rotating in.
as you can see here is the code of the function.
public void rotate(float angle){
float x1 = x, y1 = y;
float x2 = x + width, y2 = y;
float x3 = x + width, y3 = y + height;
float x4 = x, y4 = y + height;
// Vertices in object space:
float[] vector1 = {x1, y1, 0f, 1f};
float[] vector2 = {x2, y2, 0f, 1f};
float[] vector3 = {x3, y3, 0f, 1f};
float[] vector4 = {x4, y4, 0f, 1f};
// Vertices in world space:
float[] vector1r = new float[4];
float[] vector2r = new float[4];
float[] vector3r = new float[4];
float[] vector4r = new float[4];
// Calculate the vertices in world space:
Matrix.multiplyMV(vector1r, 0, modelMatrix, 0, vector1, 0);
Matrix.multiplyMV(vector2r, 0, modelMatrix, 0, vector2, 0);
Matrix.multiplyMV(vector3r, 0, modelMatrix, 0, vector3, 0);
Matrix.multiplyMV(vector4r, 0, modelMatrix, 0, vector4, 0);
// Get the middle of the rectangle:
float[] vecMid = {(vector1r[0] + vector3r[0]) / 2,
(vector1r[1] + vector3r[1]) / 2, 0f, 1f};
move(vecMid[0], vecMid[1]); // Move to the origin
Matrix.rotateM(modelMatrix, 0, angle, 0, 0, 1f); // Rotate the object
move(-vecMid[0], -vecMid[1]); // Move back to place
if(LoggerConfig.ON)
Log.d("Middle Vertex", vecMid[0] + ", " + vecMid[1]);
}
The move method:
public void move(float x, float y){
Matrix.translateM(modelMatrix, 0, x, y, 0);
}
Can someone tell me what the problem is?
EDIT: I did some modifications to the code and added the move method.
How should the x, y, z, sizeX, sizeY, sizeZ values be put to the vertices to make a cube?
public static void cube(float x, float y, float z, float sx, float sy, float sz){
glPushMatrix();
{
glTranslatef(x, y, z);
//Just one side of the cube is given due to too much unnecessary code.
glBegin(GL_QUADS);
glVertex3f(-1, -1, 1);
glVertex3f(1, -1, 1);
glVertex3f(1, 1, 1);
glVertex3f(-1, 1, 1);
glEnd();
}
glPopMatrix();
}
Thanks.
Wherever in your code you have e.g. glVertex3f(-1, -1, 1); mulitply them with the corresponding value of sx, sy, sz divided by 2 e.g. glVertex3f(-sx/2, -sy/2, sz/2);
For the position you can issue a glTranslatef(x, y, z) before drawing the cube. If you insist on hardcoding this into the vertices then you should write the above statement as glVertex3f(x - sx/2, y - sy/2, z + sz/2);
In Java for some reason the Ellipse2D.Double uses the parameters (height, width, x, y) where as when I create a RectF in Android the parameters are (left, top, right, bottom) so I'm a bit confused on adjusting to the differences.
If a create an Ellipse in Java and use the following:
//Ellipse2D.Double(height, width, x, y)
x = 100;
y = 120;
centerX = getWidth() / 2;
centerY = getHeight() / 2;
//Ellipse2D.Double(100, 120, (centerX - 100) * 2, (centerY - 120) * 2);
new Ellipse2D.Double(x, y, (centerX - x) * 2, (centerY - y) * 2);
Would this be equivalent for Android:
//RectF(left, top, right, bottom)
x = 100;
y = 120;
centerX = getWidth() / 2;
centerY = getHeight() / 2;
new RectF((centerX - 100) * 2, (centerY - 120) * 2), 120 - ((centerX - 100) * 2), 100 - ((centerY -120) * 2);
//canvas.drawOval(myRectF, paint);
I'm not quite sure if they are equivalent, and am wondering if I am calculating it correctly?
Alternatively, can one override the RectF to make it simliar to how Ellipse2D? Ie. change the parameters to work with height and width rather than right and bottom?
For the override part, I don't thing it would be a good idea since RectF isn't only used for ellipses.
you can easily write a method that draw the Oval by passing the data the way you prefer...
something like:
public RectF myOval(float width, float height, float x, float y){
float halfW = width / 2;
float halfH = height / 2;
return new RectF(x - halfW, y - halfH, x + halfW, y + halfH);
}
canvas.drawOval(myOval(width, height, x, y), paint);
To keep in the x, y, width, height thinking, you can construct a utility function to build a RectF with the coordinates in the order you like to think of them:
public static RectF buildRectF(double x, double y, double width, double height) {
// r(l, t, r, b)
RectF rectf = new RectF(x - width / 2, y - height / 2, x + width / 2, y + height / 2);
return rectf;
}
It is unclear what you are trying to do with the code sample you have.
Ellipse2D.Double takes 4 parameters: x, y, width and height.
It looks like you are setting width to be (centerX - x) * 2; this will ensure that the width is twice the distance from the center of the component your code resides in to the point (100, 120), if the center is to the right of the point (100, 120). If your component gets too small, though, you will assign a negative width, which could be awkward.
Also, you are using hardcoded values in your RectF example, and combining 120 (the y?) and 100 (the x?) in the same arguments to RectF, which is most likely not what you want to do.
I'd suggest drawing a picture on a piece of paper, label the coordinates with the values you think they should be, then write your code. You should be able to more clearly see what your top left bottom and right (or x, y, width and height) values should be.