3d rotations issue

Previous topic - Next topic

Hemlos

Sorry, my mail is not working so i have to post this bug report on the forums...

With each 3d object the heirarchy order of rotations are different.... the code is NOT universal. It doesnt work every object the same way! I added 3 x_popmatrix like you said, after the drawobj, but that doesnt help much... after  too many rotations, and after a while none of the rotations have been pushed correctly at all.

With almost a hundred hours of testing...i found the rotations dont truely stack, not matter how i code it, no matter how i place pushmatrix....in retrospect, x_movement pushes fine.

i suspect the problem is in the pushmatrix/popmatrix/rotation commands because 2 matrices do not "push" the stack the same way.
Theoratically in 3d you should only have to rotate any object a maximum of 3 times, if you push the stack 3 times, right? I ask because i need a 4th rotation for one of my objects, because 3 isnt sufficient.
Bing ChatGpt is pretty smart :O

Kitty Hello

OK. Here's a small project showing how to push/popmatrix for creating a helicopter:
Code (glbasic) Select
// --------------------------------- //
// Project:
// Start: Friday, October 13, 2006
// IDE Version: 3.283


// Make a rotor
c=RGB(0x40, 0x40, 0xff)
cn=RGB(255,255,255)

// a rotor blade
X_OBJSTART 0
X_OBJADDVERTEX   0,0,-.1,  0,0, cn
X_OBJADDVERTEX  10,0, -1, .1,0, c
X_OBJADDVERTEX  10,0,  1,-.1,0, c
X_OBJADDVERTEX   0,0, .1,  0,0, cn
X_OBJADDVERTEX   0,0,-.1,  0,0, cn
X_OBJADDVERTEX -10,0,  1,  0,0, c
X_OBJADDVERTEX -10,0, -1, .1,0, c
X_OBJADDVERTEX   0,0,-.1,-.1,0, cn
X_OBJEND


// a "helicopter"
X_OBJSTART 1
X_OBJADDVERTEX  0,   0,  0, 0,0,  cn
X_OBJADDVERTEX -2,  -2,  1, 0,0,  c
X_OBJADDVERTEX  2,  -1,  1, 0,0,  c
X_OBJADDVERTEX  0,  -4, .5, 0,0,  c
X_OBJADDVERTEX  2,  -3,  1, 0,0,  c
X_OBJADDVERTEX 10,  -2,  0, 0,0,  c
X_OBJADDVERTEX 10,-2.5,  0, 0,0,  c
X_OBJNEWGROUP
X_OBJADDVERTEX  0,   0,  0, 0,0,  cn
X_OBJADDVERTEX -2,  -2, -1, 0,0,  c
X_OBJADDVERTEX  2,  -1, -1, 0,0,  c
X_OBJADDVERTEX  0,  -4,-.5, 0,0,  c
X_OBJADDVERTEX  2,  -3, -1, 0,0,  c
X_OBJADDVERTEX 10,  -2,  0, 0,0,  c
X_OBJADDVERTEX 10,-2.5,  0, 0,0,c
X_OBJEND


WHILE TRUE
INC inclination, MOUSEAXIS(1)/20
INC rotation,    MOUSEAXIS(0)
INC spin, GETTIMER()/10

X_MAKE3D 1,100,45
X_CAMERA 0,5,-20, 0,0,0

// the helicopter itself:
// first incline the thing
X_ROTATION inclination, 0,0,1
// then rotate it
X_ROTATION rotation,    0,1,0
X_DRAWOBJ 1, 0

X_PUSHMATRIX // we go relative to the rotated helicopter now
// add a rotation for the top rotor
// see: I spin it around the y-axis of the chopper!
X_ROTATION spin, 0,1,0
X_DRAWOBJ 0, 0


// * the back propeller *
// I move it to the tail -> that clears the above rotation
X_MOVEMENT 10,-2.2,0
X_SCALING .1, .1, .1
// I rotate it to face the side
X_ROTATION 90, 1,0,0

X_ROTATION 2*spin, 0,0,1
X_DRAWOBJ 0, 0

