Smooth Rotation And Rotate to Specific Degree

Discuss programming in the QuakeC language.
Post Reply
Junrall
Posts: 191
Joined: Mon Sep 21, 2009 12:27 am
Location: North West Oregon, USA
Contact:

Smooth Rotation And Rotate to Specific Degree

Post by Junrall »

Hello all,

I'm dinking around with DP and it's bot spawning function spawnclient().

Does anyone know of a way to easily use DP to smoothly rotate a bot's yaw to a specific degree? I'm not familiar with DP, so I might be missing something in the dpextensions.qc file.

I have been experimenting with a few things:
self.v_angle_y = self.v_angle_y + 5 gives me very smooth rotation, which is what I want but I have no idea how to stop the rotation at a specified yaw degree.

self.angles_y = self.angles_y - 35 rotates the bot 35 degrees to its right, but its instant and not a smooth turn.

I feel like I'm kinda on the cusp. Any guidance would be appreciated.

Perhaps something like this? (nvm... I tried this and it didn't work. lol)

if (self.angles_y != 35)
self.v_angle_y = self.v_angle_y + 5;
Good God! You shot my leg off!
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Smooth Rotation And Rotate to Specific Degree

Post by Spike »

self.ideal_yaw = vectoangle(self.enemy.origin - self.origin);
self.yaw_speed = 5;
changeyaw();
Junrall
Posts: 191
Joined: Mon Sep 21, 2009 12:27 am
Location: North West Oregon, USA
Contact:

Re: Smooth Rotation And Rotate to Specific Degree

Post by Junrall »

Thanks for the response Spike,

Based on what you shared, I had thought that the following would work, but it did not. (self.ideal_yaw is a float and would not accept vectorangles directly.)

vector tempvec;

tempvec = vectoangles('0 200 0' - self.origin);

self.ideal_yaw = tempvec_y;
self.yaw_speed = 5;
ChangeYaw();


Knowing that self.ideal_yaw is a float, I would assume that I could just plug a value directly into self.ideal_yaw to rotate the bot.... I was wrong, it did not work.

self.ideal_yaw =75;
self.yaw_speed = 5;
ChangeYaw();


Sorry for the basic questions and not seemingly to have a clue. It's been years since I last played with this. I once had a basic understanding of this, and now I remember nothing!
Last edited by Junrall on Sun Nov 15, 2015 7:38 am, edited 1 time in total.
Good God! You shot my leg off!
Junrall
Posts: 191
Joined: Mon Sep 21, 2009 12:27 am
Location: North West Oregon, USA
Contact:

Re: Smooth Rotation And Rotate to Specific Degree

Post by Junrall »

Sorry,

tempvec = vectoangles(self.enemy.origin - self.origin);

should be

tempvec = vectoangles('0 200 0' - self.origin);
Good God! You shot my leg off!
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Smooth Rotation And Rotate to Specific Degree

Post by Spike »

vectoyaw sorry, at least if you're after just yaw angles.

//determine angle. note that vectoyaw is mostly equivelent to atan2(dir_y,dir_x)
float dir = targetpoint - entorigin;
newangle = vectoyaw(dir);
//figure out the change
float change = newangle - oldangle;
//and make sure the range is sane - specifically adjust it so it can cope with the fact that angles wrap.
while (change > 180)
change = change - 360;
while (change < -180)
change = change + 360;
//now clamp the change to the maximum angle change allowed maxchange could be changepersec*frametime if your entity is thinking at a variable framerate.
if (change < -maxchange)
change = -maxchange;
if (change > maxchange)
change = maxchange;
//and calculate the new angle.
newangle = oldangle + change;

if you want to interpolate between an old angle and a new angle, you can precompute the initial newangle, remove the maxchange stuff, and then tweak that last line to:
newangle = oldangle + change*(time-oldtime)/(nexttime-oldtime);
Junrall
Posts: 191
Joined: Mon Sep 21, 2009 12:27 am
Location: North West Oregon, USA
Contact:

Re: Smooth Rotation And Rotate to Specific Degree

Post by Junrall »

Been struggling with trying to get my brainless bot to rotate smoothly in one direction or another. I have finally discovered why it has not been working for me.

I have been using DP's spawnclient to spawn a bot into a client slot and discovered that ChangeYaw does not work with bot spawned like this..

To test this, I set up two different bot spawn functions. One using DP's spawnclient and one using the standard spawning method that everyone uses. Then, in the player_stand function I added the lines:

Code: Select all

self.ideal_yaw = 65;
self.yaw_speed = 5;
ChangeYaw ();
The above code did not effect the DP spawnclient bot at all (well, it did jitter slightly), but the bot that was spawned with the standard method rotated perfectly.

So, I got to thinking... (this is where my two brain cells start to wrestle!)

DP's self.movement_y will move the client bot in whatever direction self.v_angle_y is facing (this does not work for self.angles_y).
In the Quake source there is a ChangYaw function that is commented out. I un-commented that function and renamed it to ChangeYaw2 and changed two instances of self.angles_v to self.v_angle_y.

Code: Select all

void () ChangeYaw2 = {
	local float		ideal, move;

//current_yaw = self.ideal_yaw;
// mod down the current angle
	current_yaw = anglemod( self.v_angle_y );                    //changed self.angles  to  self.v_angle_y
	ideal = self.ideal_yaw;
	
	if (current_yaw == ideal)
		return;
	
	self.yaw_speed = (frametime / 0.1) * self.yaw_speed;    //Borrowed this from Orion's bot. (I'll give it back when done!)
	
	move = ideal - current_yaw;
	if (ideal > current_yaw) {
		if (move > 180)
			move = move - 360;
	}
	else {
		if (move < -180)
			move = move + 360;
	}
		
	if (move > 0) {
		if (move > self.yaw_speed)
			move = self.yaw_speed;
	}
	else {
		if (move < 0-self.yaw_speed )
			move = 0-self.yaw_speed;
	}

	current_yaw = anglemod (current_yaw + move);

	]self.v_angle_y = current_yaw;                                     //changed self.angles  to  self.v_angle_y
};
Then I used the following in the bot's think function (tied to PlayerPreThink):

