Some help with rotations.

Discuss whatever you want here--both QB and non-QB related. Anything from the DEF INT command to the meaning of life!

Moderators: Pete, Mods

Post Reply
Brick Wall

Some help with rotations.

Post by Brick Wall » Sat Dec 03, 2005 1:06 am

Ok, I have a program that displays a 3D model of the ship with the following camera controls:

Arrow Keys to X and Y rotate the ship.
E and Q rotates the model by Z axis.

The current problem I am facing atm is, the ship rolls as its either facing down or up. Making me assume that it's stuck in first person mode. What I wanted to do is, get the ship to be able to pitch up and down, turn left and right, and roll reguardless of what angle its at.

I tried Faster 3D Rotation by Eclipzer but it was a no go.

Heres the code:

Code: Select all

#define SC_UP     &h48
#define SC_LEFT   &h4B
#define SC_RIGHT  &h4D
#define SC_DOWN   &h50

#define SC_Q &h10
#define SC_E &h12

#define pi  3.1415926535897932

TYPE V3D
    X as Single
    Y as Single
    Z as Single
END TYPE

TYPE SimpleEnt
    Rot as V3D
    Coor as V3D
END TYPE

Type Edges
    PointA as V3D
    PointB as V3D
END TYPE

Declare SUB rotpt (x1!, y1!, z1!, rotx!, roty!, rotz!, nx!, ny!, nz!)
Declare SUB Point3D (xA!, yA!, zA!, xP, yP, DST)

CONST LENS = 384
CONST CenterX=320
CONST CenterY=240

Dim ToyLine(36) as Edges
Dim ToyVert(16) as V3D
Dim TempX, TempY, TempZ, RotPY, RotPX, RotPZ as Single
Dim Dist, ProjX(2), ProjY(2), PrevX, PrevY as Integer

DIM TOM, EEE as Integer

FOR TOM=1 to 16
    READ ToyVert(TOM).X
    READ ToyVert(TOM).Z
    READ ToyVert(TOM).Y
    ToyVert(TOM).Z=-ToyVert(TOM).Z
    ToyVert(TOM).Y=-ToyVert(TOM).Y
NEXT TOM

For TOM =1 to 36
    READ StartPNT
    READ EndPNT
    ToyLine(TOM).PointA.X=ToyVert(StartPNT+1).X
    ToyLine(TOM).PointA.Y=ToyVert(StartPNT+1).Y
    ToyLine(TOM).PointA.Z=ToyVert(StartPNT+1).Z
    
    ToyLine(TOM).PointB.X=ToyVert(EndPNT+1).X
    ToyLine(TOM).PointB.Y=ToyVert(EndPNT+1).Y
    ToyLine(TOM).PointB.Z=ToyVert(EndPNT+1).Z
Next TOM    

Screen 18

RotPY!=0
RotPX!=0
RotPZ!=0 

DO
FOR EEE = 1 to 36

    rotpt ToyLine(EEE).PointA.X,ToyLine(EEE).PointA.Y,ToyLine(EEE).PointA.Z,RotPX!,RotPY!,RotPZ!,TempX!,TempY!,TempZ!    
    Point3D TempX!, TempY!, TempZ!, ProjX(1), ProjY(1), Dist
    
    rotpt ToyLine(EEE).PointB.X,ToyLine(EEE).PointB.Y,ToyLine(EEE).PointB.Z,RotPX!,RotPY!,RotPZ!,TempX!,TempY!,TempZ!    
    Point3D TempX!, TempY!, TempZ!, ProjX(2), ProjY(2), Dist
    
    If Dist>0 Then
        LINE (ProjX(1),ProjY(1))-(ProjX(2),ProjY(2)),15        
    END IF    
NEXT EEE
SLEEP 1
CLS
Locate 5,5:Print "3D Wireframe Ship Test"

if multikey(SC_LEFT) then
        RotPY!=RotPY!+.005
