Open Gl rotate object on its origin

My transformation matrix

	public static Matrix4 createTransformationMatrix(Vector3 position, Vector3 rotation,Vector3 scale){
		Matrix4 result = new Matrix4();
		Vector3 newPos = new Vector3(position.x,position.y,position.z+scale.z);
		result.translate(newPos);
		result.rotateRad(1, 0, 0, (float)Math.toRadians(rotation.x));
		result.rotateRad(0, 1, 0, (float)Math.toRadians(rotation.y));
		result.rotateRad(0, 0, 1, (float)Math.toRadians(rotation.z));
		result.scale(scale.x, scale.y, scale.z);
		return result;
	}

I assume you’re asking why it isn’t working correctly? :slight_smile:

I’m guessing the green wireframe boxes represent the correct bounds for the objects, and you’re wanting the objects still to be centered in the bounds after rotation. Is that right?

Also, just out of curiosity, why are you applying the ‘z’ scale to the position separately?

As for why it’s not working, here’s a couple things you can check:

  • Make sure your meshes are actually built so that they’re centered at the origin (or otherwise positioned how you want them).

  • Make sure your order of operations is right. I don’t know how the API you’re using works, but you could try scale-rotate-translate rather than translate-rotate-scale. Scale-rotate-translate is what you want geometrically, but the order in which to issue the transform commands will depend on the API. That said, I’m guessing the API expects translate-rotate-scale (as you have currently), and the image doesn’t really look like an order-of-operations issue, so maybe the problem is somewhere else.

Hello thanks for replying I am not sure why i had that z-scale there but it has been removed and has fixed the other bug i have been having whoops :slight_smile:
Yes the boxes are actually collision meshes but is centered where the object is before rotation I am not sure what you mean by check my mesh creation

here is how i create a rectangle

public static HawkMesh createRectangle() {
		float[] vertices = { 
				0, 0, -.5f, // bottom left
				0, 1, -.5f, // bottom right
				1, 1, -.5f, // top right
				1, 0, -.5f,
				
		};
		float[] texCoords = { 0,1,0,0,1,0,1,1 };
		float[] normals = { 0, 1, 0
				, 0, 1, 0
				, 0, 1, 0
				, 0, 1, 0 };
		short[] indices = {
				2,1,0,
				0,3,2,};
		return new HawkMesh(true, vertices, indices, texCoords, normals, 1, 1);
	}

How are your meshes intended to be oriented in local space? Like for the character in the image you posted, which axis points up out of his head, and which axis points directly forward (the direction he’s looking)?

+Y = Up
-Y = Down
+X = right
-X = left
-Z = backward (Away from camera)
+Z = towards camera

I hope this is what you meant?

[quote]I hope this is what you meant?
[/quote]
Yes, that’s right.

Here are some things I notice:

  • Your sprite mesh appears to be anchored in the lower-left corner (that is, that’s where the origin is). Maybe this is what you want, but it seems like it’d make more sense either to anchor it in the center, or in the bottom-center (e.g. right between the feet for the character sprite).

  • Your mesh is offset by -.5 in the z direction. Is there a reason for that? That (perhaps in conjunction with the scale factor you had in there earlier) could explain the incorrect rotation in the image you posted.

  • The mesh normals appear to be parallel to the y axis, but I’d think you’d want them to be parallel to the z axis.

The .5f is for centering inside the box as all rectangles in my game will be centered in a box
I am not sure what you mean could you explain? possible examples?

Why do you need to offset the mesh to center it in the box? That suggests that something’s wrong elsewhere (e.g. the box itself isn’t centered). Actually, maybe you could post the code where you construct the mesh for the box.

Here’s something else that seems odd:

0, 1, -.5f, // bottom right

You said +y is up, so I wouldn’t expect (x = 0, y = 1) to be one of the bottom corners. That does seem to match how your texture coordinates are arranged though. Are you sure your comments there (bottom left, bottom right, etc.) are correct? Have you perhaps mis-labeled the corners?

Yeah the comments are wrong i forgot to update them

