Forum

[FTE]Rotate entities around common pivot

Discuss CSQC related programming.

Moderator: InsideQC Admins

[FTE]Rotate entities around common pivot

Postby toneddu2000 » Tue May 23, 2017 4:50 pm

Like said in the title. Is it possible to rotate 2 + entities around an external pivot (it could be an arbitrary vector or whatever)?
I chose CSQC section because I thought (probably wrongly) that CSQC has some ace in its sleeves to accomplish that
Thx guys
toneddu2000
 
Posts: 1201
Joined: Tue Feb 24, 2009 4:39 pm
Location: Italy

Re: [FTE]Rotate entities around common pivot

Postby Spike » Wed May 24, 2017 9:38 am

if you can't use .avelocity with movetype_push then you can at least do it with the following maths:

Code: Select all
vector pivot = self.origin;
vector startangles = self.angles;
vector endangles = self.angles + self.avelocity * frametime;
float pitchscale = (self.solid==SOLID_BSP)?1:-1; //stupid bug. should be for model types rather than solid, but whatever.
startangles_x *= pitchscale;
endangles_x *= pitchscale;

other.angles_x *= -1; //assuming mdl
makevectors(other.angles);
vector efw = v_forward;
//v_right is redundant as it can be calculated with a simple crossproduct if needed.
vector eup = v_up;

makevectors(startangles);
//figure out where the origin is in modelspace.
vector mo, mf, mu, org = other.origin - pivot;
mo_x = org * v_forward;
mo_y = org * v_right;
mo_z = org * v_up;
mf_x = efw * v_forward;
mf_y = efw * v_right;
mf_z = efw * v_up;
mu_x = eup * v_forward;
mu_y = eup * v_right;
mu_z = eup * v_up;
//change the frame of reference, rotating everything around the pivot.
makevectors(endangles);
//figure out where that modelspace position is in worldspace
org = mo_x*v_forward + mo_y*v_right + mo_z*v_up + pivot;
efw = mf_x*v_forward + mf_y*v_right + mf_z*v_up;
eup = mu_x*v_forward + mu_y*v_right + mu_z*v_up;
//and move it there.
other.angles = vectoangles(efw, eup); //assuming mdl
setorigin(other, org);


essentially transforms the position+direction of the 'other' entity by the transpose of the pivot's matrix yielding position+direction within the modelspace of the pivot. the pivot is then rotated (you could also move it too) and the position+directions are then transformed by the non-transpose back into worldspace and then shoved back into the other entity.
(the transpose is basically a poor-man's inverse matrix, and works fine for 3*3 matricies so long as they're normalised - ones like those expressed by v_forward+v_right+v_up. those form a single 3*3 matrix. deal with it.)
it'd be nicer if makevectors didn't write globals, but apparently quakec still sucks, and an actual matrix or quat type might be nice, but oh well.
Spike
 
Posts: 2842
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: [FTE]Rotate entities around common pivot

Postby toneddu2000 » Wed May 24, 2017 2:13 pm

Spike, you're awesome! :) Thanks a lot

I just needed to add
Code: Select all
pivot.avelocity = [0,90,0];
in my CSQC_UpdateView() and voila!
Then, because I didn't need an animation but instead a stepped increment triggered by mouse I also removed frametime in
Code: Select all
vector endangles = self.angles + self.avelocity * frametime;

And now it works great!

So, if iiuc, avelocity is the angular velocity, or better, the increased quantity of velocity gained during rotation?
toneddu2000
 
Posts: 1201
Joined: Tue Feb 24, 2009 4:39 pm
Location: Italy

Re: [FTE]Rotate entities around common pivot

Postby Spike » Wed May 24, 2017 2:52 pm

if all you want is to rotate a model around its own origin (aka: pivot), then yeah, avelocity will work for most movetypes.
this is how grenades and vore missiles etc spin in quake.
if you're setting more than one component at a time then your rotation will be non-linear of course, but that's a general issue with eular angles. you can fix that by generating forwards+up vectors then generating a 3*3 matrix and transforming them by that instead of using avelocity. that would give linear rotation, but would also be noticably more complex than a simple vector addition.

the code I gave in my earlier post was to rotate a second entity around the first entity (effectively as part of that first entity's rotation), but if you were after rotating an entity around its own origin then yeah, just setting avelocity is MUCH easier.
Spike
 
Posts: 2842
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: [FTE]Rotate entities around common pivot

Postby frag.machine » Wed May 24, 2017 7:09 pm

Spike wrote:the code I gave in my earlier post was to rotate a second entity around the first entity (effectively as part of that first entity's rotation), but if you were after rotating an entity around its own origin then yeah, just setting avelocity is MUCH easier.


Don't worry, your effort wasn't in vain. I was trying to write something like that another day and I will shamelessly steal it!
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
User avatar
frag.machine
 
Posts: 2022
Joined: Sat Nov 25, 2006 1:49 pm

Re: [FTE]Rotate entities around common pivot

Postby toneddu2000 » Wed May 24, 2017 7:35 pm

Spike wrote:the code I gave in my earlier post was to rotate a second entity around the first entity (effectively as part of that first entity's rotation), but if you were after rotating an entity around its own origin then yeah, just setting avelocity is MUCH easier.
That was my intent: rotate an object or more around a pivot (which perfectly makes sense to use a second entity), so your code it was just what I was looking for!
A very big request: do you think you could add this code as c-builtin in FTE? Just something like rotate_around_pivot(entity pivot,entity entitytorotate,float angleincrement)
That would be very handy!
toneddu2000
 
Posts: 1201
Joined: Tue Feb 24, 2009 4:39 pm
Location: Italy


Return to CSQC Programming

Who is online

Users browsing this forum: No registered users and 1 guest