Allegro.cc Forums » Programming Questions » 3D rotation using Allegro 5.2.4

Credits go to SiegeLord for helping out!
 This thread is locked; no one can reply to it.
 3D rotation using Allegro 5.2.4
 DontKnow3D Member #16,883 July 2018 I don't quite get how the 3D rotation transformations work. In my program I first rotate an object and then move it.(you can rotate it by the arrow keys and space and m.) I would think that the object would just rotate where it is but no; in a full rotation it first is where it should be, then it rotates in a circle to 0,0,0 and then it rotates back to where it should be. Why doesn't it just rotate where the object is? Why does it move to a different place when rotated?translation code: #SelectExpand 1al_identity_transform(&transform); 2al_rotate_transform_3d(&transform, 1, 0, 0, x); 3al_rotate_transform_3d(&transform, 0, 1, 0, y); 4al_rotate_transform_3d(&transform, 0, 0, 1, z); 5al_translate_transform_3d(&transform, 0, 0, -4000); 6al_use_transform(&transform); whole code: #SelectExpand 1#include 2#include 3#include 4#include 5 6#define W 1000 7#define H 1000 8#define PPHI 100 9#define PTHETA 100 10#define XYZ_MOVE_AMOUNT 0.01 11#define AMOUNT_OF_INDEXES (PPHI*PTHETA * 2-PPHI) 12#define LOOP(var,max) for(var=0;var
 Edgar Reynaldo Major Reynaldo May 2007 That's not how you use al_rotate_transform_3d. Or at least, I don't think you're doing it right. In your case, x,y, and z would be the angle you rotate around the axis. What you do is rotate around a single axis. That will make it spin. If you rotate around two axes, you can get roll, yaw, and pitch. But you need to rotate around the forward, up, and right vectors to do that.
 DontKnow3D Member #16,883 July 2018 I don't quite get what you mean. Could you post some example code?
 Edgar Reynaldo Major Reynaldo May 2007 XYZ defines the vector (axis) you wish to rotate around. Theta is by how much.If you rotate around the z (forward) vector, you get roll.If you rotate around the x (right) vector, you get pitch.If you rotate around the y (up) vector, you get yaw.
 DontKnow3D Member #16,883 July 2018 I thought that that is exactly what im doing. First I rotate by x amount around axis x, then by y amount around axis y etc. and at the end I move it a bit back
 SiegeLord Member #7,827 October 2006 You're missing an al_identity_transform before you construct your perspective transformation (thanks for providing the code, it took me a few minutes to figure out what was going on). "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]
 DontKnow3D Member #16,883 July 2018 yes thank you! I forgot the identity transform...
 Edgar Reynaldo Major Reynaldo May 2007 The thing you have to realize is that you're applying yaw, roll, and pitch improperly.Time for a quick tut.Take your left hand and open it and spread out your fingers. Now close the pinky and ring fingers into your palm while keeping your other fingers in place as best you can. Now form the 3 remaining fingers into the shape of a 3D corner. Now point your middle finger directly towards you. They now form the basis of a Left Handed Coordinate system. Your middle finger points towards positive z, towards the screen, or you. Your pointer finger points towards the positive x axis, and your thumb points towards positive y axis.Now try to rotate your hand around one of your open fingers. You can't do it without affecting the other two axes. This means if you want to apply yaw, pitch, and roll, then you need to modify the other two vectors when you rotate around an axis. So anytime you rotate around one of the three axis vectors (forward, right, up), you change the other two.This means you need to account for that in your code, if you truly want local roll, pitch, and yaw.This is mostly psuedocode. Initialization of fw,rt,and up are up to you. They should be normalized to length 1, and they can simply be your xyz axes.#SelectExpand 1ALLEGRO_TRANSFORM t,t2; 2ALLEGRO_VERTEX fw,rt,up; 3double yaw,pitch,roll; 4 5al_identity_transform(&t); 6al_identity_transform(&t2); 7al_rotate_transform_3d(&t , rt.x , rt.y , rt.z , pitch); 8al_rotate_transform_3d(&t2 , rt.x , rt.y , rt.z , pitch); 9al_transform_coordinates_3d(&t , &up.x , &up.y , &up.z); 10al_transform_coordinates_3d(&t , &fw.x , &fw.y , &fw.z); 11 12al_identity_transform(&t); 13al_rotate_transform_3d(&t , fw.x , fw.y , fw.z , roll); 14al_rotate_transform_3d(&t2 , fw.x , fw.y , fw.z , roll); 15al_transform_coordinates_3d(&t , &up.x , &up.y , &up.z); 16al_transform_coordinates_3d(&t , &rt.x , &rt.y , &rt.z); 17 18al_identity_transform(&t); 19al_rotate_transform_3d(&t , up.x , up.y , up.z , yaw); 20al_rotate_transform_3d(&t2 , up.x , up.y , up.z , yaw); 21al_transform_coordinates_3d(&t , &rt.x , &rt.y , &rt.z); 22al_transform_coordinates_3d(&t , &fw.x , &fw.y , &fw.z); 23 24al_use_transform(&t2);
 Go to: Allegro Development Installation, Setup & Configuration Allegro.cc Comments Off-Topic Ordeals The Depot Game Design & Concepts Programming Questions Recent Threads