Libgdx sprite rotation causing position offset - java

Hi I am creating a game for android using the Libgdx framework. I am trying to place a sprite in the middle of the X axis near the top of my screen. I can do this just fine but since the sprite needs to be rotated 90 degrees(to save room on spritesheet) it causes some sort of offset and I don't understand why. I believe it is because of the origin rotation but I have tried setting the origin to the center of the sprite but it still doesn't work as planned. Please can you help me understand why the rotation is causing an offset and how it can be fixed thank you.
regions [1] = new TextureRegion(menu, 395, 0, 115, 320);
logo=new Sprite(regions[1]);// the sprite
logo.setPosition(W/2-logo.getWidth()/2,H*0.95f); //draw right in the middle of X and 95% up Y
logo.rotate(90); //rotate the sprite 90 degrees
logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2); //rotate around the centre of the sprite
logo.draw(spriteBatch); //in render method draw the sprite

You set the origin of the sprite after you rotate it:
logo.rotate(90); //rotate the sprite 90 degrees
logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2); //rotate around the centre of the sprite
you should do it the other way round. First set origin, then rotate:
logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2);
logo.rotate(90);

Related

Problem with rotation around the center OpenGL

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.

How to Rotate AWT rectangle in JAVA?

I am creating a small java 2D game and i want to know if there is any way to rotate AWT rectangle
AffineTransform origXform = g2d.getTransform();
AffineTransform newXform = (AffineTransform) origXform.clone();
newXform.rotate(angle, pivotX, pivotY); // pivotX ,pivotY is the starting
point of the hand image
g2d.setTransform(newXform);
Rectangle arm = new Rectangle(bowX + 5, bowY + 55, 60, 5);
g2d.drawImage(playerBowReadyImg, bowX, bowY, null); //hand image
on the above code i simply draw the hand image which rotates based on the mouse position, i also set the rectangle on the hand but the problem is rectangle is not rotating along with the hand image.
also i am not using rectangle for any drawing purpose but to detect the collision.
however using g2d.draw(arm); draws the rotated rectangle but it not actually rotate the rectangle it just draws the rotated one.
any suggestion is appreciated.
Ok my question is marked as duplicate so i tried the answers i find there but the code i get only rotate my rectangle for the draw purpose only.
Image to depict the problem
now to be more specific the arrow in the image can only detect the collision for the blue rectangle (the original position) instead of the red one (rotated rectangle).
again i don't want to actually draw the rectangle but want to detect the collision when the arrow collide with the rectangle.
See AffineTransform.createTransformedShape(Shape) which:
Returns a new Shape object defined by the geometry of the specified Shape after it has been transformed by this transform.

Libgdx, How can rotate multiple texture same like one texture?

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

Position Vector relative to origin

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);

Rotation in OpenGL ES

I am trying to rotate a sphere continuously on a given axis using this code:
gl.glRotatef(axisX, 0, 1, 0);
gl.glRotatef(axisY, 0, 0, 1);
axisX = (axisX+1)%360;
axisY = (axisY+1)%360;
The variables axisX and axisY are both being incremented by one right now which would make the rotation go in a diagonal direction up to the right. The object gets to about 45 degrees of rotation and then start to turn and start rotation the other way. How can I get it to continuously rotate on an axis other than just x and y by themselves?
Note: I am trying to hook up a virtual joystick to control the axisX and axisY values and have the sphere rotate on the axis represented by the joystick. If anyone has any advice on that, that would be great as well.
Edit:
I've change it so that if I use gl.glRotatef(angle, axisX, axisY, axisZ); that it works and keeps the rotation going, but the rotation isn't smooth, it looks like the rotation starts over when I switch the rotation axis.
glLoadIdentity(); // Reset rotation and give a new one
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
The first argument is the angle, then the axis you want to pivot around.
About your edit: Looks like you are mixing axis and angles here. The axis should a unit length vector and the angle go from 0 to 2*PI.
Another way would be to use quaternions, then transform the quaternion to matrix and load the matrix as your current modelview matrix.
Hope that helps.

Categories