X_POPMATRIX
SHOWSCREEN
WEND
This should be pretty much what you are looking for. Please tell me if you don't understand any part of it. I'll be glad to sort this out. It's a very powerful thing if you know how to use it, and it's rather easy once you got it.

Hemlos

Hmm
this is what i need to do to ONE part of the helicopter...rotations stop working properly when the part is rotated upsidedown or sideways!....btw this is just the body of the chopper, its not even the glass or the blades....

   X_MOVEMENT x,y,z
   X_ROTATION rz,-1,0,0
   X_ROTATION ry,0,-1,0
   X_ROTATION rx,0,0,1
   X_DRAWOBJ num, frame

It MUST rotate on 3 axis ..or helicopter wont fly
it twists, it rolls, it dives...all 3 axis can be spun 360 degrees to fly in any direction basically.
when you rotate on all 3, it gets ugly.
cool 3d helicopter btw!

change  this code to your helicopter body rotations and you will see what i mean about the rotations getting messed up...the axis stop controlling the correct axis of the object...
Code (glbasic) Select
  // first incline the thing
    X_ROTATION inclination, 0,0,1
    // then rotate it
    X_ROTATION rotation,    0,1,0
    X_ROTATION spin,    1,0,0  //  <-- JUST ADD THIS LINE
twist it up with the mouse when you run it, youll notice how the tail stops spinning and occasionally the body will instead rotate in a different unwanted direction.
Bing ChatGpt is pretty smart :O

Kitty Hello

right. I see.
First, rotate it for inclination, then for rotation around y-axis. Those are global rotations.
Then pushmatrix, rotate around "forward" axis. This rotation is relative to the choper.
Code (glbasic) Select
// --------------------------------- //
// Project:
// Start: Friday, October 13, 2006
// IDE Version: 3.283


// Make a rotor
c=RGB(0x40, 0x40, 0xff)
cn=RGB(255,255,255)

// a rotor blade
X_OBJSTART 0
X_OBJADDVERTEX   0,0,-.1,  0,0, cn
X_OBJADDVERTEX  10,0, -1, .1,0, c
X_OBJADDVERTEX  10,0,  1,-.1,0, c
X_OBJADDVERTEX   0,0, .1,  0,0, cn
X_OBJADDVERTEX   0,0,-.1,  0,0, cn
X_OBJADDVERTEX -10,0,  1,  0,0, c
X_OBJADDVERTEX -10,0, -1, .1,0, c
X_OBJADDVERTEX   0,0,-.1,-.1,0, cn
X_OBJEND


// a "helicopter"
X_OBJSTART 1
X_OBJADDVERTEX  0,   0,  0, 0,0,  cn
X_OBJADDVERTEX -2,  -2,  1, 0,0,  c
X_OBJADDVERTEX  2,  -1,  1, 0,0,  c
X_OBJADDVERTEX  0,  -4, .5, 0,0,  c
X_OBJADDVERTEX  2,  -3,  1, 0,0,  c
X_OBJADDVERTEX 10,  -2,  0, 0,0,  c
X_OBJADDVERTEX 10,-2.5,  0, 0,0,  c
X_OBJNEWGROUP
X_OBJADDVERTEX  0,   0,  0, 0,0,  cn
X_OBJADDVERTEX -2,  -2, -1, 0,0,  c
X_OBJADDVERTEX  2,  -1, -1, 0,0,  c
X_OBJADDVERTEX  0,  -4,-.5, 0,0,  c
X_OBJADDVERTEX  2,  -3, -1, 0,0,  c
X_OBJADDVERTEX 10,  -2,  0, 0,0,  c
X_OBJADDVERTEX 10,-2.5,  0, 0,0,c
X_OBJEND


WHILE TRUE
INC inclination, MOUSEAXIS(1)/20
INC rotation,    MOUSEAXIS(0)
INC drift,       MOUSEAXIS(2)
INC spin, GETTIMER()/10

X_MAKE3D 1,100,45
X_CAMERA 0,5,-20, 0,0,0

// the helicopter itself:
// first incline the thing
X_ROTATION inclination, 0,0,1
// then rotate it
X_ROTATION rotation,    0,1,0

