Page 1 of 1

Smooth Rotation And Rotate to Specific Degree

Posted: Thu Nov 12, 2015 9:58 am
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;

Re: Smooth Rotation And Rotate to Specific Degree

Posted: Fri Nov 13, 2015 1:34 am
by Spike
self.ideal_yaw = vectoangle(self.enemy.origin - self.origin);
self.yaw_speed = 5;
changeyaw();

Re: Smooth Rotation And Rotate to Specific Degree

Posted: Fri Nov 13, 2015 6:59 am
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!

Re: Smooth Rotation And Rotate to Specific Degree

Posted: Fri Nov 13, 2015 7:02 am
by Junrall
Sorry,

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

should be

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

Re: Smooth Rotation And Rotate to Specific Degree

Posted: Fri Nov 13, 2015 7:54 am
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);

Re: Smooth Rotation And Rotate to Specific Degree

Posted: Sun Nov 15, 2015 7:26 am
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.

Re: Smooth Rotation And Rotate to Specific Degree

Posted: Mon Nov 16, 2015 10:45 am
by r00k
this confuses me

Code: Select all

 if (move < 0-self.yaw_speed )
         move = 0-self.yaw_speed;

Re: Smooth Rotation And Rotate to Specific Degree

Posted: Mon Nov 16, 2015 12:06 pm
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; }

Re: Smooth Rotation And Rotate to Specific Degree

Posted: Tue Nov 17, 2015 6:35 am
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.