Qbasicnews.com
July 05, 2020, 02:58:19 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Axis angle rotation demo...  (Read 4404 times)
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« on: May 20, 2005, 05:27:01 AM »

I tried to keep this as small as possible while adding a little stuff to make it more interesting to look at.  I'm using a system similar to this to make a space shooter. Wink

Anyway, I hope someone gets some use out of it.  Tongue

Code:

'|Axis Angle Rotation Demo|05/20/05|Dr_D
'The base code came from FreeBASIC/examples/gl/fbgfx_opengl.bas
'I just added a few vector operations and axis angle rotations. ;)
' W,A,S,D = Forward, Strafe Left, Reverse, Strafe Right
'LMB = Roll Left || RMB = Roll Right



option explicit
Randomize Timer

'$include: 'GL/gl.bi'
'$include: 'GL/glu.bi'

Type Vector3DType
   X as Single
   Y as Single
   Z as Single
End Type

Type EntityType
   Position as Vector3DType
   LookAt as Vector3DType
   DVector as Vector3DType
   UVector as Vector3DType
   RVector as Vector3DType
   Radius as Single
   Colr(3) as Single
   Emission(3) as Single
   Orbit(1) as Single  
End Type



DECLARE SUB Rot_X(Entity as EntityType, Angle as Single)
DECLARE SUB Rot_Y(Entity as EntityType, Angle as Single)
DECLARE SUB Rot_Z(Entity as EntityType, Angle as Single)
DECLARE SUB Vector_Cross (v1 AS Vector3DType, v2 AS Vector3DType, v AS Vector3DType)
DECLARE Function Vector_Magnitude(V AS Vector3DType) as Single
DECLARE SUB Vector_Normalize (v AS Vector3DType)
DECLARE Function Distance(P1 as Vector3DType, P2 as Vector3DType) as Single

Dim i as Integer,_
    i2 as Integer, _
    Button as Integer, _
    TempX as Integer, _
    TempY as Integer, _
    tDist as Single
   
Dim Cam as EntityType, Factor as Vector3DType, _
Light.Position(3) as Single, _
Ball(1 to 25) as EntityType

    Cam.Position.X = 0
    Cam.Position.Y = 0
    Cam.Position.Z = 1500
   
    Cam.DVector.X = 0
    Cam.DVector.Y = 0
    Cam.DVector.Z = -1
   
    Cam.UVector.X = 0
    Cam.UVector.Y = 1
    Cam.UVector.Z = 0
   
    Cam.RVector.X = 1
    Cam.RVector.Y = 0
    Cam.RVector.Z = 0
   
    Light.Position(0) = 0
    Light.Position(1) = 0
    Light.Position(2) = 0
    Light.Position(3) = 1
       

    For i = 2 to UBOUND(Ball)
        Ball(i).Position.X =-2000+(rnd*4000)
        Ball(i).Position.Z =-2000+(rnd*4000)
        Ball(i).Radius = 25+ rnd*100
        Ball(i).Orbit(1) = rnd
           For i2 = 0 to 3
               Ball(i).Colr(i2) = rnd
               Ball(i).Emission(i2) = 0
           Next
    Next
   

    Ball(1).Radius = 200
    Ball(1).Colr(0) = 1
    Ball(1).Colr(1) = .5
    Ball(1).Colr(2) = .015
    Ball(1).Colr(3) = 1
    Ball(1).Emission(0)= 1
    Ball(1).Emission(1)= .5
    Ball(1).Emission(2)= 0.15
    Ball(1).Emission(3)= 1
   
Screen 18, 32, , 1 OR 2
Const MidX = 640\2, MidY = 480\2, _
      SC_A = &h1E, SC_S = &h1F, _
      SC_W = &h11, SC_D = &h20, _
      False = 0, True = Not False

glViewport 0, 0, 640, 480
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, 640.0/480.0, 25, 10000
glMatrixMode GL_MODELVIEW
glLoadIdentity

glShadeModel GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 1.0
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glEnable GL_COLOR_MATERIAL
glEnable Gl_LIGHTING
glEnable GL_LIGHT0
glDepthFunc GL_LEQUAL
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST


Dim Sphere as GLUquadricObj ptr
    Sphere = gluNewQuadric
   