X_PUSHMATRIX
X_ROTATION drift, 1,0,0
X_DRAWOBJ 1, 0

X_PUSHMATRIX // we go relative to the rotated helicopter now
// add a rotation for the top rotor
// see: I spin it around the y-axis of the chopper!
X_ROTATION spin, 0,1,0
X_DRAWOBJ 0, 0


// * the back propeller *
// I move it to the tail -> that clears the above rotation
X_MOVEMENT 10,-2.2,0
X_SCALING .1, .1, .1
// I rotate it to face the side
X_ROTATION 90, 1,0,0

X_ROTATION 2*spin, 0,0,1
X_DRAWOBJ 0, 0

X_POPMATRIX
X_POPMATRIX
SHOWSCREEN
WEND
PS. You can join me on irc - see link above in forum.

Hemlos

link to irc is dead apparently..do i need a plugin for mybrowser or something?

im going to try the code above and see if i can use it

i have europe long distance calling, i can call youif you give me your phone number...
id give you mine but the email server says im a spammer :P

Ok here is your last code modified to use with a joystick..

Code (glbasic) Select
// --------------------------------- //
// Project:
// Start: Friday, October 13, 2006
// IDE Version: 3.283


// Make a rotor
c=RGB(0x40, 0x40, 0xff)
cn=RGB(255,255,255)

// a rotor blade
X_OBJSTART 0
    X_OBJADDVERTEX   0,0,-.1,  0,0, cn
    X_OBJADDVERTEX  10,0, -1, .1,0, c
    X_OBJADDVERTEX  10,0,  1,-.1,0, c
    X_OBJADDVERTEX   0,0, .1,  0,0, cn
    X_OBJADDVERTEX   0,0,-.1,  0,0, cn
    X_OBJADDVERTEX -10,0,  1,  0,0, c
    X_OBJADDVERTEX -10,0, -1, .1,0, c
    X_OBJADDVERTEX   0,0,-.1,-.1,0, cn
X_OBJEND


// a "helicopter"
X_OBJSTART 1
    X_OBJADDVERTEX  0,   0,  0, 0,0,  cn
    X_OBJADDVERTEX -2,  -2,  1, 0,0,  c
    X_OBJADDVERTEX  2,  -1,  1, 0,0,  c
    X_OBJADDVERTEX  0,  -4, .5, 0,0,  c
    X_OBJADDVERTEX  2,  -3,  1, 0,0,  c
    X_OBJADDVERTEX 10,  -2,  0, 0,0,  c
    X_OBJADDVERTEX 10,-2.5,  0, 0,0,  c
    X_OBJNEWGROUP
    X_OBJADDVERTEX  0,   0,  0, 0,0,  cn
    X_OBJADDVERTEX -2,  -2, -1, 0,0,  c
    X_OBJADDVERTEX  2,  -1, -1, 0,0,  c
    X_OBJADDVERTEX  0,  -4,-.5, 0,0,  c
    X_OBJADDVERTEX  2,  -3, -1, 0,0,  c
    X_OBJADDVERTEX 10,  -2,  0, 0,0,  c
    X_OBJADDVERTEX 10,-2.5,  0, 0,0,c
X_OBJEND


WHILE TRUE
    INC inclination, MOUSEAXIS(1)/20
    INC rotation,    MOUSEAXIS(0)
    INC drift,       MOUSEAXIS(2)
    INC spin, GETTIMER()/10

    X_MAKE3D 1,100,45
    X_CAMERA 0,5,-20, 0,0,0

    // the helicopter itself:
    // first incline the thing
    x=x+GETJOYX(0)
    z=z+GETJOYY(0)
    y=y+GETJOYRZ(0)
    X_ROTATION -z, 0,0,1
    // then rotate it
    X_ROTATION -y,    0,1,0
 
    X_PUSHMATRIX
        X_ROTATION -x, 1,0,0
        X_DRAWOBJ 1, 0


        X_PUSHMATRIX // we go relative to the rotated helicopter now
            // add a rotation for the top rotor
            // see: I spin it around the y-axis of the chopper!
            X_ROTATION spin, 0,1,0
            X_DRAWOBJ 0, 0
   
   
            // * the back propeller *
            // I move it to the tail -> that clears the above rotation
            X_MOVEMENT 10,-2.2,0
            X_SCALING .1, .1, .1
            // I rotate it to face the side
            X_ROTATION 90, 1,0,0
   
            X_ROTATION 2*spin, 0,0,1
            X_DRAWOBJ 0, 0
   
        X_POPMATRIX
    X_POPMATRIX
    SHOWSCREEN
