Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (756)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Open Gl rotate object on its origin  (Read 3872 times)
0 Members and 1 Guest are viewing this topic.
Offline tommohawkaction
« Posted 2015-05-29 18:22:38 »



My transformation matrix

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
   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;
   }
Offline Jesse

JGO Coder


Medals: 23



« Reply #1 - Posted 2015-05-29 18:38:33 »

I assume you're asking why it isn't working correctly? Smiley

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.
Offline tommohawkaction
« Reply #2 - Posted 2015-05-29 18:44:51 »

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 Smiley
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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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);
   }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jesse

JGO Coder


Medals: 23



« Reply #3 - Posted 2015-05-29 18:50:30 »

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)?
Offline tommohawkaction
« Reply #4 - Posted 2015-05-29 18:53:58 »

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

I hope this is what you meant?
Offline Jesse

JGO Coder


Medals: 23



« Reply #5 - Posted 2015-05-29 19:00:59 »

Quote
I hope this is what you meant?

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.
Offline tommohawkaction
« Reply #6 - Posted 2015-05-29 19:03:22 »

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?
Offline Jesse

JGO Coder


Medals: 23



« Reply #7 - Posted 2015-05-29 19:16:09 »

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:

1  
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?
Offline tommohawkaction
« Reply #8 - Posted 2015-05-30 11:54:50 »

Yeah the comments are wrong i forgot to update them

Here is how i make a cube

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
   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;
   }


Offline tommohawkaction
« Reply #9 - Posted 2015-05-30 12:03:54 »

this is what happens when i dont offset it

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tommohawkaction
« Reply #10 - Posted 2015-05-30 12:23:29 »

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

Offline tommohawkaction
« Reply #11 - Posted 2015-05-30 12:56:02 »

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)

Offline tommohawkaction
« Reply #12 - Posted 2015-05-30 13:05:39 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   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
1  
2  
3  
4  
5  
6  
7  
            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
1  
2  
3  
4  
5  
         int type = GL20.GL_LINE_STRIP;
         if(wireframe == false){
            type = GL20.GL_TRIANGLES;
         }
         rawModel.getMesh().getMeshData().render(shader, type);

Offline Jesse

JGO Coder


Medals: 23



« Reply #13 - Posted 2015-05-30 15:47:56 »

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.)
Offline tommohawkaction
« Reply #14 - Posted 2015-05-30 15:54:51 »

Sorry the collision is fixed
Rotation is fixed

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

JGO Coder


Medals: 23



« Reply #15 - Posted 2015-05-30 16:06:05 »

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?)
Pages: [1]
  ignore  |  Print  
 
 

 
DesertCoockie (52 views)
2018-05-13 18:23:11

nelsongames (83 views)
2018-04-24 18:15:36

nelsongames (74 views)
2018-04-24 18:14:32

ivj94 (759 views)
2018-03-24 14:47:39

ivj94 (87 views)
2018-03-24 14:46:31

ivj94 (643 views)
2018-03-24 14:43:53

Solater (102 views)
2018-03-17 05:04:08

nelsongames (184 views)
2018-03-05 17:56:34

Gornova (426 views)
2018-03-02 22:15:33

buddyBro (1086 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!