Archived

This topic is now archived and is closed to further replies.

3D object rotation / heading

This topic is 5130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok. So, I''ve implemented objects in my raycasting engine! They work great, but now I want to extend them so that depending on the angle you are facing them and their angle (heading), you will see a different side of them. I don''t have too much memory (GBA), so I decided to go with only 4 directions (facing the player, facing the left, facing the right, and facing away from the player). I keep pointers to the images in the object''s sturcture in an array of 4 (for the 4 different directions). So, now I just need to find a way to be able to use the player''s angle and the objects angle together to get the correct direction the object should be facing. I tried this:
deltaHeading = ABS(playerAngle - pObj->angle) >> 10;
objectImage = pObj->images[deltaHeading];
However this didn''t seem to work right (by the way, my angle system is 4096 based, so each direction represents 90 degrees...thats the reason for the ">> 10"...dividing by 1024). I know I''m on the right track. Any ideas? Thanks!

Share this post


Link to post
Share on other sites
I would have thought the angle in which you are facing shouldn''t have any effect on the orientation of the object...

Call the vector O the orientation of the object,
point A is the position of the object and
point B is the position of the player...

so if you find the angle between O and A-B that will determine at what side the object should be showing... You''ll have to use a combination of cross product and dot product to get a range of 0 to 360 degrees...

Share this post


Link to post
Share on other sites
you abs an angle, that sounds bad

try deltaheading = (playerAngle - pObj->angle); // between -2048, +2048
if (deltaheading < 0) deltaheading += 4096; // make the angle between 0, +4096


Now, what happens if the angle is 4095. What you''ll get is 3, and you want 0 (it''s almost facing you). so you need an offset, so that


[3584, 511] => 0
[512, 1535] => 1
[1536, 2559] => 2
[2560, 3583] => 3

so
int deltaangle = playerAngle - pObj->angle; // between -2048, +2048

deltaheading = (deltaangle + 512); // between -2048, +2048, with 45 degree offset.
if (deltaheading < 0) deltaheading += 4096; // make the angle between 0, +4096

int index = deltaheading >> 10;

Share this post


Link to post
Share on other sites
hmmm.. I also seemed to forgot, if you add 512, the angle will exceed 4096 so you need to substract 4096 if the angle > 4096

int deltaangle = playerAngle - pObj->angle;
deltaheading = (deltaangle + 512);

if (deltaheading < 0)
deltaheading += 4096;
else if (deltaheading >= 4096)
deltaheading -= 4096;

int index = deltaheading >> 10;


[edited by - oliii on November 29, 2003 10:15:22 PM]

Share this post


Link to post
Share on other sites