We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Moving object according to rotation [Solved]
Page Index Toggle Pages: 1
Moving object according to rotation? [Solved] (Read 1929 times)
Moving object according to rotation? [Solved]
Oct 2nd, 2009, 5:13pm
 
Hi, im trying to build a simple space simulator (just motion). The ship is rotated using the mouse, which works fine. Now when I want it to move forward or backwards (local!) I encounter serious issues. I was able to make it move well on X-Z plane and Y-Z plane using sines and cosines, but failed to combine those (I even tried to implement it using quaternion *sighs*).

I really want to believe that there is simpler way to implement such a trivial thing in processing and transformation matrices are probably the solution but I've found almost nothing helpful on the reference pages.
Please advise.
Re: Moving object according to rotation?
Reply #1 - Oct 2nd, 2009, 6:01pm
 
I use a simple mechanic, one angle pointing on the X-Y plane (right/left), and another pointing in the Y-Z plane (up/down)...you can just change your X,Y based on the X angle and Z based on the Z angle.

It does sound like you may have tried this and found it too inaccurate, though?  If you need a real 3D vector then nothing else will do.

-- Ben
Re: Moving object according to rotation?
Reply #2 - Oct 2nd, 2009, 6:44pm
 
It's not that it was inaccurate, it wasn't doing at all what i expected (my implementation sucked!). I think I've found a way to work it accurately though, but I need a way to get the current transform matrix for that, any idea how do I do that?
Re: Moving object according to rotation?
Reply #3 - Oct 2nd, 2009, 8:59pm
 
Solved! Object can pretty easily be moved with it's nose forward Cheesy Let me know if you want the code
Re: Moving object according to rotation? [Solved]
Reply #4 - Oct 3rd, 2009, 12:45am
 
sure curious how you did it
Re: Moving object according to rotation? [Solved]
Reply #5 - Oct 3rd, 2009, 5:23am
 
Ok, here's the code, I commented the relevant parts as best as I could:

This is a piece of code which is located in the Ship class.

Code:
public void drawShip() {
   resetMatrix();    //apparently OpenGL sets a default trans matrix which has to be reset everytime
   
   translate(pos.x, pos.y, pos.z);  //we move the ship to previously saved position
   rotateZ(radians(rot.z));        //rotating it as needed
   rotateX(radians(rot.x));
   translate(0, 0, delta);          //moving forward by delta (local Z-axis)
   delta = 0;  //reseting delta so that we don't move if there's no button push
   
   //the magic
   PMatrix p = getMatrix();  //apparently you can get the transform matrix
                             //functionality which is NOT documented in reference!
   float[] arr = {2,3};      // you need to supply a float array in order to get the matrix value (dunno why)
   pos.x = p.get(arr)[3];    //getting the right row of the matrix (which is translation) and put it in pos
   pos.y = p.get(arr)[7];
   pos.z = p.get(arr)[11];
   //end of magic
   
   fill(c);                  //building the ship model
   box(70, 8, 20);
   box(20, 10, 50);
   translate(0, -10, 20);
   box(10, 30, 10);
 }
 
 //this function is called every time forward/backward button is pressed
 public void move(int f) {    
   delta += f*speed;
 }
Page Index Toggle Pages: 1