Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (AndrewAMD, TipmyPip), 12,420 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Logic behind slowly rotating an entity to point at an object #393892
02/08/12 05:56
02/08/12 05:56
Joined: Sep 2003
Posts: 353
Mahonroy Offline OP
Senior Member
Mahonroy  Offline OP
Senior Member

Joined: Sep 2003
Posts: 353
Hello,
I am trying to program an entity to slowly rotate towards an object. I did the following below, which works, but it doesn't account for the fact that if the entity is facing lets say 355 degrees, and it needs to rotate to 5 degrees to point towards object, it goes all the way around the long way to get there. Here is what I have:
Code:
vec_set(vTarget_Temp,your.x); 
vec_sub(vTarget_Temp,my.x);
vec_to_angle(vTarget_Angle,vTarget_Temp);
if(my.pan > vTarget_Angle.x)
{
	my.pan -= time_step;
	//if((my.pan - vTarget_Angle.x) > 180){my.pan += time_step;}
	//if((my.pan - vTarget_Angle.x) <= 180){my.pan -= time_step;}
}
  				
if(my.pan < vTarget_Angle.x)
{
	my.pan += time_step;
	//if((my.pan - vTarget_Angle.x) > 180){my.pan += time_step;}
	//if((my.pan - vTarget_Angle.x) <= 180){my.pan -= time_step;}
}
  				
wait(1);


You can see the commented out stuff that I was trying to do, but it just was not working right. Any ideas?


NATIO Released! Check it out at http://mahonroy.home.comcast.net/natio.htm -Matt AMD 1.2 Ghz Thunderbird 512 Meg Ram GeForce 2 Ultra 64 Meg DDR Using A5 Professional
Re: Logic behind slowly rotating an entity to point at an object [Re: Mahonroy] #393899
02/08/12 09:26
02/08/12 09:26
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Im going to give you two examples of the same process. Based on yours BTW...
I give you two because the first is to CLEARLY demonstrate how it is working.

The second corrects some shortcomings of the first, but is not so easy to read,
unless you have a basic understanding of the first...


So ... at its most basic, this is a better process to use...
Code:
vec_set(vTarget_Temp,your.x); 
vec_sub(vTarget_Temp,my.x);
vec_to_angle(vTarget_Angle, vTarget_Temp);

if     (ang(vTarget_Angle.x - my.pan + 360)<0)
{
	my.pan -= time_step;
}
else if(ang(vTarget_Angle.x - my.pan + 360)>0)
{
	my.pan += time_step;
}

It works by calculating the difference between the current pan and the target pan,
then pushing the answer into an area where any POSSIBLE answer is greater than zero,
then uses the ANG() function to turn it back into a 'directional' angle...


Unfortunately, it suffers with 'the jitters' when it (nearly) reaches the target.
Here is an improved version that eliminates the jitters.
Code:
vec_set(vTarget_Temp,your.x); 
vec_sub(vTarget_Temp,my.x);
vec_to_angle(vTarget_Angle, vTarget_Temp);
var diff = ang(vTarget_Angle.x - my.pan + 360);

if(diff<0)
{
	if(abs(diff)>time_step)		{	my.pan -= time_step;	}
	else				{	my.pan -= abs(diff);	}
}
  				
else if(diff>0)
{
	if(abs(diff)>time_step)		{	my.pan += time_step;	}
	else				{	my.pan += abs(diff);	}
}




[EDIT] And after some further thought... If you want to try and decypher a
REALLY compact version of the self-same process that avoids those nasty IF statements,
then try this on for size...
Code:
vec_to_angle(vTarget_Angle, vec_sub(vec_set(vTarget_Temp, your.x),my.x));
var diff = ang(vTarget_Angle.x - my.pan + 360);
my.pan += minv(time_step, abs(diff)) * sign(diff);


Enjoy!



Last edited by EvilSOB; 02/08/12 11:27. Reason: update

"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Logic behind slowly rotating an entity to point at an object [Re: EvilSOB] #393915
02/08/12 12:44
02/08/12 12:44
Joined: Sep 2003
Posts: 353
Mahonroy Offline OP
Senior Member
Mahonroy  Offline OP
Senior Member

Joined: Sep 2003
Posts: 353
Thanks for the reply this helps out a lot!


NATIO Released! Check it out at http://mahonroy.home.comcast.net/natio.htm -Matt AMD 1.2 Ghz Thunderbird 512 Meg Ram GeForce 2 Ultra 64 Meg DDR Using A5 Professional

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1