Page 1 of 1
OpenGL Rotating and Moving
Posted: Fri May 28, 2004 12:04 pm
by DarkDragon
Hello,
I want to move absolut and rotate relative, but it seems not to work:
Uhm the movement is always relative to the rotation(if the rotation ends before the moving) and the rotation is always relative to the movement(if the movement ends before rotating).
So help me please

.
Thx
Posted: Fri May 28, 2004 2:29 pm
by LarsG
Movement relative to the direction?!? sounds weird..
Isn't it as simple as storing the velocity (movement direction) on a two/three variables (depending on if it's 2D or 3D), and rotation values in two/three other variables?!
Posted: Fri May 28, 2004 3:44 pm
by DarkDragon
Thanks for one answer, but it isn't as easy as you think:
Code: Select all
glTranslatef_(OBJ()\X, OBJ()\Y, OBJ()\Z)
glRotatef_(OBJ()\AngleX, 1.0, 0.0, 0.0)
glRotatef_(OBJ()\AngleY, 0.0, 1.0, 0.0)
glRotatef_(OBJ()\AngleZ, 0.0, 0.0, 1.0)
This moves the object to the Position and rotates it around the zeropoint.
Code: Select all
glRotatef_(OBJ()\AngleX, 1.0, 0.0, 0.0)
glRotatef_(OBJ()\AngleY, 0.0, 1.0, 0.0)
glRotatef_(OBJ()\AngleZ, 0.0, 0.0, 1.0)
glTranslatef_(OBJ()\X, OBJ()\Y, OBJ()\Z)
this rotates and moves FORWARD to the OBJ()\Angle direction.
but how can I move absolute and rotate through the meshself axis?
Posted: Fri May 28, 2004 4:38 pm
by dmoc
Hmmm, at a guess (without code) I'd say your mixing up a vector with a translation, ie, the vector should be added to the translation to get new position. Come back with code if this doesn't help.
Posted: Thu Jun 17, 2004 6:05 pm
by DarkDragon
Uhm well it works, but I had a 3rd person camera, so it looks a bit strange.
But now there is another question:
Why doesn't this work?
Code: Select all
Structure POINT3D
X.f
Y.f
Z.f
EndStructure
Procedure Rotate(*Buffer, X.f, Y.f, Z.f, angle_x.f, angle_y.f, angle_z.f)
*Buffer = ReAllocateMemory(*Buffer, 4*3)
angle_x / 180 *(ATan(1)*4) ;Translate in RAD
angle_y / 180 *(ATan(1)*4)
angle_z / 180 *(ATan(1)*4)
;
; Rotate X
;
X = X
Y = Cos(angle_x) * Y - Sin(angle_x) * Z
Z = Sin(angle_x) * Y + Cos(angle_x) * Z
;
; Rotate Y
;
X = Cos(angle_y) * X + Sin(angle_y) * Z
Y = Y
Z = -Sin(angle_y) * X + Cos(angle_y) * Z
;
; Rotate Z
;
X = Cos(angle_z) * X - Sin(angle_z) * Y
Y = Sin(angle_z) * X + Cos(angle_z) * Y
Z = Z
PokeF(*Buffer, X)
PokeF(*Buffer+4, Y)
PokeF(*Buffer+8, Z)
ProcedureReturn 4*3
EndProcedure
*Buffer.POINT3D = AllocateMemory(3*4)
angle_x.f = 0
angle_y.f = 90
angle_z.f = 0
Rotate(*Buffer.POINT3D, -0.5, 0.0, 0.0, angle_x.f, angle_y.f, angle_z.f)
Debug *Buffer\X
Debug *Buffer\Y
Debug "should be 0.5:"
Debug *Buffer\Z
Posted: Sun Jun 20, 2004 4:34 pm
by DarkDragon
Uhm, when it rotates, it gets smaller(45° = 0.25, 90° = 0.0).
Posted: Mon Jun 21, 2004 12:17 am
by BillNee
I think you need to say womething like -
xx=....
yy=...
x=xx:y=yy
when you compute a new x and then use that value to compute a new y the answers will be wrong.
Bill Nee
Posted: Mon Jun 21, 2004 5:47 am
by DarkDragon
Thanks, it works nearly perfect, but now only one number is uncorrect:
Code: Select all
Structure POINT3D
X.f
Y.f
Z.f
EndStructure
Procedure Rotate(*Buffer.POINT3D, X.f, Y.f, Z.f, angle_x.f, angle_y.f, angle_z.f)
*Buffer.POINT3D = ReAllocateMemory(*Buffer.POINT3D, 3*4)
PI.f = ATan(1)*4
;
; Rotate X
;
YY.f = Cos(angle_x*(PI/180)) * Y - Sin(angle_x*(PI/180)) * Z
ZZ.f = Sin(angle_x*(PI/180)) * Y + Cos(angle_x*(PI/180)) * Z
;
; Rotate Y
;
XX.f = Cos(angle_y*(PI/180)) * X + Sin(angle_y*(PI/180)) * ZZ
ZZ.f = -Sin(angle_y*(PI/180)) * X + Cos(angle_y*(PI/180)) * ZZ
;
; Rotate Z
;
XX.f = Cos(angle_z*(PI/180)) * XX - Sin(angle_z*(PI/180)) * YY
YY.f = Sin(angle_z*(PI/180)) * XX + Cos(angle_z*(PI/180)) * YY
*Buffer\X = XX.f
*Buffer\Y = YY.f
*Buffer\Z = ZZ.f
ProcedureReturn 4*3
EndProcedure
*Buffer.POINT3D = AllocateMemory(4*3)
angle_x.f = 0
angle_y.f = 45
angle_z.f = 0
Rotate(*Buffer.POINT3D, -0.5, 0.0, 0.0, angle_x.f, angle_y.f, angle_z.f)
Debug "should be -0.25:"
Debug *Buffer\X
Debug *Buffer\Y
Debug *Buffer\Z
45° means 2 variables are the same.
Posted: Mon Jun 21, 2004 2:36 pm
by BillNee
In your Rotate z part, you're computing a new XX in the first line but then using that XX to compute a new YY. Guess you might say XXX= for the first line. I find it easier to always replace xx,yy,zz with x,y,z at each step along the way after computing their new values. It's a lot easier and less complicated. Wait till you try 4D rotation with 6 sets of computations. For 3D I use the order
xx=...
yy=...
x=xx:y=yy
yy=...
zz=...
y=yy:z=zz
zz=...
xx=...
z=zz:x=xx
this keeps the sine, cosine formula the same for all three sets of computations and is easier to see any mistake. I can use the formula as a procedure and just substitute (x,y), (y,z) and (z,x) in the procedure. Guess you really dont have to call the second line yy,zz,xx; could use y,z,x and omit the y=yy,z=zz,x=xx but I'm oldfashioned and the speed of PB doesn't seem to matter.
Bill Nee
Posted: Mon Jun 21, 2004 4:14 pm
by DarkDragon
Thanks, it works now. Now I'm happy.
Posted: Thu Jun 24, 2004 2:50 pm
by DarkDragon

