I've seen similar questions before but none helped me.
The player in my game can go to the right forever until he dies, but he can also go back.
I have a wall texture that I need to repeat forever on the top and bottom of the screen. I've seen answers where they say you need to put different values when drawing the texture at srcX, srcY, srcWidth and srcHeight, but when I do that the texture doesn't look anyting like the original. I set the texture to repeat with wallTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat)
The code I have to draw the wall on the top and bottom of the screen:
spriteBatch.draw(wallTexture, -Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT - Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH / 2, Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH, Constants.WALL_HEIGHT, 1, 1, 0, 0, 0, 720, 64, false, false);
spriteBatch.draw(wallTexture, -Constants.WORLD_WIDTH, -Constants.WORLD_HEIGHT - Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH / 2, Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH, Constants.WALL_HEIGHT, 1, 1, 0, 0, 0, 720, 64, false, false);
Thanks
Related
I am trying to draw a gradient rectangle that goes from red to blue. I have the following code:
g2d.setPaint(new GradientPaint(0, 0, Color.RED, 1000, 1000, Color.BLUE));
g2d.fillRect(0, 0, 1000, 1000);
This is working. However, the direction of the gradient is diagonal, from the top left point of the rectangle to the bottom right point (another way to look at this is that the gradient follows the line of y=-x + windowHeight)
I would like my gradient to go from top to bottom. So the entire top of the rectangle is red, and the entire bottom is blue. In other words, the color should only change with the y coordinate, given any y=point line the color should be uniform across that line.
I have included the following images also to give a general idea of what I am trying to do:
How can I accomplish this?
It all has to do with the vector of your gradient. Here: (0, 0, Color.RED, 1000, 1000, Color.BLUE) you're vector is a diagonal vector that originates at [0, 0], and then ends or points at [1000, 1000] or on a 45 degree angle.
Change that to straight down: [0, 0] going to [0, 1000] should work well. e.g,
new GradientPaint(0, 0, Color.RED, 0, 1000, Color.BLUE)
I have a triangle like this;
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(1, 1, 0, 1);
shapeRenderer.polygon(new float[] { -10, 0, 10, 0, 0, 200 });
shapeRenderer.rotate(0, 0, 1, 1);
shapeRenderer.end();
and I rotate 1 degree in each render. But I want to fix rotation (e.g. 45) to an angle. How can I do this?
Thanks.
To have a fixed rotation you hav to rotate the ShapeRenderer only once.
There are 2 possible ways i can think about:
call shapeRenderer.rotate(0, 0, 1, 45); in the constructor or in create() / show() method
This call rotates your ShapeRenderer by 45° (last parameter) arround the Z-Axis (The 3rd parameter)
call shapeRenderer.rotate(0, 0, 1, 45); in the rendermethod, only if you did not rotate yet. So you have to keep a boolean rotated and only if it is false you call rotate() and set it to true.
To answer the question in your comment: You cannot directly set the rotation, you can only rotate (relative to the current rotation). So i would suggest to store a float rotation, and everytime you rotate your ShapeRenderer you set the new value. To set a rotation in degrees you have to rotate like:
shapeRenderer.rotate(0, 0, 1, newRotation - rotation);
rotation = newRotation;
This works only if you always rotate arround the same axis, in your case the Z-axis. Else you would have to store 3 rotations (x,y,z). If you rotate arround a custom axis, defined by for example (0.1, 0.3, 0.6) you would need to calculate the rotation for all axes. But i don't really know how to do that. I think some Vectormath would do that. But i don't think you need that.
How do we know which texture is related to which mesh in OpenGL? In this example, we tell the mesh we use texture coordinates, but we don't say which texture (if we have more than one), and we don't tell the texture where to be drawn. How does it work? (I know the concept of UVs, but I don't know the concept of "where" the texture is drawn) :
mesh = new Mesh(true, 4, 6,
new VertexAttribute(VertexAttributes.Usage.Position, 3,"attr_Position"),
new VertexAttribute(Usage.TextureCoordinates, 2, "attr_texCoords"));
texture = new Texture(Gdx.files.internal("data/img.png"));
mesh.setVertices(new float[] {
-1024f, -1024f, 0, 0, 1,
1024f, -1024f, 0, 1, 1,
1024f, 1024f, 0, 1, 0,
-1024f, 1024f, 0, 0, 0
});
#Override
public void render() {
// Texturing --------------------- /
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glEnable(GL10.GL_TEXTURE_2D);
texture.bind();
mesh.render(GL10.GL_TRIANGLES);
}
OK I actually found the solution : https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/DecalTest.java
I recently looked at the OpenGL ES 2.0 Tutorial provided by developers.android.com. I succesfully finished it (even if it wasn't very clear) but then I bumped into a problem. Once I finished it, I was never told how to translate or scale objects. I tried different options that seemed logical at the moment but they didn't work. I am not very expirienced in OpenGL ES 2.0 in android.
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
long time = SystemClock.uptimeMillis() % 4000L;
mAngle = 0.090f * ((int) time);
Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);
t.draw(mMVPMatrix);
All of these matrices are size 16 float arrays. My question is, how could I do a translation with an x and y position and sam for scale (a float with the scale)? There seems to be mo setTranslateM method and, when I tried the alternative methods I was not able to make them work. What do I do?
To translate/scale/rotate your objects, you should apply these operations to your Model Matrix.
Example:
Matrix.setIdentityM(mMMatrix, 0); // reset transformations
Matrix.setRotateM(mMMatrix, 0, 0, 1.0f, 0, 0); // set rotation
Matrix.translateM(mMMatrix, 0, 10.0f, 20.0f, 0); // apply translation
Matrix.scaleM(mMMatrix, 0, 0.25f, 0.25f, 0.25f); // apply scale
Please note that certain static methods of Matrix class do memory allocations so if you do objects transformation on each frame you may want not to use them. Read more about this here http://groups.google.com/group/android-developers/browse_thread/thread/b30dd2a437cfb076?pli=1 and in my blog post here http://androidworks-kea.blogspot.com/2012/05/developers-notes-about-opengl-es.html
I am creating custom SWT widget but I have got problem with transparency. My class extends canvas, I have got png with alpha image in my resources, when I simply write:
this.setBackgroundImage(Colors.getMenuButton()); //getMenuButton returns Image object
everything works ok (with transparency), but my object has to be resizeable so I decided to create funcion:
protected Image BGHelper(Image src) {
Image i2 = new Image(Display.getDefault(),2,26);
GC gc2 = new GC(i2);
Image image = new Image(Display.getDefault(),this.getBounds().width,26);
GC gc = new GC(image);
gc.drawImage(src, 0, 0, 3, 25, 0, 0, 3, 26);
gc2.drawImage(src, 3, 0, 2, 25, 0, 0, 2, 26);
gc.drawImage(i2, 0, 0, 2, 25, 3, 0, this.getBounds().width-6, 26);
gc.drawImage(src,53, 0, 3, 26, this.getBounds().width-3, 0, 3, 26);
gc.dispose();
gc2.dispose();
return image;
}
it cuts left border from source and paste into result, cut center from source, resize and paste into result, cut right border from source and paste into result. Resizing works but there is no transparency (white pixel). Why?
please look at following doc.It might help you to solve your issue.
http://www.eclipse.org/articles/Article-SWT-images/graphics-resources.html#Transparency