GLBasic forum

Main forum => GLBasic - en => Topic started by: bigsofty on 2007-Apr-14

Title: Camera Vector
Post by: bigsofty on 2007-Apr-14
I want to control the camera direct via a direction vector.

Simply... its not working... the camera view flips when pointing directly up or directly down.
also
when the X axis is added to the equation it spirals the view sometimes....

There must be an easier way to use a direction vector to control the camera angle?

This is what Ive been trying (800x600 windowed)...


Code (glbasic) Select
// View code
TYPE Tvector
x
y
z
magnitude
ENDTYPE

GLOBAL cam_pos AS Tvector; cam_pos.x = 0; cam_pos.y = 0; cam_pos.z = 0 // Camera position
GLOBAL cam_vec AS Tvector; cam_vec.x = 0; cam_vec.y = 0; cam_vec.z = 0 // Camera angle

// Mouse look along vector code...
  MOUSESTATE mx, my, b1, b2
  cam_vec.x = 0; cam_vec.y = 0; cam_vec.z = -1 // Initial view
mx=(mx/800)*3600 // Get an x rotation value
my=(my/600)*3600 // Get an y rotation value
cam_vec = Vector_RotateAroundY(cam_vec, mx/10) // Rotatate around x axis
  cam_vec = Vector_RotateAroundX(cam_vec, my/10) // Rotate around y axis
    X_CAMERA cam_pos.x, cam_pos.y, cam_pos.z, cam_vec.x, cam_vec.y, cam_vec.z
Functions from the vector lib.


Code (glbasic) Select
FUNCTION Vector_RotateAroundX AS Tvector: vec1 AS Tvector, angle
LOCAL result AS Tvector
result.x = vec1.x
result.y = COS(angle) * vec1.y - SIN(angle) * vec1.z
result.z = SIN(angle) * vec1.y + COS(angle) * vec1.z
RETURN result
ENDFUNCTION

FUNCTION Vector_RotateAroundY AS Tvector: vec1 AS Tvector, angle
LOCAL result AS Tvector
result.x  = COS(angle) * vec1.x + SIN(angle) * vec1.z
result.y = vec1.y
result.z = -SIN(angle) * vec1.x + COS(angle) * vec1.z
RETURN result
ENDFUNCTION

FUNCTION Vector_RotateAroundZ AS Tvector: vec1 AS Tvector, angle
LOCAL result AS Tvector
result.x = COS(angle) * vec1.x - SIN(angle) * vec1.y
result.y = SIN(angle) * vec1.x + COS(angle) * vec1.y
result.z = vec1.z
RETURN result
ENDFUNCTION
Title: Camera Vector
Post by: Kitty Hello on 2007-Apr-14
Set X_CAMERAUP directly before X_CAMERA
Title: Camera Vector
Post by: bigsofty on 2007-Apr-14
No luck Gernot.

Heres a little test program to illustrate the problem...

Code (glbasic) Select
// --------------------------------- //
// Project: particles_test (800x600 res.)
// Start: Saturday, April 14, 2007
// IDE Version: 4.154

TYPE Tvector
x
y
z
magnitude
ENDTYPE

GLOBAL cam_pos AS Tvector; cam_pos.x = 0; cam_pos.y = 0; cam_pos.z = 0
GLOBAL cam_vec AS Tvector; cam_vec.x = 0; cam_vec.y = 0; cam_vec.z = 0.1

WHILE TRUE
  X_MAKE3D 1,5500,45

  MOUSESTATE mx, my, b1, b2
// mx = MOD(GETTIMERALL()/10,3600) // Unremark these 1 at a time, to see the problem more clearly
// my = MOD(GETTIMERALL()/10,3600) // Look out for camera axis flipping at Y 0 or 180

  cam_vec.x = 0; cam_vec.y = 0; cam_vec.z = -1
mx=(mx/800)*3600
my=(my/600)*3600
cam_vec = Vector_RotateAroundY(cam_vec, mx/10)
  cam_vec = Vector_RotateAroundX(cam_vec, my/10)
 
    X_CAMERAUP 0,1,0
    X_CAMERA cam_pos.x, cam_pos.y, cam_pos.z, cam_vec.x, cam_vec.y, cam_vec.z

    X_DRAWAXES 100,0,0
    X_DRAWAXES -100,0,0
    X_DRAWAXES 0,100,0
    X_DRAWAXES 0,-100,0
    X_DRAWAXES 0,0,100
    X_DRAWAXES 0,0,-100
    X_PRINT "Right X+",100,0,0,0.3
    X_PRINT "Left X-",-100,0,0,0.3
    X_PRINT "Up Y+",0,100,0,0.3
    X_PRINT "Down Y-",0,-100,0,0.3
    X_PRINT "Out Z+",0,0,100,0.3
    X_PRINT "In Z-",0,0,-100,0.3
   
X_MAKE2D
PRINT "CX: "+cam_vec.x,10,20
PRINT "CY: "+cam_vec.y,10,30
PRINT "CZ: "+cam_vec.z,10,40
PRINT "MX: "+mx/10,10,50
PRINT "MY: "+my/10,10,60
  SHOWSCREEN
WEND


FUNCTION Vector_RotateAroundX AS Tvector: vec1 AS Tvector, angle
LOCAL result AS Tvector
result.x = vec1.x
result.y = COS(angle) * vec1.y - SIN(angle) * vec1.z
result.z = SIN(angle) * vec1.y + COS(angle) * vec1.z
RETURN result
ENDFUNCTION

FUNCTION Vector_RotateAroundY AS Tvector: vec1 AS Tvector, angle
LOCAL result AS Tvector
result.x  = COS(angle) * vec1.x + SIN(angle) * vec1.z
result.y = vec1.y
result.z = -SIN(angle) * vec1.x + COS(angle) * vec1.z
RETURN result
ENDFUNCTION

FUNCTION Vector_RotateAroundZ AS Tvector: vec1 AS Tvector, angle
LOCAL result AS Tvector
result.x = COS(angle) * vec1.x - SIN(angle) * vec1.y
result.y = SIN(angle) * vec1.x + COS(angle) * vec1.y
result.z = vec1.z
RETURN result
ENDFUNCTION
Title: Camera Vector
Post by: bigsofty on 2007-Apr-14
Hmm, looking at it, It looks like the camera axis is rotating on a fixed axis, as opposed to an already rotated one (camera already moved...)... this is all very confusing, hehe :P
Title: Camera Vector
Post by: bigsofty on 2007-Apr-14
Yes, that's the problem, the camera vector seems to be based on a fixed set of axis and its not relative to the current camera vector... hmmm, knowing the problem does not help with the solution though. :(
Title: Camera Vector
Post by: bigsofty on 2007-Apr-15
Put a halt to this approach, saw a nice article on polar co-ordinates, Ill give that a bash instead ;)
Title: Camera Vector
Post by: Kitty Hello on 2007-Apr-19
The camera always points from cam_pos to cam_view. However, you could rotate the camera around this axis. To define this angle, too, you need an "up" direction. It is 0,1,0 by default and can be changed by X_CAMERAUP.
So, in your case, you should make an up vector as well, and rotate this one toghether with the view-angle.
Title: Camera Vector
Post by: bigsofty on 2007-Apr-19
Thanks Gernot, Ive already got this working but no information is wasted information... :D

Im back to trying to fix my alphablending problems with a 3d particle system...