Advertisement

Rotate Point Around Vector

Started by March 13, 2002 04:29 AM
-1 comments, last by TheMick 22 years, 11 months ago
I''m attempting to use the Quake1 function RotatePointAroundVector to rotate the player''s view. I''m using 3 mutually orthogonal unit vectors to store the player''s orientation. For now I''m attempting to rotate around each of the 3 player''s own orientation vectors, later on I hope to rotate around any vector that goes through the player''s origin. At first viewangles are used to store the amount of rotation on each axis, then used to send the orientation to the renderer, but are not used to store the player''s orientation, orientation is stored in the orientation vectors. I made a slighty modified RotatePointAroundVector function that rotates 3 points in one call. I copied over the vectortoangles function from cl.cam.c, which doesn''t figure roll, but that just means the roll angle isn''t being rendered. The problem is it simply doesn''t work, the viewangles don''t seem to be modified. Am I using RotatePointAroundVector inappropiately? Also, is there a way to not convert the orientation vectors to angles to OpenGL for the rendered view, and just pass it the orientation vectors? Thanks for any corrections/advice you can offer. /* -3 mutually orthogonal unit vectors that all originate at translational position (0,0,0) so thier endpoints equals thier direction -add vec3_t fvec,rvec,uvec in file client.h structure client_state_t -add contents of this file to the end of mathlib.c */ void InitVectors (void) //call this in file cl.input.c function CL_InitInput { /* cl.fvec = {0,0,1}; cl.rvec = {1,0,0}; cl.uvec = {0,1,0}; */ cl.fvec[0] = 0; cl.fvec[1] = 0; cl.fvec[2] = 1; cl.rvec[0] = 1; cl.rvec[1] = 0; cl.rvec[2] = 0; cl.uvec[0] = 0; cl.uvec[1] = 1; cl.uvec[2] = 0; Cbuf_AddText ("echo You should only see this once\n"); } void ClearAngles (void) //call this in file cl.input.c function CL_SendCmd right before CL_BaseMove function call { cl.viewangles[ROLL] = cl.viewangles[PITCH] = cl.viewangles[YAW] = 0; } void RotateAroundVector( vec3_t dir, float degrees ) { float m[3][3]; float im[3][3]; float zrot[3][3]; float tmpmat[3][3]; float rot[3][3]; int i; vec3_t vr, vup, vf; vf[0] = dir[0]; vf[1] = dir[1]; vf[2] = dir[2]; PerpendicularVector( vr, dir ); CrossProduct( vr, vf, vup ); m[0][0] = vr[0]; m[1][0] = vr[1]; m[2][0] = vr[2]; m[0][1] = vup[0]; m[1][1] = vup[1]; m[2][1] = vup[2]; m[0][2] = vf[0]; m[1][2] = vf[1]; m[2][2] = vf[2]; memcpy( im, m, sizeof( im ) ); im[0][1] = m[1][0]; im[0][2] = m[2][0]; im[1][0] = m[0][1]; im[1][2] = m[2][1]; im[2][0] = m[0][2]; im[2][1] = m[1][2]; memset( zrot, 0, sizeof( zrot ) ); zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F; zrot[0][0] = cos( DEG2RAD( degrees ) ); zrot[0][1] = sin( DEG2RAD( degrees ) ); zrot[1][0] = -sin( DEG2RAD( degrees ) ); zrot[1][1] = cos( DEG2RAD( degrees ) ); R_ConcatRotations( m, zrot, tmpmat ); R_ConcatRotations( tmpmat, im, rot ); for ( i = 0; i < 3; i++ ) { cl.fvec = rot[0] * cl.fvec[0] + rot[1] * cl.fvec[1] + rot[2] * cl.fvec[2]; cl.rvec = rot[0] * cl.rvec[0] + rot[1] * cl.rvec[1] + rot[2] * cl.rvec[2]; cl.uvec = rot[0] * cl.uvec[0] + rot[1] * cl.uvec[1] + rot[2] * cl.uvec[2]; } } void toangles(vec3_t vec) { float forward; float yaw, pitch; if (vec[1] == 0 && vec[0] == 0) { yaw = 0; if (vec[2] > 0) pitch = 90; else pitch = 270; } else { yaw = (int) (atan2(vec[1], vec[0]) * 180 / M_PI); if (yaw < 0) yaw += 360; forward = sqrt (vec[0]*vec[0] + vec[1]*vec[1]); pitch = (int) (atan2(vec[2], forward) * 180 / M_PI); if (pitch < 0) pitch += 360; } cl.viewangles[0] = pitch; cl.viewangles[1] = yaw; cl.viewangles[2] = 0; } void ThreeRotations (void) //call this in file cl.input.c function CL_SendCmd right after IN_Move function call { RotateAroundVector(cl.fvec, cl.viewangles[ROLL]); RotateAroundVector(cl.rvec, cl.viewangles[PITCH]); RotateAroundVector(cl.uvec, cl.viewangles[YAW]); toangles(cl.fvec); } </source> </i>

This topic is closed to new replies.

Advertisement