end if
if multikey(SC_Right) then
        RotPY!=RotPY!-.005
end if
if multikey(SC_UP) then
        RotPX!=RotPX!-.005
end if
if multikey(SC_Down) then
        RotPX!=RotPX!+.005
end if

if multikey(SC_Q) then
        RotPZ!=RotPZ!-.005
end if

if multikey(SC_E) then
        RotPZ!=RotPZ!+.005
end if

Locate 1,1:Print RotPX!
Locate 2,1:Print RotPY!
Locate 3,1:Print RotPZ!

Loop Until Inkey$=" "

END


SUB Point3D (xA!, yA!, zA!, xP, yP, DST)
    DST = (LENS - zA!)
    xP=CenterX+(LENS * xA! / DST)
    yP=CenterY+(LENS * yA! / DST)
END SUB

SUB rotpt (x1!, y1!, z1!, rotx!, roty!, rotz!, nx!, ny!, nz!)
    DIM AS SINGLE rx,ry,rz
    rx = rotx!
    ry = roty!+90
    rz = rotz!+90
    'xrot
    nx1! = x1!
    ny1! = z1! * SIN(pi * rx) + y1! * COS(pi * rx)
    nz1! = z1! * COS(pi * rx) - y1! * SIN(pi * rx)
   'yrot
    nx2! = nx1! * COS(pi * ry) - nz1! * SIN(pi * ry)
    ny2! = ny1!
    nz2! = nx1! * SIN(pi * ry) + nz1! * COS(pi * ry)
    'zrot
    nx! = nx2! * COS(pi * rz) - ny2! * SIN(pi * rz)
    ny! = nx2! * SIN(pi * rz) + ny2! * COS(pi * rz)
    nz! = nz2!   
END SUB

Data -22,-203,-3
Data  22,-203,-3
Data -53,162,43
Data  53,162,43
Data  118,129,-19
Data  154,162,-3
Data -154,162,-3
Data -118,129,-19
Data -18,-142,10
Data  18,-142,10
Data  38,-51,11
Data -38,-51,11
Data  20,-51,43
Data -20,-51,43
Data  53,162,-3
Data -53,162,-3

Data 8,13
Data 8,11
Data 8,9
Data 9,12
Data 9,10
Data 0,11
Data 0,8
Data 0,7
Data 0,6
Data 0,1
Data 1,10
Data 1,9
Data 1,5
Data 1,4
Data 2,15
Data 2,13
Data 2,11
Data 2,6
Data 2,3
Data 3,14
Data 3,12
Data 3,10
Data 3,5
Data 4,14
Data 4,7
Data 4,5
Data 5,14
Data 5,10
Data 6,15
Data 6,11
Data 6,7
Data 7,15
Data 10,12
Data 11,13
Data 12,13
Data 14,15
I thought the solution for this should be a no brainer mainly because there should be a sub that quadrates the raw axes rotations to the actual pitch yaw and roll angles.
Something like this:

'In the EEE loop:

GetPitchYawRoll RotX,RotY,RotZ,RealAngleX,RealAngleY,RealAngleZ
rotpt CoordX,CoordY,CoordZ,RealAngleX.RealAngleY.RealAngleZ,TempX,TempY,TempZ
'Where RealAngleX..Z get's their real rotation angle axis, and where TempX..Z gets a coordination of the offsetted rotational axises.

Really, how hard is this to solve?

Brick Wall

Post by Brick Wall » Sat Dec 03, 2005 1:08 am

I want to point out 2 mistakes.

First one I posted this topic in the wrong sector.

Second one is that I forgot to tell that you need freebasic to use this code.

User avatar
matt2jones
Veteran
Posts: 80
Joined: Sat Feb 19, 2005 8:29 am
Location: elsewhere
Contact:

Post by matt2jones » Sat Dec 03, 2005 8:45 am

Right, its a simple fix (if I understand you).

