OpenGL Rotating and Moving

Advanced game related topics
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

OpenGL Rotating and Moving

Post by DarkDragon »

Hello,

I want to move absolut and rotate relative, but it seems not to work:

Image

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 :cry: .

Thx
bye,
Daniel
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post 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?!

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post 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?
bye,
Daniel
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post 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.
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post 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
bye,
Daniel
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Uhm, when it rotates, it gets smaller(45° = 0.25, 90° = 0.0).
bye,
Daniel
BillNee
User
User
Posts: 93
Joined: Thu Jan 29, 2004 6:01 am
Location: Homosassa, FL

Post 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
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post 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.
bye,
Daniel
BillNee
User
User
Posts: 93
Joined: Thu Jan 29, 2004 6:01 am
Location: Homosassa, FL

Post 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
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Thanks, it works now. Now I'm happy.
bye,
Daniel
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

:cry: 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
bye,
Daniel
Post Reply