What technique should be used? (moving 3d object in 2d space)

Started by
2 comments, last by GameDev.net 17 years, 4 months ago
Hey guys, I am using glunproject for picking and have a selected item (a 3d chess piece). I am now trying to make this model move with the mouse (kind of like its bound to the mouse). any suggestions on how to go about doing this? I was looking into ortho2d but wasn't sure if that was even what i wanted to use. When we were doing 2d stuff I had to convert mouse coordinates to screen space but how do i go about doing this in conjunction with the projection? right now my display looks like this:

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.3f, 0.3f, 0.3f, 0.0f);				
 
    glMatrixMode(GL_PROJECTION);    	   
    glLoadIdentity();
    gluPerspective(45.0f, (GLfloat) 6.4/4.8, 0.1f, 100.0f);
    glViewport (0,0,640,480);			
		    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();	    
	
    gluLookAt(0.0,18.0,-30.0,0.0,0.0,0.0,0.0,1.0,0.0);	
    glRotated(rotation,0.0,1.0,0.0);  //rotating camera

    //glPushMatrix();
    //Translated(?,?,?);
    //chess_piece[my_selected_model]->Draw();
    //glPopMatrix();

    Draw_board();
    Draw_pieces();
	
    glFlush();
    glutSwapBuffers();
}

Advertisement
A really simple way would be to calculate the plane of the board, then just move along this plane. So instead of converting mouse coord to world coord, just move the object by how much the mouse had moved.
Depends on what you want to do. Move the object on a 3D chessboard?

You could use gluUnproject() to transform the mouse coordinates to world space.
Do this twice - once with z = near plane, once with z = far plane. Then intersect the chessboard with the ray formed by those 2 points and move the object to the intersection point.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
In a previous related thread I suggested that you use Math to do the mouse picking by projecting a ray from the mouse cursor and intersect it with the plane of the board.

Assuming you did your picking with that method (and not using glpicking) then moving the piece is Easy.
Just keep preforming the same intersection test as the cursor moves, and redraw the chess piece at the board intersection point as it changes.

(this is what we meant in the other thread about how the ray intersection method was best because you would Need it Anyway later on.)

This topic is closed to new replies.

Advertisement