You say it's "stuck in first person mode", what I think you mean by this is when you try and rotate the ship it moves off one side of the screen, goes behind you, and comes out the other side.

If that's the case, visualise this:

Code: Select all



                                     *--------*
           [Camera points->          | Ship   |    
                                     *--------*   


Now, when you rotate the ship 90 degress, what happens is:

Code: Select all


                     *-*
                     |p|
                     |i|
                     |h|                    
                     |S|
                     *-*
            [Camera points->                                                       


So the ship is rotating AROUND the camera, because the camera is at coords (0,0,0), the origin.

Almost all rotation formulae only work for rotating around the origin.

So, if you want the ship to revolve around the middle of itself, you have to move it so that the origin is in the middle of it. This is really easy, just find the coordinates of the middle of the ship, and subtract them from all the other points in the ship.

Then do your rotations. Now the ship will be rotated about it's middle, but it will look like this:

Code: Select all



                       *-----------------*  
                       | p               |
                       | i               |
                       | h    [CAM]->    |
                       | S               |
                       *-----------------*  

So you'll now be looking at the rotated ship from the inside.  You have to move it back to where is was originally to get the correct rotated view of it.  Doing that is easy, just add back to the coords of every point in the ship the values you subtracted from them in the first step.


Recap:

* Pick point to rotate around
* Translate ship model so that point is a origin
* Rotate Ship about origin
* Translate ship model so that the middle point is back where it was
* Draw the model.


I hope this helps,

matt


Do not mistake Apathy for feeling Content.

http://www.disjointed.cjb.net - Short Storys
http://matt2jones.deviantart.com - Random Art
http://www.freewebs.com/matt2jones - WebComic

Brick Wall

Post by Brick Wall » Sat Dec 03, 2005 2:56 pm

If you or anyone can hack that code I posted above to make it use spherical rotations(as in pitch yaw and rolling) instead of the rotation to get stuck in first person shooter mode, that would be appreciatable and helpful for me since I do not have anymore time to type this.

Brick Wall

Post by Brick Wall » Sun Dec 04, 2005 3:36 am

I think I almost got the problem solved!!

Taking a look at this article over here:

http://www.delphi3d.net/articles/viewar ... iewing.htm

There are a few things I still need know.

In Fig. 2 in that article, first matrix on the 4th column, do I have to worry about the -x -y -z stuff? Also

Also on the 4th row and coloumn on two matrix which both equals 1, do I leave that alone?

On the second matrix, does the Rx, Ux, Vx etc mean R * x, U * x, V * x?

Last question, with the final rotate feature, which part on the second matrix do I use?

Sorry for the excessive amount of newbie questions.

User avatar
matt2jones
Veteran
Posts: 80
Joined: Sat Feb 19, 2005 8:29 am
Location: elsewhere
Contact:

Post by matt2jones » Sun Dec 04, 2005 5:51 am

Don't try to use rotations using matrices straight up. Use the trig formulae, then rewrite it interms of a transformation matrix... or whatever the fork it's called.

Your confusing yourself. Rotations are very easy to do when you just use the formulae... Take a look at the HAW (3D the Half Assed Way) tutorial on this site for an idiot proof tutorial :D , it covers simple rotations of wireframe models.

matt
Do not mistake Apathy for feeling Content.

http://www.disjointed.cjb.net - Short Storys
http://matt2jones.deviantart.com - Random Art
http://www.freewebs.com/matt2jones - WebComic

User avatar
The Awakened
Veteran
Posts: 144
Joined: Sun Aug 07, 2005 1:51 am

Post by The Awakened » Sun Dec 04, 2005 12:19 pm

Or check out Rel's 3D tutorials.

http://rel.betterwebber.com/index.php?a ... =Tutorials

-The Awakened
"Sorry for beating you up with a baseball bat Julian, but I DID think that you were a samsquanch."

Post Reply