WEND
Perfect scenario is pull back on the stick and point the helicopter up...now if you twist the joystick(rudder) it twists on the y axis...but the world y axis, not the heli Y axis...
is there a way around this, so the axis of control moves with the object in 3 dimensions??
Bing ChatGpt is pretty smart :O

Kitty Hello

OK. Got it sorted, I guess. Gimbal Lock is not a problem with simple helkicopters. See this example:
Code (glbasic) Select
// --------------------------------- //
// Project:
// Start: Friday, October 13, 2006
// IDE Version: 3.283


// Make a rotor
c=RGB(0x40, 0x40, 0xff)
cn=RGB(255,255,255)

// a rotor blade
X_OBJSTART 0
X_OBJADDVERTEX   0,0,-.1,  0,0, cn
X_OBJADDVERTEX  10,0, -1, .1,0, c
X_OBJADDVERTEX  10,0,  1,-.1,0, c
X_OBJADDVERTEX   0,0, .1,  0,0, cn
X_OBJADDVERTEX   0,0,-.1,  0,0, cn
X_OBJADDVERTEX -10,0,  1,  0,0, c
X_OBJADDVERTEX -10,0, -1, .1,0, c
X_OBJADDVERTEX   0,0,-.1,-.1,0, cn
X_OBJEND


// a "helicopter"
X_OBJSTART 1
X_OBJADDVERTEX  0,   0,  0, 0,0,  cn
X_OBJADDVERTEX -2,  -2,  1, 0,0,  c
X_OBJADDVERTEX  2,  -1,  1, 0,0,  c
X_OBJADDVERTEX  0,  -4, .5, 0,0,  c
X_OBJADDVERTEX  2,  -3,  1, 0,0,  c
X_OBJADDVERTEX 10,  -2,  0, 0,0,  c
X_OBJADDVERTEX 10,-2.5,  0, 0,0,  c
X_OBJNEWGROUP
X_OBJADDVERTEX  0,   0,  0, 0,0,  cn
X_OBJADDVERTEX -2,  -2, -1, 0,0,  c
X_OBJADDVERTEX  2,  -1, -1, 0,0,  c
X_OBJADDVERTEX  0,  -4,-.5, 0,0,  c
X_OBJADDVERTEX  2,  -3, -1, 0,0,  c
X_OBJADDVERTEX 10,  -2,  0, 0,0,  c
X_OBJADDVERTEX 10,-2.5,  0, 0,0,c
X_OBJEND

// a tree
c = RGB(0x00, 0xff, 0x80)
cn= RGB(0x00, 0x80, 0x00)
X_OBJSTART 2
X_OBJADDVERTEX 0,0,0,    0,0, c
X_OBJADDVERTEX 0,-10,2,  0,0, cn
X_OBJADDVERTEX 0,-10,-2, 0,0, cn
X_OBJNEWGROUP
X_OBJADDVERTEX 0,   0,0, 0,0, c
X_OBJADDVERTEX 2, -10,0, 0,0, cn
X_OBJADDVERTEX -2,-10,0, 0,0, cn
X_OBJEND


pitch = 90
WHILE TRUE
dtime = GETTIMER()/100
INC accx, MOUSEAXIS(1)*dtime*.01 // forward
INC accz, MOUSEAXIS(0)*dtime*.01 // sideways
INC accr, (MOUSEAXIS(3) - MOUSEAXIS(4))*dtime // rotation
INC spin, dtime*30

// air friction
DEC accr, 0.1*accr*dtime
DEC accx, 0.1*accx*dtime
DEC accz, 0.1*accz*dtime

yaw   = accx*45
roll  = accz*45
INC pitch, accr*dtime