Code: Select all

self.ideal_yaw = 65;
self.yaw_speed = 20;
ChangeYaw2 ();
This works perfectly! It mimics player movement, which is... when a true player is moving forward and changes direction, the initial change is in the form of an arc.

The new CheckYaw2 does not work with bots that have been spawned using the standard spawning method.
Good God! You shot my leg off!
r00k
Posts: 1111
Joined: Sat Nov 13, 2004 10:39 pm

Re: Smooth Rotation And Rotate to Specific Degree

Post by r00k »

this confuses me

Code: Select all

 if (move < 0-self.yaw_speed )
         move = 0-self.yaw_speed;
frag.machine
Posts: 2126
Joined: Sat Nov 25, 2006 1:49 pm

Re: Smooth Rotation And Rotate to Specific Degree

Post by frag.machine »

r00k wrote:this confuses me

Code: Select all

 if (move < 0-self.yaw_speed )
         move = 0-self.yaw_speed;
It's just limiting the value of move to the range between -self.yaw_speed and self.yaw_speed. But I agree it could be more concise:

Code: Select all

if (move < 0 - self.yaw_speed) { move = 0-self.yaw_speed; }
else if (move > self.yaw_speed) { move = self.yaw_speed; }
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
Junrall
Posts: 191
Joined: Mon Sep 21, 2009 12:27 am
Location: North West Oregon, USA
Contact:

Re: Smooth Rotation And Rotate to Specific Degree

Post by Junrall »

frag.machine wrote: It's just limiting the value of move to the range between -self.yaw_speed and self.yaw_speed. But I agree it could be more concise:

Code: Select all

if (move < 0 - self.yaw_speed) { move = 0-self.yaw_speed; }
else if (move > self.yaw_speed) { move = self.yaw_speed; }
Haha... yeah, I didn't like how it read either. I made the same change as well. That wacky stuff is all over the place in the Quake source.
Good God! You shot my leg off!
Post Reply