YawToEntity Function

Blitz3D Forums/Blitz3D Beginners Area/YawToEntity Function

Aussie(Posted 2012) [#1]
I am having some strange issues with this function.

If i move towards the enemy the enemy turns away from the player on the south side of the sphere. If i chase the enemy the enemy will eventually start to point towards the player.

I am wondering if it has something to do with the playing area being a sphere.

Here is the code.
If you hold down the up arrow straight away you will see what happens when the player gets close to the enemy.


Last edited 2012


Floyd(Posted 2012) [#2]
It's not entirely clear to me what should be happening. But you probably want to turn around the world Y axis, meaning set the global flag to True when you turn the enemy. Near the end of the code that would be
	; If the required correction amount is less than the correction amount to be applied...
	If Abs( target_yaw# ) < rate#
		; Point 'src_entity' directly at 'dest_entity' to prevent jittering.
		TurnEntity src_entity, 0.0, target_yaw#, 0.0, True
	Else
		; Turn 'src_entity' gradually towards 'dest_entity'.
		TurnEntity src_entity, 0.0, rate# * Sgn(  target_yaw# ), 0.0, True
	EndIf

You can find my description of DeltaYaw here.
That was four and a half years ago and I was just posting something I wrote many years earlier. So I don't remember much about it.


Aussie(Posted 2012) [#3]
Hey there Floyd,
What should be happening is when the played gets close to the enemy the enemy should point at the player to give chase.
Zethrax pointed me to his Function YawToEntity http://www.blitzbasic.com/codearcs/codearcs.php?code=2258 in another thread.
I have set the global flag to true & i still get the same thing happening.


Floyd(Posted 2012) [#4]
Okay, I hadn't really thought about the spherical world.

The correct approach is the opposite of what I was thinking. Instead of looking at this globally do it locally, from the point of view of the enemy. The only decision is which way to turn.

The enemy entity has his own coordinate system. Y+ is up, Z+ is forward and X+ and X- are right and left. We need only decide whether the target ( the player ) is to the right or left as seen by the enemy. The player is always at 0,0,0 in his own space so we transform that to the enemy's space. Then TFormedX() tells us which side the player is on. If this value is positive then the player is to the right of the enemy, and the enemy must turn clockwise as seen from above. That means a negative change to the Yaw angle. Thus the sign of the Yaw change is the opposite of the sign of TFormedX().

With that in mind the last part of YawToEntity becomes

		; Turn 'src_entity' gradually towards 'dest_entity'.
		TFormPoint 0,0,0,  dest_entity, src_entity
		TurnEntity src_entity, 0.0, rate# * -Sgn( TFormedX() ), 0.0


Last edited 2012


Kryzon(Posted 2012) [#5]
The enemy entity has his own coordinate system. Y+ is up, Z+ is forward and X+ and X- are right and left. We need only decide whether the target ( the player ) is to the right or left as seen by the enemy.

Hi Floyd, I've implemented your suggestion as a simple AI following the player - link to post.