Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (472)
Games in Android Showcase (105)
games submitted by our members
Games in WIP (524)
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  
  Rotation around Center  (Read 1383 times)
0 Members and 1 Guest are viewing this topic.
Offline randy

Senior Newbie





« Posted 2010-04-08 11:33:13 »

Hi guys,

I want to rotate a mesh around its center (kinda like in a arcball). The mesh is read in from a .obj file.
The way I have it now, the model is rotating around a point which is not its center (kinda like if you run around a house in circles).

I read that I would have to translate the object to 0,0,0 and then rotate, but I have no ideas which values I should take.

Oh, I am using JOGL btw.

Here's the code for rendering:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   
public void display(GLAutoDrawable drawable) {
      final GL gl = drawable.getGL();      
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
      gl.glLoadIdentity();
     
      if(zoom)
         setProjectionMatrix(gl);
     
      drawBone(gl);

     
      gl.glFlush();


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   
private void drawBone(GL gl){
      gl.glPushMatrix();
     
      gl.glTranslated(0, 0,0);      
           
      gl.glRotated(rotX, 1, 0, 0);
      gl.glRotated(rotY, 0, 1, 0);
     
      if(faces != null){
         for(Face f: faces){
            f.draw(gl);
         }
      }

      gl.glPopMatrix();
   }


reshape:

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  
   public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
      GL gl = drawable.getGL();
      if(h<=0){
         this.height=1;
      }
      else{
         this.height = h;
      }
      this.width = w;
     
      aspect = (float)width/(float)height;
     
      // Set the viewport to be the entire window
     gl.glViewport(0, 0, width, height);
     
      // Reset the coordinate system before modifying
     gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
     
      // Set the correct perspective.
     glu.gluPerspective(DEFAULT_ZOOM*zoomFactor, aspect, 1.0, 1000.0f);
     
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
     
      glu.gluLookAt(0.0, 0.0, 5.0,
      0.0, 0.0, 0.0,
      0.0, 1.0, 0.0);


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   
public void setProjectionMatrix(GL gl){
      GLU glu = new GLU();
     
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
     
      glu.gluPerspective(DEFAULT_ZOOM*zoomFactor, aspect, 1f, 100.0f);
     
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
   }


Any help is much appreciated. Thank you!
Offline elias4444

Junior Member





« Reply #1 - Posted 2010-04-08 17:22:16 »

If you plan on rotating the object from it's center a lot, you probably want to go ahead and center it's vertices around a 0,0,0 origin on import.

Offline DzzD
« Reply #2 - Posted 2010-04-08 18:50:00 »

tha following have no effect

1  
gl.glTranslated(0, 0,0);


what you probalby want to do is

1  
gl.glTranslated(-centerX, -centerY,-centerZ);


where centerX,centerY,centerZ is the center of your object vertices in local object space => sum all your vertices and divide each result by number of vertices (inded do it only once and apply same transform to it if you apply/modify vertices of your object (zoom for example...) like if it was another vertex)
1  
2  
3  
4  
5  
6  
7  
8  
9  
for(n=0;n<nbVertices;n++) 
{
centerX+=v[n].x;
centerY+=v[n].y;
etc....
}
centerX/=nbVertices;
centerY/=nbVertices;
etc...

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline randy

Senior Newbie





« Reply #3 - Posted 2010-04-09 08:50:54 »

If you plan on rotating the object from it's center a lot, you probably want to go ahead and center it's vertices around a 0,0,0 origin on import.

yes, the rotation will be only done around the objects center Smiley how would i center the vertices around the origin at import? with the method DzzD described below or is there a simpler way ?

what you probalby want to do is

1  
gl.glTranslated(-centerX, -centerY,-centerZ);


where centerX,centerY,centerZ is the center of your object vertices in local object space => sum all your vertices and divide each result by number of vertices (inded do it only once and apply same transform to it if you apply/modify vertices of your object (zoom for example...) like if it was another vertex)
1  
2  
3  
4  
5  
6  
7  
8  
9  
for(n=0;n<nbVertices;n++) 
{
centerX+=v[n].x;
centerY+=v[n].y;
etc....
}
centerX/=nbVertices;
centerY/=nbVertices;
etc...


thanks for your suggestion, i will try this and report back my results Smiley
Offline randy

Senior Newbie





« Reply #4 - Posted 2010-04-09 10:28:13 »

okay, i did what you said and it seems it's better now Smiley

instead of the object flying in circles around "my head", i can see it now in front of me and it's rotating. but still not around its center, it looks like it's attached to a point at its lower side and it's going around circles there Sad
Offline DzzD
« Reply #5 - Posted 2010-04-10 18:03:18 »

may be verify the value of centerx,centery,centerz to see if they match the intended object center location. averaging vertices to find center may not work as expected in severals cases.

Offline randy

Senior Newbie





« Reply #6 - Posted 2010-04-12 08:54:53 »

hi DzzD

it might be a stupid question, but how can i find out the intended object center location coordinates ?
Offline DzzD
« Reply #7 - Posted 2010-04-12 09:47:22 »

hi DzzD

it might be a stupid question, but how can i find out the intended object center location coordinates ?
find the bouding box : find min X,max X min Y max Y, min Z max Z of all vertices this will give you the bouding box then you got a pretty good center point with
centerX=(maxX+minX)/2;
centerY=(maxY+minY)/2;
centerZ=(maxZ+minZ)/2;

Offline randy

Senior Newbie





« Reply #8 - Posted 2010-04-12 11:13:50 »

Thank you very much for your answer DzzD.

The bounding box coordinates are around the same coordinates as the ones I calculated before with summing up each axis and dividing through number of points, so that seems to be okay.

1  
2  
getCenterVertex at : 119.68530292026117/62.29729093334656/239.88274836337095
Bounding box coordinates at : 116.318302/67.105699/248.7705065


I still don't get why it is not rotating around the centroid Sad

Did I set up my view wrong ?

Offline kevglass

JGO Kernel


Medals: 98
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2010-04-12 11:25:16 »

Sure there isn't some junk data in the original mesh which is skewing your center?

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline randy

Senior Newbie





« Reply #10 - Posted 2010-04-12 13:18:18 »

I'm not really sure, no. How would I go and check this ?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (12 views)
2014-07-10 02:26:14

CopyableCougar4 (24 views)
2014-07-09 02:55:38

Code Mage (23 views)
2014-07-08 23:57:00

Code Mage (8 views)
2014-07-08 23:49:08

AppleSauce (19 views)
2014-07-08 19:25:32

CopyableCougar4 (21 views)
2014-07-06 01:51:26

ipe369 (26 views)
2014-07-05 14:18:25

vastrolorde (36 views)
2014-07-04 18:45:44

theagentd (53 views)
2014-07-02 22:19:37

theagentd (50 views)
2014-07-02 22:17:51
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!