Here is how i make a cube

	public static HawkMesh createCube() {
		if(cube == null){
		List<Float> vertices = new ArrayList<Float>();
		List<Float> normals = new ArrayList<Float>();
		List<Float> textureCoords = new ArrayList<Float>();
		List<Short> indices = new ArrayList<Short>();
		// Face
		indices.add((short) (vertices.size() / 3 + 0));
		indices.add((short) (vertices.size() / 3 + 1));
		indices.add((short) (vertices.size() / 3 + 2));

		indices.add((short) (vertices.size() / 3 + 2));
		indices.add((short) (vertices.size() / 3 + 3));
		indices.add((short) (vertices.size() / 3 + 0));

		addVertex(1, 0, 0, vertices);
		addVertex(1, 1, 0, vertices);
		addVertex(0, 1, 0, vertices);
		addVertex(0, 0, 0, vertices);

		addNormals(0, 0, 1, normals);
		addNormals(0, 0, 1, normals);
		addNormals(0, 0, 1, normals);
		addNormals(0, 0, 1, normals);

		addTextureCoords(0, 0, textureCoords);
		addTextureCoords(1, 0, textureCoords);
		addTextureCoords(1, 1, textureCoords);
		addTextureCoords(0, 1, textureCoords);

		// Back
		indices.add((short) (vertices.size() / 3 + 2));
		indices.add((short) (vertices.size() / 3 + 1));
		indices.add((short) (vertices.size() / 3 + 0));
		
		indices.add((short) (vertices.size() / 3 + 0));
		indices.add((short) (vertices.size() / 3 + 3));
		indices.add((short) (vertices.size() / 3 + 2));

		addVertex(1, 0, -1, vertices);
		addVertex(1, 1, -1, vertices);
		addVertex(0, 1, -1, vertices);
		addVertex(0, 0, -1, vertices);

		addNormals(0, 0, 1, normals);
		addNormals(0, 0, 1, normals);
		addNormals(0, 0, 1, normals);
		addNormals(0, 0, 1, normals);

		addTextureCoords(0, 0, textureCoords);
		addTextureCoords(1, 0, textureCoords);
		addTextureCoords(1, 1, textureCoords);
		addTextureCoords(0, 1, textureCoords);
		// Left
		indices.add((short) (vertices.size() / 3 + 2));
		indices.add((short) (vertices.size() / 3 + 1));
		indices.add((short) (vertices.size() / 3 + 0));
		
		indices.add((short) (vertices.size() / 3 + 0));
		indices.add((short) (vertices.size() / 3 + 3));
		indices.add((short) (vertices.size() / 3 + 2));
		
		addVertex(0, 0, 0, vertices);
		addVertex(0, 0, -1, vertices);
		addVertex(0, 1, -1, vertices);
		addVertex(0, 1, 0, vertices);
		
		addNormals(-1, 0, 0, normals);
		addNormals(-1, 0, 0, normals);
		addNormals(-1, 0, 0, normals);
		addNormals(-1, 0, 0, normals);
		
		addTextureCoords(0, 0, textureCoords);
		addTextureCoords(1, 0, textureCoords);
		addTextureCoords(1, 1, textureCoords);
		addTextureCoords(0, 1, textureCoords);
		// Right
		indices.add((short) (vertices.size() / 3 + 0));
		indices.add((short) (vertices.size() / 3 + 1));
		indices.add((short) (vertices.size() / 3 + 2));
		
		indices.add((short) (vertices.size() / 3 + 2));
		indices.add((short) (vertices.size() / 3 + 3));
		indices.add((short) (vertices.size() / 3 + 0));
		
		addVertex(1, 0, 0, vertices);
		addVertex(1, 0, -1, vertices);
		addVertex(1, 1, -1, vertices);
		addVertex(1, 1, 0, vertices);
		
		addNormals(1, 0, 0, normals);
		addNormals(1, 0, 0, normals);
		addNormals(1, 0, 0, normals);
		addNormals(1, 0, 0, normals);
		
		addTextureCoords(0, 0, textureCoords);
		addTextureCoords(1, 0, textureCoords);
		addTextureCoords(1, 1, textureCoords);
		addTextureCoords(0, 1, textureCoords);
		// Bottom
		indices.add((short) (vertices.size() / 3 + 0));
		indices.add((short) (vertices.size() / 3 + 1));
		indices.add((short) (vertices.size() / 3 + 2));
		
		indices.add((short) (vertices.size() / 3 + 2));
		indices.add((short) (vertices.size() / 3 + 3));
		indices.add((short) (vertices.size() / 3 + 0));
		
		addVertex(0, 0, 0, vertices);
		addVertex(0, 0, -1, vertices);
		addVertex(1, 0, -1, vertices);
		addVertex(1, 0, 0, vertices);
		
		addNormals(0, -1, 0, normals);
		addNormals(0, -1, 0, normals);
		addNormals(0, -1, 0, normals);
		addNormals(0, -1, 0, normals);
		
		addTextureCoords(0, 0, textureCoords);
		addTextureCoords(1, 0, textureCoords);
		addTextureCoords(1, 1, textureCoords);
		addTextureCoords(0, 1, textureCoords);
		// Top
		indices.add((short) (vertices.size() / 3 + 2));
		indices.add((short) (vertices.size() / 3 + 1));
		indices.add((short) (vertices.size() / 3 + 0));
		
		indices.add((short) (vertices.size() / 3 + 0));
		indices.add((short) (vertices.size() / 3 + 3));
		indices.add((short) (vertices.size() / 3 + 2));
		
		addVertex(0, 1, 0, vertices);
		addVertex(0, 1, -1, vertices);
		addVertex(1, 1, -1, vertices);
		addVertex(1, 1, 0, vertices);
		
		addNormals(0, 1, 0, normals);
		addNormals(0, 1, 0, normals);
		addNormals(0, 1, 0, normals);
		addNormals(0, 1, 0, normals);
		
		addTextureCoords(0, 0, textureCoords);
		addTextureCoords(1, 0, textureCoords);
		addTextureCoords(1, 1, textureCoords);
		addTextureCoords(0, 1, textureCoords);

		float[] verticesArray = new float[vertices.size()];
		float[] normalsArray = new float[normals.size()];
		float[] texturesArray = new float[textureCoords.size()];
		short[] indicesArray = new short[indices.size()];

		for (int i = 0; i < vertices.size(); i++) {
			verticesArray[i] = vertices.get(i);
		}
		for (int i = 0; i < indices.size(); i++) {
			indicesArray[i] = indices.get(i);
		}
		for (int i = 0; i < normals.size(); i++) {
			normalsArray[i] = normals.get(i);
		}
		for (int i = 0; i < textureCoords.size(); i++) {
			texturesArray[i] = textureCoords.get(i);
		}

		cube = new HawkMesh(false, verticesArray, indicesArray, texturesArray, normalsArray);
		}
		
		
		return cube;
	}