Do
    glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
   
    GetMouse(TempX, TempY,, Button)
    Factor.Y = (MidX-TempX)/500
    Factor.X = (MidY-TempY)/500
    If Button and 1 then
        Factor.Z = .01
    ELSEIf Button and 2 then
        Factor.Z = -.01
    ELSE
        Factor.Z = 0
    End If    
    SetMouse(MidX, MidY, 0)
   
    Rot_X Cam, Factor.X
    Rot_Y Cam, Factor.Y
    Rot_Z Cam, Factor.Z
   
    If MultiKey(SC_W) Then
        Cam.Position.X += Cam.DVector.X*5
        Cam.Position.Y += Cam.DVector.Y*5
        Cam.Position.Z += Cam.DVector.Z*5
    End If

    If MultiKey(SC_S) Then
        Cam.Position.X -= Cam.DVector.X*5
        Cam.Position.Y -= Cam.DVector.Y*5
        Cam.Position.Z -= Cam.DVector.Z*5
    End If
   
   
    If MultiKey(SC_D) Then
        Cam.Position.X += Cam.RVector.X*5
        Cam.Position.Y += Cam.RVector.Y*5
        Cam.Position.Z += Cam.RVector.Z*5
    End If
   
    If MultiKey(SC_A) Then
        Cam.Position.X -= Cam.RVector.X*5
        Cam.Position.Y -= Cam.RVector.Y*5
        Cam.Position.Z -= Cam.RVector.Z*5
    End If
   
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
   
    Cam.LookAt.X = Cam.Position.X + Cam.DVector.X
    Cam.LookAt.Y = Cam.Position.Y + Cam.DVector.Y
    Cam.LookAt.Z = Cam.Position.Z + Cam.DVector.Z
   
gluLookAt Cam.Position.X, Cam.Position.Y, Cam.Position.Z, _
              Cam.LookAt.X, Cam.LookAt.Y, Cam.LookAt.Z, _
              Cam.UVector.X, Cam.UVector.Y, Cam.UVector.Z
             
    glLightfv GL_LIGHT0, GL_POSITION, @light.Position(0)
       
    gluQuadricDrawStyle Sphere, GLU_LINE
    gluQuadricNormals Sphere, GLU_FLAT
    gluQuadricOrientation Sphere, GLU_INSIDE
    glColor3f 0,0,.25
    gluSphere Sphere, 5000, 64, 32
   
       
    gluQuadricDrawStyle Sphere, GLU_FILL
    gluQuadricNormals Sphere, GLU_SMOOTH
    gluQuadricOrientation Sphere, GLU_OUTSIDE
    For i = 1 to UBOUND(Ball)
        Ball(i).Orbit(0) +=Ball(i).Orbit(1)
        If Ball(i).Orbit(0)>=360 then Ball(i).Orbit(0) = 0
        glPushMatrix
            glRotatef Ball(i).Orbit(0), 0, 1, 0            
            glTranslatef Ball(i).Position.X, Ball(i).Position.y, Ball(i).Position.Z
            glColor4fV @Ball(i).Colr(0)
            glMaterialfv GL_FRONT, GL_EMISSION, @Ball(i).Emission(0)
            gluSphere Sphere, Ball(i).Radius, 32, 16
        glPopMatrix
    Next
   
flip
loop while inkey$ <> CHR$(27)



gluDeleteQuadric Sphere
End





Function Distance(P1 as Vector3DType, P2 as Vector3DType) as Single
   Distance = sqr((P2.x - P1.x)^2 + (P2.y - P1.y)^2 + (P2.z - P1.z)^2)
End Function

SUB Vector_Cross (v1 AS Vector3DType, v2 AS Vector3DType, v AS Vector3DType)
   v.x = (v1.y * v2.z) - (v2.y * v1.z)
   v.y = (v1.z * v2.x) - (v2.z * v1.x)
   v.z = (v1.x * v2.y) - (v2.x * v1.y)
END SUB

Function Vector_Magnitude(V AS Vector3DType) as Single
   Dim Mag as Single
   Mag = SQR(v.x ^2 + v.y ^2 + v.z ^2)
   If Mag = 0 then Mag = 1
   Vector_Magnitude = Mag
End Function

SUB Vector_Normalize (v AS Vector3DType)
   Dim Mag as Single
   Mag = Vector_Magnitude(V)  
   v.x = v.x / Mag
   v.y = v.y / Mag
   v.z = v.z / Mag
END SUB

SUB Rot_X(Entity as EntityType, Angle as Single)
   Dim tDir as Vector3DType, tSin as Single, tCos as Single
 
   tCos = COS(Angle)
   tSin = SIN(Angle)

   tDir.X = Entity.DVector.X*tCOS+Entity.UVector.X*tSin
   tDir.Y = Entity.DVector.Y*tCOS+Entity.UVector.Y*tSin
   tDir.Z = Entity.DVector.Z*tCOS+Entity.UVector.Z*tSin
   Vector_Normalize tDir
   Entity.DVector.X = tDir.X
   Entity.DVector.Y = tDir.Y
   Entity.DVector.Z = tDir.Z  

   Vector_Cross Entity.DVector, Entity.RVector, Entity.UVector
   Entity.UVector.X =-Entity.UVector.X
   Entity.UVector.Y =-Entity.UVector.Y
   Entity.UVector.Z =-Entity.UVector.Z
End Sub



SUB Rot_Y(Entity as EntityType, Angle as Single)
   Dim tDir as Vector3DType, tCos as Single, tSin as Single
   
   tCos = COS(Angle)
   tSin = Sin(Angle)
   
   tDir.X = Entity.DVector.X*tCOS-Entity.RVector.X*tSin
   tDir.Y = Entity.DVector.Y*tCOS-Entity.RVector.Y*tSin
   tDir.Z = Entity.DVector.Z*tCOS-Entity.RVector.Z*tSin
   Vector_Normalize tDir
   Entity.DVector.X = tDir.X
   Entity.DVector.Y = tDir.Y
   Entity.DVector.Z = tDir.Z
   
   Vector_Cross Entity.DVector, Entity.UVector, Entity.RVector
