I want to rotate multiple texture same like this picture on libgdx.
How can do this? Please help me.
You can rotate them the same as you do for a single texture, but you need to set the origin point for each texture to the centre of the rotation arc (bottom left point in the case of your image), so some basic maths will be required on your part to set each texture correctly so that the further away ones rotate around the same point as the closest texture.
From the API: https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html
//Set the origin in relation to the sprite's position for scaling and rotation.
yourSprite.setOrigin(float originX, float originY)
//Sets the sprite's rotation in degrees relative to the current rotation.
//Rotation is centered on the origin set in setOrigin(float, float)
yourSprite.rotate(float degrees)
These questions and answers may help you:
https://gamedev.stackexchange.com/a/75330
https://gamedev.stackexchange.com/questions/119870/libgdx-sprite-rotation-around-specific-point
Related
I have a problem with rotation around the center of an object.
I have a plane and I found out its center x,y the z for translation is used for moving.
I want to rotate that around it is center, but not around 0,0,0
But it keeps that way.
Can you give me some advice how to get it work?
glRotatef(moveLeftRight, 1,0,0);
glTranslatef(xT,yT, thrustP);
xT and yT are the points, thrustP is Z like moving. MoveLeftRight - rotation.
I am building a small testing game, basically where is waldo. I have a large image that I can pan around and look for Waldo, but I can't figure out how to keep the camera within the sprite borders (x, y). Right now you can pan past the image borders and on and on and on forever.
Relevant code:
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
public boolean pan(float x, float y, float deltaX, float deltaY) {
camera.translate(-deltaX * PAN_SPEED, deltaY * PAN_SPEED);
camera.update();
return true;
}
there isn't much, I've tried quite a few things but the problem is I can't figure out how to get the distance I have panned, and I need that if I am going to put up a "border". Right now the sprite.getX() == -2200), and the camera viewport is only (480x800), so I am having a hard time working with the Image size and the viewport, and the distance that has been panned.
I've had to solve this before, but I did it very inelegantly. I basically just had a log printing out my current camera's position as I panned around. When I could see off the edge of the screen (to the blank GL wipe underneath), I wrote down the camera's position.
I ended up knowing that if the camera.getX() > sprite.getWidth() - someVal, the edge would on screen. So, I just added in a method that clipped down any X/Y val of a camera if it over shot these predefined bounds.
It's not a great answer, but it also allows you control.
I have a little tech game I am messing around with and I can't figure out the formula to position 1 object given another objects origin.
So I have a Spaceship and a Cannon. I have the game setup to use units, so 1 unit = 16 pixels (pixel art).
Basically my cannon should be placed 0.5625 units on the X and 0 on the Y relative to the origin of the Spaceship, which is located at 0, 0 (bottom left corner).
The cannon should is independent on the angle of the spaceship, it can aim in different directions rather than being fixed to aim the way of the spaceship.
I have it constantly following the cursor, which works fine. Now when I rotate the Spaceship, obviously the origin of the Spaceship is changing in world coordinates, so my formula to place the cannon is all messed up, like so:
protected Vector2 weaponMount = new Vector2();
weaponMount.set(getBody().getPosition().x + 0.5625f, getBody()
.getPosition().y);
Obviously if I position the ship at a 90° angle, X is going to be different and the cannon would be waaaayyy off the ship. Here is a screenshot example of what I mean:
What would be the formula for this? I have tried using cos/sin but that does not work.
Any ideas?
weaponMount.set(0.5625f,0).setAngle(SpaceshipAngle).add(getBody().getPosition());
Where SpaceshipAngle is the angle of your Spaceship.
The origin of the spaceship is the point, arround which the spaceship will rotate and scale (the Texture of it). The position instead is always the lower left corner of the Texture and does not depend on the rotation.
Your problem is, that your offset does not depent on the rotation of your spaceship.
To take care about this rotation you should store a Vector2 offset, which describes your weapons offset (in your case it is a Vector2(0.5625f, 0)).
Next store a float angle describing your spaceships rotation.
Then you can rotate the offset by using: offset.setAngle(rotation).
The last thing is to set the weapons position. The code for this did not change so much:
weaponMount.set(getBody().getPosition().x + offset.x, getBody()
.getPosition().y + offset.y);
I have rotated an image in libgdx using this method,
SpriteBatch.draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
But i dont know how to get the co-ordinates of the rotated image.
Instead of rotating the texture at draw time, you could use a Sprite (or your own object that tracks its current location).
The draw call doesn't store any of the rotation results, it just rotates the texture as its copied to the screen. The Sprite class will keep track of the location and rotation so you can query it. (You should use the Sprite's draw method to draw it.)
The Sprite will also give you an axis-aligned bounding box. If you want the precise location of the rotated corners of your texture, you'll need to extract it from getVerticies() or compute it yourself from the location and rotation.
I don't get the question. I read it a few times, but I don't even get the core of what you want to know. If you are drawing an image with spriteBatch.draw(), your drawn image will be placed on the coorinates of the second and third parameter, and rotated/scaled around the relative origin.
If you want to know where the image is drawn on the screen, you should consider using camera.project(Vector3); with a 3 dimensional vector where x,y are your coordinates of your image and z = 0;
The vector will be multiplied by the matrix of the camera, so that x,y will be the screen-coorinates.
Or better, you should rotate your image before drawing. If you do this, you can get rotation from your sprite/image object any time you want.
I am attempting to use texture coordinates from a pre-generated PNG file on a 3d world of quads loaded into Java with LWJGL's slick-util extension.
The texture file is 192x96pixels, and properly formatted. It's composed of 6x3 32x32 tiles.
The 3d quads are 1.5f wide and long. They are spaced apart properly.
I am having issues getting the right texture coordinates. When I put 0.0f to 0.333333f as the y coordinates, I get slightly more than the top tile's height displayed. However, if I put 0.0f-0.25f, I get exactly 1/3rd, which is my tile's height. I have yet to find a magic number for the X coordinates, but maybe someone could explain to me why 1/4 of 96 is 24 according to textures coordinates, or what I'm doing wrong? I'm suspecting it could be a clash between my quad size and textures.
The tops of the cubes are using the texture coordinates (0.0, 0.0f), (0.0, 0.333333f), (0.166666f, 0.333333f), (0.166666f, 0.0f), which is applied moving anticlockwise from the top left to the top right. Again, the main texture file is 32x32 tiles arranged to make 192x96(96 is the height).
Notice I placed a white line at the top of one of the tiles to see its border, and black line at it's bottom, then a white line for the top of the next one below it. The texture 'bleeds' too far down. The other textures have their own even stranger coordinates, as you can see.
Arranging texture coordinates with the assumption the top of the image is 1.0 rather than the bottom produces odd squares with a rectangular hole in the center where quads should be.
I am using TEX_ENV GL_MODULATE.
Texture sizes are usually a power of 2. I suspect something resized your 192x96 texture as a 256x128 or 256x256 texture. This doesn't really explain the values you found however... But, I think, if you resize your texture to 256x256(increase the size, don't scale!) and calculate your texture coordinates based on that, your problem will go away.
I don't know about java but with my image atlases in objective C and openGL ES you need to make the textures smaller than what you are referring to when selecting them from the atlas.
Have you left a sufficient gap between the texture images to prevent 'bleeding?