Problem with rotation around the center OpenGL - java

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.

Related

3D rotation ratios via mouse placement on screen - Java

I am making a 3D game in which the player can rotate their view point via the mouse to look around the environment. I firstly just did x and y rotation via vertical and horizontal movement of the mouse and z via another control. But after playing the game I realised it did not rotate correctly. NOTE: I have a global variable matrix which represents the player's angle (3x1), at 0,0,0 it seems to work correctly as up or down is a direct x axis rotation and right or left is a direct y axis rotation, but if I move my camera diagonally for example then left doesn't directly correlate to a y axis rotation anymore.
Visually on a unit circle the players viewpoint wouldn't travel the full circumference anymore and would travel in a circle that is smaller that the circumference. This is the current code (x and yRateOfRot is the ratio of how far away from the centre the cursor is in each direction between -1 and 1):
private static void changeRotation(){
angle.set(Matrix.add(angle.matrix,new double[][]{
{ROTATION_SPEED * camera.xRateOfRot()},
{ROTATION_SPEED * camera.yRateOfRot()},
{ROTATION_SPEED * camera.zRateOfRot()}}));
}
I have looked at this source http://paulbourke.net/geometry/rotate/ and understand how to rotate via an arbitrary axis which I could do but I am not sure how to correlate this into getting a ratio to find out what the x,y and z change would be for looking in a specific direction i.e. at 0,0,0 the ratio of looking up would be x:1, y:0, z:0 but then at another angle the ratios would be different as looking up no longer means only an x rotation. Any information would be appreciated, thanks!

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

Libgdx sprite rotation causing position offset

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

How to properly apply the transformations of a ModelInstance in libGDX?

I am writing my first 3D libGDX game, and I am stuck with the following scenario:
I have a scene, loaded from a g3db file. The ModelInstance I am trying to transform is controlled by a separate class. In its move() method I am currently trying to rotate the ModelInstance around its own Y coordinate. The code for that is the following:
Vector3 position = model.calculateBoundingBox(new BoundingBox()).getCenter();
model.transform.translate(-position.x, 0f, -position.z);
model.transform.rotate(Vector3.Y, (float) (Math.toDegrees(Math.acos(adjacent/hypotenuse))));
model.transform.translate(position.x, 0f, position.z);
model.calculateTransforms();
FYI, adjacent and hypotenuse are used to find the cosine of the angle between the Y center of the screen and the line between the center of the screen and the point touched.
What I am trying to do with this code is to translate model (the ModelInstance) to the center of the coordinate system, rotate it around world's Y and translate it back to where it originally was. Instead, model rotates around world's Y with the original offset from the center. How do I apply the desired transformation properly?

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

Categories