End Sub



SUB Rot_Z(Entity as EntityType, Angle as Single)
   Dim tRight as Vector3DType, tCos as Single, tSin as Single
   
   tCos = COS(Angle)
   tSin = Sin(Angle)
     
   tRight.X = Entity.RVector.X*tCOS+Entity.UVector.X*tSin
   tRight.Y = Entity.RVector.Y*tCOS+Entity.UVector.Y*tSin
   tRight.Z = Entity.RVector.Z*tCOS+Entity.UVector.Z*tSin
   Vector_Normalize tRight
   Entity.RVector.X = tRight.X
   Entity.RVector.Y = tRight.Y
   Entity.RVector.Z = tRight.Z
     
   Vector_Cross Entity.DVector, Entity.RVector, Entity.UVector
   Entity.UVector.X =-Entity.UVector.X
   Entity.UVector.Y =-Entity.UVector.Y
   Entity.UVector.Z =-Entity.UVector.Z
END SUB
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #1 on: May 20, 2005, 07:21:34 AM »

cool!!
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
rdc
Senior Member
**
Posts: 176



WWW
« Reply #2 on: May 20, 2005, 10:09:56 AM »

Cool indeed. If you all keep this up, I might actually learn a little OGL. Smiley
Logged

WinderBoy
Member
*
Posts: 50


« Reply #3 on: May 20, 2005, 12:04:25 PM »

Tottaly awsome here.

Many thanks.
Logged
Deleter
Na_th_an
*****
Posts: 1293



WWW
« Reply #4 on: May 20, 2005, 05:12:15 PM »

OMG  Shocked  Shocked  Shocked  I'm going to be the first person to play your game.  :lol: I love space shooters, escpecially when I can rotate and stuff... best of luck finishing it, else I will send someone with a very large sword after you  :rotfl:
Logged

Rattrapmax6
__/--\__
*****
Posts: 2577



WWW
« Reply #5 on: May 20, 2005, 06:08:39 PM »

As I expected... Awusome!!!!!

<- Studies code now... :roll:  :wink:
Logged

Kevin (x.t.r.GRAPHICS)

MystikShadows
Ancient Guru
****
Posts: 542



WWW
« Reply #6 on: May 20, 2005, 06:25:53 PM »

Awesome isn't the word....The Dr did it again!!!  Wow, I can't wait to see what the whole game is gonna look like.
Logged

hen they say it can't be done, THAT's when they call me ;-).




need hosting:  http://www.jc-hosting.net
All about ASCII: http://www.ascii-world.com
WinderBoy
Member
*
Posts: 50


« Reply #7 on: May 21, 2005, 12:57:26 AM »

I was wondering how a free form flight like this could be done in an ODE way.

I am currently drooling at that library not knowing if it will be easy or complicated. I already got the .bi files but havent seen any examples in FB yet.

Perhaps, I shall check the CVS again.
Logged
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #8 on: May 21, 2005, 01:46:07 AM »

Not me. Sorry, but unless someone that knows the C++ crap shows us how to init ode object spaces without crashing, then these are my thoughts...

FreeBASIC is the boss, while ode is biased C++crap.

  :roll:
Logged
WinderBoy
Member
*
Posts: 50


« Reply #9 on: May 21, 2005, 03:57:05 AM »

Anyways, I may consider making a .bi library out of this. I hope someone does not mind about this.

This is just to make life easier.
Logged
v3cz0r
I hold this place together
*****
Posts: 924



WWW
« Reply #10 on: May 21, 2005, 09:38:34 PM »

Be sure you are using the right ODE DLL, the .bi file is for the SINGLE DLL's, not for the DOUBLE DLL's, but you could change the "dReal" type from single to double at ode/common.bi to use the DOUBLE version though.

Using the wrong DLL will cause an exception, as the wrong types will be pushed to stack. As ODE uses the C convention, the function names will be the same, so you can't detect the error at link-time - that would be clear if functions were using the standard-call convention.
Logged

Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #11 on: May 22, 2005, 04:12:49 AM »

Which DLL are you using for SINGLE floats? Which ODE package did you download? Maybe I can get it to work, if I have been using the wrong DLL, that would explain it.  :roll:

My other post was a little harsh, I guess. I've seen some really awesome stuff made with it, but I've also never had so much trouble using a well known library.
Logged
v3cz0r
I hold this place together
*****
Posts: 924



WWW
« Reply #12 on: May 23, 2005, 03:02:00 AM »

If you downloaded ODE off sf.net, any package labeled -SINGLE would be okay, but the DLL must be in the same dir where your test is being executed, or a -DOUBLE DLL at your %PATH could take place.

Every app using ODE must come with the DLL, because that double/single thing, not really clever, but..
Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!