// Directional movement to global movement
acgx =  COS(pitch)*accx - SIN(pitch)*accz
acgz = -SIN(pitch)*accx - COS(pitch)*accz

INC posx, acgx*dtime
INC posz, acgz*dtime

X_MAKE3D 1,300,45
X_CAMERA posx,20,posz-50, posx,0,posz

// positioning
X_MOVEMENT posx, 0, posz
X_PUSHMATRIX

// the helicopter itself:
// first incline the thing
X_ROTATION yaw, 0,0,-1
// roll it
X_ROTATION roll, -1,0,0
// then rotate it
X_ROTATION pitch,    0,1,0
X_DRAWOBJ 1, 0
X_PUSHMATRIX // we go relative to the rotated helicopter now
// add a rotation for the top rotor
// see: I spin it around the y-axis of the chopper!
X_ROTATION spin, 0,1,0
X_DRAWOBJ 0, 0

// * the back propeller *
// I move it to the tail -> that clears the above rotation
X_MOVEMENT 10,-2.2,0
X_SCALING .1, .1, .1
// I rotate it to face the side
X_ROTATION 90, 1,0,0

X_ROTATION 2*spin, 0,0,1
X_DRAWOBJ 0, 0

X_POPMATRIX
X_POPMATRIX


// the wood
FOR x=-5 TO 5
FOR y=-5 TO 5
X_MOVEMENT x*20, 0, y*20
X_DRAWOBJ 2,0
NEXT
NEXT

SHOWSCREEN
WEND
You controll the heli with your mouse in x/z surface. Rotate it with left/right mouse buttons. Movement is relative to helicopter. Camera should turn, too :/


Hemlos

forgot where this thread went,

hmm, you added phsyics to hide the gimbal lock, very funny joke of yours. haha

The solution is actually going to be far more sophisticated.
What needs to be added is a quatrion conversion algorithm to input the rotation angles to the x_rotation. This eliminates gimbal lock and all 3 control axis will freely control thier respective axis.
It might be a while before i figgure out how to add that stuff with inline command from gamasutra..i have no idea what im doing with that.
-hem
Bing ChatGpt is pretty smart :O

Kitty Hello

Neither do I. I don't quite get the idea of quaternions, but with X_MULTMATRIX you should be able to use them now. Again: You can write a helicopter simulation without quaternions, too.

Hemlos

I dont undrstand the format for x_multmatrix in the bubble help,
how is it used?
Bing ChatGpt is pretty smart :O

Kitty Hello

Code (glbasic) Select
DIM mat[16]
mat[0..3] = forward (local x-axis) (x,y,z,0)
mat[4..7] = local up (x,y,z,0)
mat[8..11]=local right (x,y,z,0)
mat[12..16]=translation (offset) from origin (x,y,z,1)

X_MULTMATRIX mat[]
If you want to use X_ROTATION and stuff, too - use the X_PUSHMATRIX before X_MULTMATRIX.

Hemlos

ok.
this replaces rotation and movment or are rotation and movment still needed?
Bing ChatGpt is pretty smart :O

Hemlos

The object renders fine with this:
Code (glbasic) Select
mult[0]=1
mult[1]=0
mult[2]=0
mult[3]=0
mult[4]=0
mult[5]=1
mult[6]=0
mult[7]=0
mult[8]=0
mult[9]=0
mult[10]=1
mult[11]=0
mult[12]=0
mult[13]=0
mult[14]=0
mult[15]=1
X_MULTMATRIX mult[]
This obliterates the object stretching it out to infinity:
Code (glbasic) Select
mult[0]=1
mult[1]=0
mult[2]=0
mult[3]=90
mult[4]=0
mult[5]=1
mult[6]=0
mult[7]=0
mult[8]=0
mult[9]=0
mult[10]=1
mult[11]=0
mult[12]=0
mult[13]=0
mult[14]=0
mult[15]=1
X_MULTMATRIX mult[]
Am i not using this correctly?
Bing ChatGpt is pretty smart :O

Hemlos

I think either something is very wrong with x_mult or i am totally not using it how it is intended..

anything i do causes the object in question to get stretched out like a rubberband.
Bing ChatGpt is pretty smart :O

Kitty Hello