# smooth turning for steering behaviors

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

## Recommended Posts

Hi, i am doing my final project and I am having some problems to get a smooth turning of my 3d bots every time they change the target. (I am using steering behaviours: seek ...) Now I am using the arc tangent of VHeading.z/VHeading.x , just to get the absolute angle, but my problem is that I don't get a smooth turning and the bots look like robots every time they turn and it doesn't look realistic; I would like to know if there is any tutorial that explains how to interpolate angles, or if you know a good book or any formula that it could help me . I have read some articles of quaternions but I haven't got anything clear. Now I am using Euler angles, yaw-pitch. I always get stuck on maths !!! Thank you very much again;

##### Share on other sites
A dynamics approach has worked well for me in the past. Basically what you do is calculate the force and torque on the bot using equations like the following:

F = mKv(Vd - V)
τ = I[Kpd - θ) - Kvθ']

F is the force in the forward direction on the bot (whichever way it's facing), m is the mass, Vd and V are the desired velocity and current velocity, Kv is a derivative constant (a different one for each equation), τ is the torque, I is the moment of inertia, θd and θ are the desired heading and the current heading, θ' is the current angular velocity, and Kp is a proportional constant. Essentially what you're doing is using a PID controller for both the force and the torque (in this case it's really just a D- and PD-controller, but you can use whatever you want). All the K constants you pick out of thin air so that you get good results. m and I are pretty arbitrary too. You calculate these equations every frame and apply the resulting force and torque on the bot.

##### Share on other sites

I will check out the website you told me to know how to use these two formulas, because I am a bit lost with that and I don't know how to add them to the updated velocity .

Bye !!!

##### Share on other sites
Another, maybe easier way, is to just cap your turn amount. Have a tuning variable which is the max rotation speed. With this you can calculate the max turn allowed per frame. After you calculate the amount to turn you just do something like:

amtToTurn = min( amtToTurn, MAX_ALLOWED );

Limiting turn rate, however, will introduce new bugs. Notably, orbiting. If your target is inside the circle inscribed by your current velocity and your max turn rate then your bot will orbit the target. You have to detect this edge case and begin slowing down so that you'll either spiral into the target or just come to a stop and turn & face.

-me

##### Share on other sites
Hi, now I have another problem with that

Finally, I have implemented it, increasing the angle until I get the one facing the target to interpolate between two angles.

The corresponding code is:

void CBot::InterpolarAngle(float AngleStart, float AngleEnd){        D3DXVECTOR3 AngleInicial=D3DXVECTOR3(cos(m_YAWFinal),0,sin(m_YAWFinal));        //AngleEnd =   atan2f(VDir.x,VDir.z)        //VDir = heading vector to target	D3DXVECTOR3 AngleFinal = D3DXVECTOR3(cos(AngleEnd),0,sin(AngleEnd));         ////From Radians To degrees        float a = (atan2f(AngleInicial.x,AngleInicial.z))*180/D3DX_PI; 	float b = (atan2f(AngleFinal.x,AngleFinal.z)*180/D3DX_PI;	if(a != b) //If we haven't reached the angle facing the target	{		float AngleResultat = a - b;                //I increase the yaw until I get the one                //that I want to face		m_YAWFinal += DEG2RAD(AngleResultat*0.07);	}		}

The problem I have now, is that sometimes the bots start turning in circles, or spinning . It doesn't happen all the time, but I don't know what's wrong.

Thanks again !!!!

##### Share on other sites
Hi, again !!! I think I have solved it. I forgot to wrap the angle between + 180 and -180, for that reason sometimes the bot was turning without taking into account the shortest way to reach the angle, and It was taking the long way when the angle wasn't in this range [-180..+180].

I will continue testing because there might be more problems with this, or more things to take into account .

Thanks again.

Bye!!!!

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633737
• Total Posts
3013607
×