PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

3D, 6 degrees of freedom & PocketGL

As I'm writing a space game I should really have the player's ship moving freely in all directions however PocketGL's examples only show rotation on the Y axis & movement on xyz.

When I add an x rotation I get my ship pitching correctly and when my movement is into the screen moving correctly but when I rotate & point back out the screen things go wrong. The forward, backward controls are reversed and depending on how I change code the pitch is also reversed so the ship goes down instead of up etc.

PocketGL has various commands derived from OpenGL but 2 days of searching for a suitable OGL demo have produced nothing of value other than a couple of mentions of the need to multiply matrices.

If anyone is familiar with 3D movement & is willing to give advice please respond. I can post my source code or email.

ps: I'm using PocketGL 1.0 at present (this affects the functions not the concept)
Hi Conan,

I've done some 3d stuff and happy to help if I can. I'm not that up on PocketGL or OpenGL, but feel free to post or email and I'll have a look.

I usually store 3 sets of coordinates for everything: Local, World, and View.

Local coordinates store the starting "default" position of the object, usually centered on the origin, World coordinates store the position and rotation in the "world", and View coordinates store the perspective view or screen coordinates.

When moving the object through the "world", first rotate from the local coordinates to the world rotation, then translate the object to the world location. In other words, always rotate around the origin first, then translate.

Hope that makes sense and helps.

Pam
Hi Pam, It would probably take you a few hours to grasp this if you have not used OpenGL as it's all to do with the various functions derived from OGL so it's unfair unless you are totally without useful stuff to do at the moment.

It looks like there is a set sequence of events which I cannot figure & can't get an example of.

eg: There are functions to define an X rotation or a Y rotation ( & z ) and a matrix mult function which looks like it needs one matrix on the matrix stack and the other passed to it but yet again nothing example-wise to say whether the DefRotateX function is pushed then the Y set up then the mult etc.

Trial & error is taking a long time but I will get there.
Conan,

Have you looked at the Nehe tutorials? The last time I spent anytime looking into OpenGL that was the most helpful of any I think. I'll find a link if you can't find it via google.

Pam
The motion of an object is independant of its rendering. How are you moving the ship? PocketGL does not have any type of entity handling (although it does have a tutorial demonstrating physics and collision detection). Generally for each object you have a Vector4s for its position (in world coords) and another Vector4s for its orientation. To render you then apply those to the matrix:

Code:

MatrixStack.Push();
MatrixStack.Translate(ship.pos.x, ship.pos.y, ship.pos.z);
MatrixStack.RotateX(ship.rot.x);
MatrixStack.RotateY(ship.rot.y);
MatrixStack.RotateZ(ship.rot.z);
//render the mesh
MatrixStack.Pop();


Now if the camera is "attached" to the ship, then you will need to negate all of those values.

Dan East
Hi Dan, I'm moving the ship just like in the PocketGL demo 10, ie: the ship is the camera. I use the CMatrix44 m and Vector3i DeltaMove variables. I calculate the new Y rotation and run that through the m.DefRotateY(ShipRot.y) and m.GetDir(&DeltaMove;) followed by the 3 lines which set the new xyz eg: ShipPos.x +=(DeltaMove.x * Fwd) etc etc just like in the PocketGL demo 10 I think.

This all works fine until I try to add in an X rotation for the ship to adjust it's pitch.

eg: I can move around on one plane rotating on the Y & moving forwards, backwards, sideways & up/down but cannot move correctly when I pitch on the X axis. It starts out looking correct but when I rotate to come back it goes wonky.

Currently I'm reading OpenGL tutorials & playing with CurrentMatrix->Mult & CurrentMatrix->Load just to see what happens.

My assumption is that there need to be DefRotateX & DefRotateZ commands before the ship's new position is calculated but so far I can't find a single sample anywhere showing this. I have seen about 50 identical OGL documents so lots of cut & paste going on out there.

Note to Pam, Yes I've read several of the NeHe tutorials. The thing is that there does not seem to be a single tutorial anywhere whowing how to fly like a plane/space ship. Looks like the work of a secret society keeping key knowledge to themselves :lol:

UPDATE: I had a reply from Pierre leroy, author of PocketGL so I'm off to try his suggestions
:lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:
Yeah, that's the ticket.
All I was doing wrong was failing to add together my x & Y rotations while working out the correct direction. I was using m.DefRotateX followed by m.DefRotateY. What I should be doing was m.DefRotateX(ShipRot.x) followed by m.RotateY(ShipRot.y)

Pierre told me that the DefRotate function clears out the matrix while the others add on to the existing one. This will make my flight much more realistic :lol:
for 6DOF camera, you should be looking for quaterions, i think its the best choice for that kinda camera. Descent-like camera isnt it ?
i cant remember any tut on the web right now, but google should give you some.. try out gamedev.net and flipcode.com might help
peace !
re: Anonymous tip re: quaternians, there's a good post against my previous 3D post. Problem is that I'm stuck with what I have as I can't program 3D so I use an existing engine.
if you dont want to use quats, just create a rotation matrix (just rotation 3x3 part of the matrix, no translation) and transform a directiron vector with that matrix (normalized vector). that ofcoz when you know the pitch,yaw,roll values.

glRotate( -pitch, 1, 0, 0 );
glRotate( -roll, 0, 0, 1 );
glRotate( -yaw, 0, 1, 0 );
glTranslatef( -cam_position ); // translate to cam pos

glGetMatrix( GL_MODELVIEW, RotMat ); // get modelview matrix
transform( RotMat, V ); // Make sure no translation done
cam = CAM+V;

sumtin like that
Remember that a matrix is not initialized by default. If you push another matrix onto the stack, then it is initialized by the previous matrix (the combination of all previous matrices on the stack, that is). So you can do the DefRotate, or the typical method is to LoadIdentity. That sets up the identity matrix, which should be at the origin, looking down the positive X axis.

Dan East
Ok Dan, understood.

I can now get on & figure out the best way to use the D-Pad in free flight. Shame there isn't a full-sized joystick on these things.
Conan, since your putting alot of time/effort into this, can you make it so the buttons can be bound to any key?

I think then you wouldnt have to worry as much about how to bind the controls to the Dpad.
There's no problem with doing this later. At the moment what I'm working out are the best ways to move with limited controls & there is a virtual dpad 5 way on screen which can be toggled to give 4 additional movements. Currently the hard buttons are directly mapped but I can build in the additional code to let players set the buttons. I'm looking at demos of other PPC games to see how they handle movement.
If you can, I'd suggest using the stylus for everything but acceleration (think m-look), and then up/down on the d-pad for forward/backward