this is what happens when i dont offset it

Okay well i have found the problem and ixed it but now my collision is broken ahhhh!

Now i have a problem with the height of the object

this is what i get (all objects have there position.y set to 0)

I am now stuck again…
I am trying to render more objects but calling mesh.render() every frame is eating everything up
so i need to make a method that binds an object and then render all the same objects and then unbind it for multiple times
How would i do this in Libgdx?
Code i have attempted

	for (Entity e : preparedEntity) {
				Mesh mesh = e.getModel().getModel().getMesh().getMeshData();
				Gdx.gl20.glEnable(GL20.GL_ARRAY_BUFFER_BINDING);
				
				Gdx.gl.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, Usage.Position); // <-- not sure if that is the VBO ID
				Gdx.gl30.glVertexAttribIPointer(GL20.GL_ELEMENT_ARRAY_BUFFER, mesh.getNumVertices(), GL20.GL_FLOAT, 0, 0);
				
				Gdx.gl.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, Usage.Normal);
				VertexAttribute normal = mesh.getVertexAttribute(Usage.Normal);
				Gdx.gl30.glVertexAttribIPointer(GL20.GL_ELEMENT_ARRAY_BUFFER, mesh.getNumVertices(), GL20.GL_FLOAT, 0,normal.offset);
				
				Gdx.gl.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, Usage.TextureCoordinates);
				VertexAttribute tex = mesh.getVertexAttribute(Usage.TextureCoordinates);
				Gdx.gl30.glVertexAttribIPointer(GL20.GL_ELEMENT_ARRAY_BUFFER, mesh.getNumVertices(), GL20.GL_FLOAT, 0,tex.offset);
				
				Gdx.gl.glDrawElements(GL20.GL_TRIANGLES, mesh.getNumIndices(), GL20.GL_UNSIGNED_INT, mesh.getIndicesBuffer());

My code before

				e.getModel().getTexture().bind(0);
				shader.setUniformMatrix(Utils.TRANSFORM_UNIFORM, e.getModel().getModel().getTransform().getTransformationMatrix());
				shader.setUniformf(Utils.COLOUR_UNIFORM, e.getModel().getModel().getColour());
				if (MasterRenderer.getRenderCollisionMesh()) {
					e.getCollisionMesh().unsignedRender(GameWindow.getDefaultShader(), GameWindow.getCamera());
				}
				e.getModel().unsignedRender(GameWindow.getDefaultShader(), GameWindow.getCamera(), false);

Inside render

			int type = GL20.GL_LINE_STRIP;
			if(wireframe == false){
				type = GL20.GL_TRIANGLES;
			}
			rawModel.getMesh().getMeshData().render(shader, type);

Based on your last few posts, I’m not sure which problems are fixed and which aren’t, so if you’re still stuck on something, perhaps you could clarify what the current issue is. (Also, if the current problem is unrelated to the original transform problems, you could probably just start a new thread.)

Sorry the collision is fixed
Rotation is fixed

I am now having a problem with rendering lots of objects smoothly

You might have better luck with a new thread for that (since people would know what the content of the thread was based on the title), but either way, some more info would be useful. For example, how many objects are you trying to render? And what problems with smoothness are you seeing? (Low frame rate? Jittery motion?)