When I'm now rotating my autocollision around X=90°, Y=0°, Z=45° The object goes down into the earth. If Z=0° all is perfect. So the Z° goes a wrong way I think:
Code: Select all
Procedure Rotate(*Buffer.POINT3D, X.f, Y.f, Z.f, angle_x.f, angle_y.f, angle_z.f)
*Buffer.POINT3D = ReAllocateMemory(*Buffer.POINT3D, 3*4)
PI.f = ATan(1)*4
;
; Rotate X
;
YY.f = Cos(angle_x*(PI/180)) * Y - Sin(angle_x*(PI/180)) * Z
ZZ.f = Sin(angle_x*(PI/180)) * Y + Cos(angle_x*(PI/180)) * Z
Y=YY
Z=ZZ
;
; Rotate Y
;
XX.f = Cos(angle_y*(PI/180)) * X + Sin(angle_y*(PI/180)) * Z
ZZ.f = -Sin(angle_y*(PI/180)) * X + Cos(angle_y*(PI/180)) * Z
X=XX
Z=ZZ
;
; Rotate Z
;
XX.f = Cos(angle_z*(PI/180)) * X - Sin(angle_z*(PI/180)) * Y
YY.f = Sin(angle_z*(PI/180)) * X + Cos(angle_z*(PI/180)) * Y
X=XX
Y=YY
*Buffer\X = XX.f
*Buffer\Y = YY.f
*Buffer\Z = ZZ.f
ProcedureReturn 4*3
EndProcedure